First, why do you use these commands? You
may have questions, why do you use these commands
? Can you switch directories with cd?
Yes, you can use cd to switch to the directory that needs to be accessed,
but sometimes it is a directory with a long path and many levels. After entering this directory, we accidentally run the cd command.
Of course, we are back. My home directory, what if I want to go back?
Also: because of the needs of work, we need to keep switching between several deep directories, more than one,
then even with the help of the tab key, we will spend a lot of time because of a cd command, how to switch between multiple directories It can be more convenient when switching between
pushd, popd is our good helper
Description: pushd, popd, dirs, the commands we talked about are built-in commands in bash,
so as long as you log in to bash, these commands can be used
Second, how to switch between the two directories?
If you just switch between two directories, then you don't have to use pushd, you
just need to use cd - and that's it
. The function of cd - is to go back to the previous directory.
See the example:
[root@localhost ~]# cd /usr/share/kde4/apps/kget/pics/ [root@localhost pics]# cd - /root [root@localhost ~]# cd - /usr/share/kde4/apps/kget/pics [root@localhost pics]#
how about it? It is very convenient to switch between the two directories, no longer need to enter a long path
Explanation:
Why is cd - able to return to the previous directory?
This is because - is equivalent to the $OLDPWD variable here. For
this problem, please refer to the bash info information. The
cd item has a related description: An argument of - is equivalent to $ OLDPWD.
The $OLDPWD variable is the previous directory recorded by bash.
That is: $OLDPWD and - are equal.
Let's look at another example:
[root@localhost ~]# cd /usr/share/kde4/apps/kget/pics/ [root@localhost pics]# echo $OLDPWD; /root [root@localhost pics]# cd $OLDPWD [root@localhost ~]# echo $OLDPWD; /usr/share/kde4/apps/kget/pics [root@localhost ~]# cd $OLDPWD; [root@localhost pics]# echo $OLDPWD; /root
We can get the structure: both cd - and cd $OLDPWD can switch between the two most recent directories
Third, how to switch between multiple directories?
Because cd - and cd $OLDPWD are two directories that only support recent operations,
we need to use pushd to operate between multiple directories
1, first introduce the 3 commands we will use
pushd: switch to the directory as a parameter, and push the original directory and the current directory into a virtual stack.
If no parameter is specified, it will return to the previous directory and exchange the two most recent directories in the stack
popd: Pop the most recent directory
in the stack dirs: List the directories saved in the current stack
See example:
[root@localhost ~]# pushd /usr/local/sbin/ /usr/local/sbin ~ [root@localhost sbin]# dirs /usr/local/sbin ~ [root@localhost sbin]# dirs -p -v 0 /usr/local/sbin 1 ~ [root@localhost sbin]# pushd /usr/share/kde4/apps/kget/ /usr/share/kde4/apps/kget /usr/local/sbin ~ [root@localhost kget]# dirs -p -v 0 /usr/share/kde4/apps/kget 1 /usr/local/sbin 2 ~
Description: The -p parameter of dirs can display the list of directories in the stack in the form of one directory per line
-v parameter can add a number before the directory
Note: When -v is present, it can also be displayed in the form of one directory per line without adding -p
Explanation 2: We can see that the directory that was recently pushed onto the stack is at the top
2, How to switch between the two most recent directories?
Switch between the two most recent directories: use pushd without parameters
[root@localhost kget]# pushd /boot/grub/ /boot/grub /usr/share/kde4/apps/kget /usr/local/sbin ~ [root@localhost grub]# dirs -v 0 /boot/grub 1 /usr/share/kde4/apps/kget 2 /usr/local/sbin 3 ~ [root@localhost grub]# pushd /usr/share/kde4/apps/kget /boot/grub /usr/local/sbin ~ [root@localhost kget]# dirs -v 0 /usr/share/kde4/apps/kget 1 /boot/grub 2 /usr/local/sbin 3 ~ [root@localhost kget]# pushd /boot/grub /usr/share/kde4/apps/kget /usr/local/sbin ~ [root@localhost grub]# dirs -v 0 /boot/grub 1 /usr/share/kde4/apps/kget 2 /usr/local/sbin 3 ~
Description: As you can see, when using pushd without parameters to switch between the two most recent directories,
the current directory is always at the top of the stack
3, How to switch between multiple directories?
Use pushd +n to
explain:
n is a number, when there is this parameter, it is to switch to the nth directory in the stack, and push this directory to the top of the stack in a stack loop.
Note: The stack starts from the 0th start counting
See example:
[root@localhost grub]# dirs -v 0 /boot/grub 1 /usr/share/kde4/apps/kget 2 /usr/local/sbin 3 ~ [root@localhost grub]# pushd +2 /usr/local/sbin ~ /boot/grub /usr/share/kde4/apps/kget [root@localhost sbin]# dirs -v 0 /usr/local/sbin 1 ~ 2 /boot/grub 3 /usr/share/kde4/apps/kget
4, How to delete the directory from the stack?
Just use popd
See example:
[root@localhost sbin]# dirs -v 0 /usr/local/sbin 1 ~ 2 /boot/grub 3 /usr/share/kde4/apps/kget [root@localhost sbin]# popd ~ /boot/grub /usr/share/kde4/apps/kget [root@localhost ~]# dirs -v 0 ~ 1 /boot/grub 2 /usr/share/kde4/apps/kget [root@localhost ~]# popd +1 ~ /usr/share/kde4/apps/kget [root@localhost ~]# dirs -v 0 ~ 1 /usr/share/kde4/apps/kget
Description: You can see the operation of popd without parameters: popd deletes the directory at the top of the
stack from the stack and switches to the directory at the new top The nth directory, which means to delete the nth directory in the stack from the stack
Fourth, learn a little more
1. Both pushd and popd can only affect the stack without switching directories.
Use the -n parameter to
see an example:
[root@localhost ~]# dirs -v 0 ~ 1 /usr/share/kde4/apps/kget [root@localhost ~]# pushd -n /boot/grub ~ /boot/grub /usr/share/kde4/apps/kget [root@localhost ~]# dirs -v 0 ~ 1 /boot/grub 2 /usr/share/kde4/apps/kget
2, dirs can clear the directory stack
with the -c parameter
See example:
[root@localhost ~]# dirs -v 0 ~ 1 /boot/grub 2 /usr/share/kde4/apps/kget [root@localhost ~]# dirs -c [root@localhost ~]# dirs -v 0 ~
Explanation: The directory at the top of the stack is the current directory, it cannot be popped
Original text: http://www.cnblogs.com/zhengyuxin/articles/1933920.html
From: Linux: Make it easier to switch directories: pushd,popd,dirs,cd -