1.1 Moving Quickly Among Arbitrary Directories
Use the pushd and popd built-in commands to manage a stack of directory locations, and to switch between them easily.
[root@MaxwellDBA ~]# mkdir /var/log/cups
[root@MaxwellDBA ~]# pwd
/root
[root@MaxwellDBA ~]# cd /tmp/tank
[root@MaxwellDBA tank]# pwd
/tmp/tank
[root@MaxwellDBA tank]# pushd /var/log/cups
/var/log/cups /tmp/tank
[root@MaxwellDBA cups]# pwd
/var/log/cups
[root@MaxwellDBA cups]# touch access_log error_log page_log
[root@MaxwellDBA cups]# ls
access_log error_log page_log
[root@MaxwellDBA cups]# popd
/tmp/tank
[root@MaxwellDBA tank]# ls
[root@MaxwellDBA tank]# ls
[root@MaxwellDBA tank]# touch empty full
[root@MaxwellDBA tank]# ls
empty full
[root@MaxwellDBA tank]# pushd /var/log/cups
/var/log/cups /tmp/tank
[root@MaxwellDBA cups]# pushd
/tmp/tank /var/log/cups
[root@MaxwellDBA tank]# pushd
/var/log/cups /tmp/tank
[root@MaxwellDBA cups]# pushd
/tmp/tank /var/log/cups
[root@MaxwellDBA tank]# dirs
/tmp/tank /var/log/cups
[root@MaxwellDBA tank]#
1.2 Repeating the Last Command
The other (more modern) solution involves using the arrow keys. Typing the uparrow key will scroll back through the previous commands that you have issued. When you find the one you want, just press the Enter key and that command will be run (again).
[maxwell@MaxwellDBA Day0801]$ /usr/bin/somewhere/someprog -g -H -yknot -w /tmp/soforthandsoon
-bash: /usr/bin/somewhere/someprog: No such file or directory
[maxwell@MaxwellDBA Day0801]$ !!
/usr/bin/somewhere/someprog -g -H -yknot -w /tmp/soforthandsoon
-bash: /usr/bin/somewhere/someprog: No such file or directory
[maxwell@MaxwellDBA Day0801]$
1.3 Running Almost the Same Command\
If you’re going to use this feature, just be careful with your substitutions. If you had tried to change the -g option by typing !!:s/g/h/ you would have ended up changing Substituting Across Word Boundaries | 457 the first letter g, which is at the end of the command name, and you would be trying to run /usr/bin/somewhere/someproh.
[maxwell@MaxwellDBA Day0801]$ /usr/bin/somewhere/someprog -g -H -yknot -w /tmp/soforthandsoon
-bash: /usr/bin/somewhere/someprog: No such file or directory
[maxwell@MaxwellDBA Day0801]$ !!:s/H/A/
/usr/bin/somewhere/someprog -g -A -yknot -w /tmp/soforthandsoon
-bash: /usr/bin/somewhere/someprog: No such file or directory
[maxwell@MaxwellDBA Day0801]$
1.4 Substituting Across Word Boundaries
Use the caret (^) substitution mechanism:
[maxwell@MaxwellDBA Day0801]$ /usr/bin/somewhere/someprog -g -A -yknot -w /tmp/soforthandsoon
-bash: /usr/bin/somewhere/someprog: No such file or directory
[maxwell@MaxwellDBA Day0801]$ ^-g -A^-gB^
/usr/bin/somewhere/someprog -gB -yknot -w /tmp/soforthandsoon
-bash: /usr/bin/somewhere/someprog: No such file or directory
[maxwell@MaxwellDBA Day0801]$
本文介绍了Linux中使用pushd和popd命令在目录间快速切换的方法,以及如何利用历史记录重复执行或修改最近的命令。通过箭头键可以方便地浏览并重新运行之前的命令,而!!和^符号则用于替换命令选项或参数,提高了命令行操作的效率。
1312

被折叠的 条评论
为什么被折叠?



