如何在 Bash 中管理长路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/670488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to manage Long Paths in Bash?
提问by Léo Léopold Hertz ??
I have a problem to manage long paths. How can I get quickly to paths like
我在管理长路径时遇到问题。我怎样才能快速到达像这样的路径
/Users/User/.../.../.../.../.../Dev/C/card.c
I tried an alias
我试过别名
alias cd C='cd /Users/User/.../.../.../.../.../Dev/C'
but I am unable to do aliases for two separate words. I have long lists of Bash aliases and paths in CDPATH, so I am hesitating to make them more. How can manage long paths?
但我无法为两个单独的词做别名。我在 CDPATH 中有很长的 Bash 别名和路径列表,所以我犹豫是否要增加它们。如何管理长路径?
[Ideas for Replies]
【回复思路】
The user litb's reply revealed some of my problems in the management. Things, such as "CTRL+R", "!-3:1:2:4:x" and "incremental search", are hard for me. They probably help in navigating long directories and, in the sense, management.
用户litb的回复暴露了我在管理上的一些问题。诸如“CTRL+R”、“!-3:1:2:4:x”和“增量搜索”之类的东西对我来说很难。它们可能有助于浏览长目录,从某种意义上说,有助于管理。
回答by lhunath
Using symlinksis probably the best idea; but you can do it even easier than dumping them all into your home directory.
使用符号链接可能是最好的主意;但是您可以比将它们全部转储到您的主目录中更容易做到这一点。
As you mentioned, BASH has a feature called CDPATHwhich comes in really handy here.
正如您提到的,BASH 有一个名为CDPATH的功能,在这里非常方便。
Just make a hidden folder in your homedir (so it doesn't clutter your homedir too much):
只需在您的 homedir 中创建一个隐藏文件夹(这样它就不会使您的 homedir 过于混乱):
$ mkdir ~/.paths
$ cd ~/.paths
$ ln -s /my/very/long/path/name/to/my/project project
$ ln -s /some/other/very/long/path/to/my/backups backups
$ echo 'CDPATH=~/.paths' >> ~/.bashrc
$ source ~/.bashrc
This creates a directory in your homedir called ".paths" which contains symlinks to all your long directory locations which you regularly use, then sets the CDPATH bash variable to that directory (in your .bashrc) and re-reads the .bashrc file.
这会在您的 homedir 中创建一个名为“.paths”的目录,其中包含指向您经常使用的所有长目录位置的符号链接,然后将 CDPATH bash 变量设置为该目录(在您的 .bashrc 中)并重新读取 .bashrc 文件。
Now, you can go to any of those paths from anywhere:
现在,您可以从任何地方转到任何这些路径:
$ cd project
$ cd backups
Leaving you with a short CDPATH, no cluttering aliasses, and more importantly: A really easy way to navigate to those long paths from other applications, such as UI applications, by just going into ~/.paths or adding that directory into your UI application's sidebar or so.
给你留下一个短的 CDPATH,没有混乱的别名,更重要的是:一个非常简单的方法来导航到来自其他应用程序的那些长路径,比如 UI 应用程序,只需进入 ~/.paths 或将该目录添加到您的 UI 应用程序的侧边栏左右。
Probably the easiest all-round solution you can have.
可能是您可以拥有的最简单的全方位解决方案。
回答by Juliano
Consider using symbolic links. I have a ~/work/directory where I place symlinks to all my current projects.
考虑使用符号链接。我有一个~/work/目录,用于放置指向所有当前项目的符号链接。
You may also use shell variables:
你也可以使用 shell 变量:
c='/Users/User/.../.../.../.../.../Dev/C'
Then:
然后:
cd "$c"
回答by Dan Breslau
Create symlinks in your home directory (or somewhere else of your choosing)
在您的主目录(或您选择的其他地方)中创建符号链接
ln -slongDirectoryPath ~/MySymLinkName
ln -slongDirectoryPath ~/MySymLinkName
See man lnfor more details.
有关man ln更多详细信息,请参阅。
回答by paxdiablo
Probably the easiest solution is to use:
可能最简单的解决方案是使用:
alias cdc='cd /Users/User/.../.../.../.../.../Dev/C'
alias cdbin='cd /Users/User/.../.../.../.../.../Dev/bin'
alias cdtst='cd /Users/User/.../.../.../.../.../Dev/tst'
if you're only really working on one project at a time. If you work on multiple projects, you could have another alias which changed the directories within those aliases above.
如果您一次只真正从事一个项目。如果您处理多个项目,您可以使用另一个别名来更改上述别名中的目录。
So, you'd use something like:
所以,你会使用类似的东西:
proj game17
cdc
make
proj roman_numerals
cdbin
rm -f *
proj game17 ; cdc
Since this is a useful thing to have, I decided to put together a series of scripts that can be used. They're all based aroung a configuration file that you place in your home directory, along with aliases to source scripts. The file "~/.cdx_data"is of the form:
由于这是一个有用的东西,我决定将一系列可以使用的脚本放在一起。它们都基于您放置在主目录中的配置文件,以及源脚本的别名。该文件"~/.cdx_data"的格式为:
scrabble:top=~/dev/scrabble
scrabble:src=~/dev/scrabble/src
scrabble:bin=~/dev/scrabble/bin
sudoku:top=~/dev/scrabble
sudoku:src=~/dev/scrabble/src
sudoku:bin=~/dev/scrabble/bin
sudoku:data=~/dev/scrabble/data
and lists all the relevant projects (scrabble and sodoku in this case) and their directories (which may be different for each project, but have top, bin, src and data in this example).
并列出所有相关项目(在本例中为拼字游戏和数独)及其目录(每个项目可能不同,但在本例中包含 top、bin、src 和 data)。
The first action is to initialize stuff, so put:
第一个动作是初始化东西,所以把:
. ~/.cdx_init
at the end of your .bash_profile and create the "~/.cdx_init"file as:
在 .bash_profile 的末尾并将"~/.cdx_init"文件创建为:
alias cdxl='. ~/.cdx_list'
alias projl='. ~/.cdx_projlist'
alias cdx='. ~/.cdx_goto'
alias proj='. ~/.cdx_proj'
This sets up the four aliases to source the files which I'll include below. Usage is:
这将设置四个别名来获取我将在下面包含的文件。用法是:
cdxl - List all directories in current project.
projl - List all projects.
proj - Show current project.
proj <p> - Set current project to <p> (if allowed).
cdx - Show current project/directory and expected/actual real
directory, since they can get out of sync if you mix cd and cdx.
cdx . - Set actual real directory to expected directory (in other words,
get them back into sync).
cdx <d> - Set directory to <d> (if allowed).
The actual script follow. First, ".cdx_list"which just lists the allowed directories in the current project (pipelines are broken into multiple lines for readability but they should all be on one line).
实际脚本如下。首先,".cdx_list"它只列出当前项目中允许的目录(管道被分成多行以提高可读性,但它们都应该在一行上)。
echo "Possible directories are:"
cat ~/.cdx_data
| grep "^${CDX_PROJ}:"
| sed -e 's/^.*://' -e 's/=.*$//'
| sort -u
| sed 's/^/ /'
Similarly, ".cdx_projlist"shows all the possible projects:
同样,".cdx_projlist"显示所有可能的项目:
echo "Possible projects are:"
cat ~/.cdx_data
| grep ':'
| sed 's/:.*$//'
| sort -u
| sed 's/^/ /'
In the meaty scripts, ".cdx_proj"sets and/or shows the current project:
在大量脚本中,".cdx_proj"设置和/或显示当前项目:
if [[ "" != "" ]] ; then
grep "^:" ~/.cdx_data >/dev/null 2>&1
if [[ $? != 0 ]] ; then
echo "No project name ''."
projl
else
export CDX_PROJ=""
fi
fi
echo "Current project is: [${CDX_PROJ}]"
and ".cdx_goto"is the same for directories within the project:
而".cdx_goto"对于项目中的目录相同:
if [[ "" == "." ]] ; then
CDX_TMP="${CDX_DIR}"
else
CDX_TMP=""
fi
if [[ "${CDX_TMP}" != "" ]] ; then
grep "^${CDX_PROJ}:${CDX_TMP}=" ~/.cdx_data >/dev/null 2>&1
if [[ $? != 0 ]] ; then
echo "No directory name '${CDX_TMP}' for project '${CDX_PROJ}'."
cdxl
else
export CDX_DIR="${CDX_TMP}"
cd $(grep "^${CDX_PROJ}:${CDX_DIR}=" ~/.cdx_data
| sed 's/^.*=//'
| head -1
| sed "s:^~:$HOME:")
fi
fi
CDX_TMP=$(grep "^${CDX_PROJ}:${CDX_DIR}=" ~/.cdx_data
| sed 's/^.*=//'
| head -1
| sed "s:^~:$HOME:")
echo "Current project is: [${CDX_PROJ}]"
echo "Current directory is: [${CDX_DIR}]"
echo " [${CDX_TMP}]"
echo "Actual directory is: [${PWD}]"
unset CDX_TMP
It uses three environment variables which are reserved for its own use: "CDX_PROJ", "CDX_DIR"and "CDX_TMP". Other than those and the afore-mentioned files and aliases, there are no other resources used. It's the simplest, yet most adaptable solution I could come up with. Best of luck.
它使用三个保留供自己使用的环境变量:"CDX_PROJ","CDX_DIR"和"CDX_TMP"。除了这些和上述文件和别名之外,没有使用其他资源。这是我能想出的最简单但适应性最强的解决方案。祝你好运。
回答by Juliano
Revisiting. Today I received this link from a social bookmarking site, then I immediately remembered this question:
重温。今天我从一个社交书签网站收到这个链接,然后我立刻想起了这个问题:
We keep a simple, plain text bookmarks file and use a tool called bm to do the look-ups. The tool can also be used to edit the bookmark index dynamically as shown below where we add the directories from the previous example to the index.
我们保留一个简单的纯文本书签文件,并使用名为 bm 的工具进行查找。该工具还可用于动态编辑书签索引,如下所示,我们将上一个示例中的目录添加到索引中。
回答by Johannes Schaub - litb
Once i cd'ed into such a long directory, i have that in the history. Then i just type Ctrl-Rfor the "(reverse-i-search)" prompt and type in a few characters, like Dev/Cthat appear somewhere in the path, and it shows me the command what i issued back then and i can easily jump to it again.
一旦我 cd 进入这么长的目录,我就会在历史记录中找到它。然后我只需输入Ctrl-R“(reverse-i-search)”提示并输入几个字符,就像Dev/C出现在路径中的某个地方一样,它向我显示了我当时发出的命令,我可以轻松地再次跳转到它.
That works pretty well in practice. Because it won't find an entry if you haven't typed that path for quite some time, which would mean doing work to make things easier probably wouldn't be worth the time. But it definitely will find it if you used it recently. Which is exactly what i need.
这在实践中非常有效。因为如果您在很长一段时间内没有输入该路径,它就不会找到条目,这意味着为使事情变得更容易而做的工作可能不值得花时间。但是如果你最近使用它肯定会找到它。这正是我需要的。
In some way, it's a self-organizing cache for long commands & path-names :)
在某种程度上,它是长命令和路径名的自组织缓存:)
回答by wzzrd
回答by Léo Léopold Hertz ??
The user jhs suggested Pushd and Popd-commands. I share here some of my Bash-scripts that I found in Unix Power Tools -book. They are very cool when your directories get a way too long :)
用户 jhs 建议使用 Pushd 和 Popd 命令。我在这里分享我在 Unix Power Tools -book 中找到的一些 Bash 脚本。当您的目录太长时,它们非常酷:)
#Moving fast between directories
alias pd=pushd
alias pd2='pushd +2'
alias pd3='pushd +3'
alias pd4='pushd +4'
The command 'pushd +n' "rotates" the stack. The reverse command 'popd +n' deletes the n entry of the stack. If your stack gets too long, use 'repeat n popd'. For examle, your stack is 12 directories long:
命令“pushd +n”“旋转”堆栈。反向命令 'popd +n' 删除堆栈的第 n 个条目。如果您的堆栈太长,请使用“repeat n popd”。例如,您的堆栈有 12 个目录长:
repeat 11 popd
When you want to see your stack, write 'pushd'. For further reading, I recommend the book on pages 625-626.
当您想查看堆栈时,请编写“pushd”。为了进一步阅读,我推荐这本书第 625-626 页。
回答by Prabesh Subedi
In your .bashrc find
在你的 .bashrc 中找到
PS1='${debian_chroot:+($debian_chroot)}[3[01;32m]\u@\h[3[00m]:[3[01;34m]
\W[3[00m]$ '
并更换 \w\w与\W\W.我已经在这里改变了。这只会为您提供您正在工作的主目录。您可以通过键入来获取完整目录pwdpwd回答by Leonard
There are fundamental well-known ideas, like creating aliases:
有一些基本的众所周知的想法,比如创建别名:
alias cdfoo="cd /long/path/to/foo"
and also "dropping pebbles"
还有“扔鹅卵石”
export foo=/long/path/to/foo
and also making the above "project-based". I use 'ticket based' directories.
并且还使上述“基于项目”。我使用“基于票证”的目录。
topdir=ticket_12345
alias cdfoo="cd home/me/sandbox/$topdir/long/path/to/foo"
export foo="/home/me/sandbox/$topdir/long/path/to/foo"
but beyond all this, sometimes it's just handy to jump back and forth to where you've been recently, using command-line menus. (pushd and popd are cumbersome, IMHO).
但除此之外,有时使用命令行菜单来回跳转到您最近去过的地方也很方便。(pushd 和 popd 很麻烦,恕我直言)。
I use acd_func.sh (listed below). Once defined, you can do
我使用 acd_func.sh(如下所列)。一旦定义,你可以做
cd --
光盘——
to see a list of recent directories, with a numerical menu
查看最近的目录列表,带有数字菜单
cd -2
光盘 -2
to go to the second-most recent directory.
转到第二个最近的目录。
Very easy to use, very handy.
非常容易使用,非常方便。
Here's the code:
这是代码:
# Insert into .profile, .bash_profile or wherever
# acd_func 1.0.5, 10-nov-2004
# petar marinov, http:/geocities.com/h2428, this is public domain
cd_func ()
{
local x2 the_new_dir adir index
local -i cnt
if [[ == "--" ]]; then
dirs -v
return 0
fi
the_new_dir=
[[ -z ]] && the_new_dir=$HOME
if [[ ${the_new_dir:0:1} == '-' ]]; then
#
# Extract dir N from dirs
index=${the_new_dir:1}
[[ -z $index ]] && index=1
adir=$(dirs +$index)
[[ -z $adir ]] && return 1
the_new_dir=$adir
fi
#
# '~' has to be substituted by ${HOME}
[[ ${the_new_dir:0:1} == '~' ]] && the_new_dir="${HOME}${the_new_dir:1}"
#
# Now change to the new dir and add to the top of the stack
pushd "${the_new_dir}" > /dev/null
[[ $? -ne 0 ]] && return 1
the_new_dir=$(pwd)
#
# Trim down everything beyond 11th entry
popd -n +11 2>/dev/null 1>/dev/null
#
# Remove any other occurence of this dir, skipping the top of the stack
for ((cnt=1; cnt <= 10; cnt++)); do
x2=$(dirs +${cnt} 2>/dev/null)
[[ $? -ne 0 ]] && return 0
[[ ${x2:0:1} == '~' ]] && x2="${HOME}${x2:1}"
if [[ "${x2}" == "${the_new_dir}" ]]; then
popd -n +$cnt 2>/dev/null 1>/dev/null
cnt=cnt-1
fi
done
return 0
}
alias cd=cd_func
if [[ $BASH_VERSION > "2.05a" ]]; then
# ctrl+w shows the menu
bind -x "\"\C-w\":cd_func -- ;"
fi

