Chapter 1: Powerful CD Command Hacks__Linux 101 Hacks

本文介绍Linux中使用cd命令的多种高效技巧,包括通过设置CDPATH变量快速切换目录、定义别名简化上层目录导航、组合mkdir与cd命令一步到位创建并进入新目录等。这些技巧有助于提高日常开发效率。

Chapter 1: Powerful CD Command Hacks

            __Linux 101 Hacks

Hack 1. Use CDPATH to define the base directory forcd command

 

[ramesh@dev-db ~]# cd mail
~bash: cd: mail: No such file or directory
[ramesh@dev
-db ~]# export CDPATH=/etc
[ramesh@dev-db ~]# cd mail
/etc/mail
[ramesh@dev
-db ~]# pwd
/etc/mail

 

To make this change permanent, add export CDPATH=/etc to your ~/.bash_profile

 

Similar to the PATH variable, you can add more than one directory entry inthe CDPATH variable, separating them with : , as shown below.

export CDPATH=.:~:/etc:/var

 

Hack 2. Use cd alias to navigate up the directory effectively

    Method 1: Navigate up the directory using “..n”

    In the example below, ..4 is used to go up 4 directory level.

    Add the following alias to your~/.bash_profile and re-login.

    ContractedBlock.gifExpandedBlockStart.gif代码
alias ..="cd .."
alias ..
2="cd ../.."
alias ..
3="cd http://www.cnblogs.com/.."
alias ..
4="cd http://www.cnblogs.com/../.."
alias ..
5="cd http://www.cnblogs.com/http://www.cnblogs.com/.."

# cd
/tmp/very/long/directory/structure/that/is/too/long

# ..4
#
pwd
/tmp/very/long/directory/structure/

 

    Method 2: Navigate up the directory using only dots

    In the example below, ..... (five dots) is used to go up 4 directory level.    

    Add thefollowing alias to your ~/.bash_profile and re-login for the ..... (five dots) towork properly.

 

ContractedBlock.gifExpandedBlockStart.gif代码
alias ..="cd .."
alias ...
="cd ../.."
alias ....
="cd http://www.cnblogs.com/.."
alias .....
="cd http://www.cnblogs.com/../.."
alias ......
="cd http://www.cnblogs.com/http://www.cnblogs.com/.."

# cd /tmp/very/long/directory/structure/that/is/too/deep
#
.....
#
pwd
/tmp/very/long/directory/structure/

 

 

    Method 3: Navigate up the directory using cd followed byconsecutive dots

    In the example below, cd..... (cd followed by five dots) is used to go up 4directory level.

    Add the following alias toyour ~/.bash_profile and re-login for the above cd..... (five dots) to workproperly.

 

ContractedBlock.gifExpandedBlockStart.gif代码
alias cd..="cd .."
alias cd...
="cd ../.."
alias cd....
="cd http://www.cnblogs.com/.."
alias cd.....
="cd http://www.cnblogs.com/../.."
alias cd......
="cd http://www.cnblogs.com/http://www.cnblogs.com/.."

# cd /tmp/very/long/directory/structure/that/is/too/deep
#
cd.....
#
pwd
/tmp/very/long/directory/structure

 

 

    Method 4: Navigate up the directory using cd followed by number

    In the example below, cd4 (cd followed by number 4) is used to go up 4directory level.

 

ContractedBlock.gifExpandedBlockStart.gif代码
alias cd1="cd .."
alias cd2
="cd ../.."
alias cd3
="cd http://www.cnblogs.com/.."
alias cd4
="cd http://www.cnblogs.com/../.."
alias cd5
="cd http://www.cnblogs.com/http://www.cnblogs.com/.."

# cd /tmp/very/long/directory/structure/that/is/too/deep
#
cd4
#
pwd
/tmp/very/long/directoy/structure

 

 

Hack 3. Perform mkdir and cd using a singlecommand

Be nice to combine both mkdir and cd in a single command.

Add the following to the .bash_profile and re-login.

 

ContractedBlock.gifExpandedBlockStart.gif代码
# mkdir -p /tmp/subdir1/subdir2/subdir3
#
cd /tmp/subdir1/subdir2/subdir3
#
pwd
/tmp/subdir1/subdir2/subdir3

$ vi .bash_profile
function mkdircd() { mkdir
-p "$@" && eval cd "\"\$$#\""; }

# mkdircd /tmp/subdir1/subdir2/subdir3
#
pwd
/tmp/subdir1/subdir2/ssubdir3ubdir3

 

 

Hack 4. Use “cd -” to toggle between the last twodirectories

You can toggle between the last two current directories using cd - as shown below.

 

ContractedBlock.gifExpandedBlockStart.gif代码
# cd /tmp/very/long/directory/structure/that/is/too/deep
#
cd /tmp/subdir1/subdir2/subdir3

# cd -
#
pwd
/tmp/very/long/directory/structure/that/is/too/deep

# cd -
#
pwd
/tmp/subdir1/subdir2/subdir3

# cd -
#
pwd
/tmp/very/long/directory/structure/that/is/too/deep

 



 

Hack 5. Use dirs, pushd and popd to manipulatedirectory stack

You can use directory stack to push directories into it and later pop directoryfrom the stack. Following three commands are used in this example.

    dirs: Display the directory stack

    pushd: Push directory into the stack

    popd: Pop directory from the stack and cd to it

dirs will always print the current directory followed by the content of thestack. Even when the directory stack is empty.

The last directory that was pushed to the stack will be at the top. When you perform popd, it will cd to the top directory entry in the stack and remove it from the stack. As shown above, the last directory that was pushed into the stack is /tmp/dir4. So, when we do a popd, it will cd to the /tmp/dir4 and remove it from the directory stack as shown below.

 

ContractedBlock.gifExpandedBlockStart.gif代码
# popd
~bash: popd: directory stack empty
# dirs
~
#pwd
/home/ramesh

# ------------------

# mkdir /tmp/dir1
#
mkdir /tmp/dir2
#
mkdir /tmp/dir3
#
mkdir /tmp/dir4

# cd /tmp/dir1
#
pushd .

# cd /tmp/dir2
#
pushd .

# cd /tmp/dir3
#
pushd .

# cd /tmp/dir4
#
pushd .

# dirs
/tmp/dir4 /tmp/dir4 /tmp/dir3 /tmp/dir2 /tmp/dir1

# popd
#
pwd
/tmp/dir4

# popd
#
pwd
/tmp/dir3

# popd
#
pwd
/tmp/dir2

#popd
#
pwd
/tmp/dir1

# popd
~bash: popd: directory stack empty

 

 

Hack 6. Use “shopt -s cdspell” to automaticallycorrect mistyped directory names on cd

If you are not good at typing and make lot of mistakes, this willbe very helpful.

# cd /etc/mall
~bash: cd: /etc/mall: No such file or directory

# shopt -s cdspell

# pwd
/etc/mail

 

 

转载于:https://www.cnblogs.com/way_testlife/archive/2010/09/12/1824483.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值