本文是正在进行中的一部分 极客的生产力提示 series. 光盘 is 上e of the most frequently used command during a Unix session. In this article, I’ve provided 6 光盘命令 hacks, which will boost your productivity instantly 和 make it easier to navigate the directory structure from command line.
hack#1: 使用CDPATH to define the base directory for 光盘命令
If you are frequently 做ing 光盘 to subdirectories of a specific parent directory, you can set the CD路径 to the parent directory 和 perform 光盘 to the subdirectories without giving the parent directory path as explained below.
[[email protected] ~]# 密码 /home/ramesh [[email protected] ~]# 光盘 mail -bash: 光盘: mail: No such 科幻le or directory [Note: This is looking for mail directory under current directory] [[email protected] ~]# 导出CDPATH = / etc [[email protected] ~]# 光盘 mail [Note: This is looking for mail under /etc 和 not under current directory] [[email protected] /etc/mail]# 密码 /etc/mail
要使此更改永久生效,请添加 导出CDPATH = / etc 到你的〜/ .bash_profile
在以下情况下,此hack非常有用:
- 经常在$ ORACLE_HOME下工作的Oracle DBA可以将CDPATH变量设置为oracle home
- Unix系统管理员经常在/ etc下工作,可以将CDPATH变量设置为/ etc
- 经常在项目目录/ home / projects下工作的开发人员可以将CDPATH变量设置为/ home / projects
- 最终用户经常访问其主目录下的子目录,可以将CDPATH变量设置为〜(主目录)
技巧2: 使用cd alias to navigate up the directory effectively
当您浏览很长的目录结构时,您可能正在使用带有多个.. \的cd .. \ .. \’取决于要上移的目录数量,如下所示。
# mkdir -p / tmp/very/long/directory/structure/that/is/too/deep # 光盘 / tmp/very/long/directory/structure/that/is/too/deep # 密码 /tmp/very/long/directory/structure/that/is/too/deep # 光盘../../../../ # 密码 /tmp/very/long/directory/structure
代替执行cd ../../../ ..来向上浏览四个级别,请使用以下别名方法之一:
使用..n浏览目录: In the example below,..4 is used to go up 4 directory level, ..3 to go up 3 directory level, ..2 to go up 2 directory level. Add the following alias to the .bash_profile 和 re-login.
别名.. ="光盘.."
alias ..2="光盘../.."
alias ..3="光盘../../.."
alias ..4="光盘../../../.."
alias ..5="光盘../../../../.."
# 光盘 / tmp/very/long/directory/structure/that/is/too/deep
#..4
[Note: use ..4 to go up 4 directory level]
# 密码
/tmp/very/long/directory/structure/
仅使用点导航目录: In the example below,….. (five 做ts) is used to go up 4 directory level. Typing 5 做ts to go up 4 directory structure is really easy to remember, as when you type the 科幻rst two 做ts, you are thinking“going up 上e directory”, after that every additional 做t, is to go 上e level up. So, use…. (four 做ts) to go up 3 directory level 和 .. (two 做ts) to go up 1 directory level. Add the following alias to the .bash_profile 和 re-login for the….. (five 做ts) to work properly.
别名.. ="光盘.."
alias ...="光盘../.."
alias ....="光盘../../.."
alias .....="光盘../../../.."
alias ......="光盘../../../../.."
# 光盘 / tmp/very/long/directory/structure/that/is/too/deep
# .....
[Note: use ..... (five 做ts) to go up 4 directory level]
# 密码
/tmp/very/long/directory/structure/
使用cd导航目录,后跟连续的点: In the example below,cd….. (cd followed 通过 科幻ve 做ts) is used to go up 4 directory level. Making it 5 做ts to go up 4 directory structure is really easy to remember, as when you type the 科幻rst two 做ts, you are thinking“going up 上e directory”, after that every additional 做t, is to go 上e level up. So, usecd…. (cd followed 通过 four 做ts) to go up 3 directory level 和 光盘…(cd followed 通过 three 做ts) to go up 2 directory level. Add the following alias to the .bash_profile 和 re-login for the above 光盘….. (five 做ts) to work properly.
alias 光盘..="光盘.."
alias 光盘...="光盘../.."
alias 光盘....="光盘../../.."
alias 光盘.....="光盘../../../.."
alias 光盘......="光盘../../../../.."
# 光盘 / tmp/very/long/directory/structure/that/is/too/deep
# 光盘.....
[Note: use 光盘..... to go up 4 directory level]
# 密码
/tmp/very/long/directory/structure
技巧3: 使用单个命令执行mkdir和cd
有时,当您创建一个新目录时,您可能会立即CD到新目录中以执行某些工作,如下所示。
# mkdir -p / tmp/subdir1/subdir2/subdir3 # 光盘 / tmp/subdir1/subdir2/subdir3 # 密码 /tmp/subdir1/subdir2/subdir3
不会’在单个命令中同时组合mkdir和cd会很好吗?将以下内容添加到.bash_profile中,然后重新登录。
function mkdircd () { mkdir -p "[email protected]" && eval 光盘 "\"\$$#\""; }
现在,使用一个命令同时执行mkdir和cd,如下所示:
# mkdircd / tmp/subdir1/subdir2/subdir3
[Note: This creates the directory 和 光盘 to it automatically]
# 密码
/tmp/subdir1/subdir2/subdir3
技巧4: 使用“cd -”在最后两个目录之间切换
您可以使用cd在最后两个当前目录之间切换– as shown below.
# 光盘 / tmp/very/long/directory/structure/that/is/too/deep # 光盘 / tmp/subdir1/subdir2/subdir3 # 光盘- # 密码 /tmp/very/long/directory/structure/that/is/too/deep # 光盘- # 密码 /tmp/subdir1/subdir2/subdir3 # 光盘- # 密码 /tmp/very/long/directory/structure/that/is/too/deep
注意:您还可以使用历史记录中提到的example#12和#13将历史记录中其他命令的参数替换为cd命令。 命令行历史记录示例 文章。
技巧5: 使用dirs, 推 和 弹出 to manipulate directory stack
您可以使用目录堆栈将目录压入其中,然后再从堆栈中弹出目录。在此示例中,使用以下三个命令。
- 目录:显示目录堆栈
- 推入:将目录推入堆栈
- 弹出:从堆栈弹出目录,然后将其cd到
Dirs will always print the current directory followed 通过 the content of the stack. Even when the directory stack is empty, rs command will still print 上ly the current directory as shown below.
# 弹出 -bash: 弹出: directory stack empty # rs ~ # 密码 /home/ramesh
如何使用推入式和弹出式?让我们首先创建一些临时目录,然后将它们推入目录堆栈,如下所示。
# mkdir / tmp/dir1 # mkdir / tmp/dir2 # mkdir / tmp/dir3 # mkdir / tmp/dir4 # 光盘 / tmp/dir1 # 推 . # 光盘 / tmp/dir2 # 推 . # 光盘 / tmp/dir3 # 推 . # 光盘 / tmp/dir4 # 推 . # rs /tmp/dir4 / tmp/dir4 / tmp/dir3 / tmp/dir2 / tmp/dir1 [Note: 的first directory (/tmp/dir4) of the dir command output is always the current directory 和 not the content from the stack.]
在此阶段,目录堆栈包含以下目录:
/ tmp/dir4 /tmp/dir3 /tmp/dir2 /tmp/dir1
的last directory that was pushed to the stack will be at the top. When you perform 弹出, it will 光盘 to the top directory entry in the stack 和 remove it from the stack. As shown above, the last directory that was pushed into the stack is / tmp/dir4. So, when we 做 a 弹出, it will 光盘 to the / tmp/dir4 和 remove it from the directory stack as shown below.
# 弹出 # 密码 /tmp/dir4 [Note: After the above 弹出, directory Stack Contains: /tmp/dir3 /tmp/dir2 /tmp/dir1] # 弹出 # 密码 /tmp/dir3 [Note: After the above 弹出, directory Stack Contains: /tmp/dir2 /tmp/dir1] # 弹出 # 密码 /tmp/dir2 [Note: After the above 弹出, directory Stack Contains: / tmp/dir1] # 弹出 # 密码 /tmp/dir1 [Note: After the above 弹出, directory Stack is empty!] # 弹出 -bash: 弹出: directory stack empty
技巧6: 使用“shopt -s 光盘spell”自动更正cd上错误键入的目录名称
使用shopt -s 光盘spell 如下所示自动纠正cd命令中的错别字。如果您不擅长键入并犯了很多错误,这将非常有帮助。
# 光盘 /etc/mall
-bash: 光盘: / etc / mall:没有这样的文件或目录
# shopt -s 光盘spell
# 光盘 /etc/mall
# 密码
/etc/mail
[Note: By mistake, when I typed mall instead of mail,
光盘 corrected it automatically]
如果您喜欢这篇文章,请在其上添加书签 美味的, 绊倒它.
如果您喜欢这篇文章,您可能还会喜欢..
![]() |
![]() |
![]() |
![]() |
优秀的技巧。我已经成为您博客的忠实拥护者。非常感谢所有的好帖子。