For directory switching under Linux, you will definitely think of a command: CD command. This is a basic Linux command, if you don’t know this command, quickly disembowelling yourself to go.
The CD command is certainly handy, but if you need to switch between the following directories frequently, you may have to doubt your life:
/home/alvin/projects/blogdemos/linux-system-programming/thread /home/alvin/projects/blogdemos/diff /home/harry/study/ Japanese culture/Sino-Japanese exchange/Film industry/Action moviesCopy the code
If you only know the CD command, then you need to keep CD until you go crazy.
In this case, how can we efficiently switch directories? Ryoshi introduces three commands: pushd, popd, dirs.
All three of these commands operate on the directory stack, which is a structure that holds the current directory at the top of the stack. .
Those of you with programming background know that stacks follow the last in, first out principle. In other words, in the stack structure, the last element on the stack will be first off the stack.
After reviewing the basic concepts, let’s go over these three commands in detail.
Display the contents of directory stack: dirs
The first is dirs. This command is as simple as displaying the contents of the directory stack. It has the following three common options:
options | meaning |
---|---|
-p | Each row displays one record |
-v | Each row displays one record, along with the index of the record in the stack |
-c | Clearing the directory stack |
The difference between the -p option and the -v option is that the -v option displays the index of each record on the stack, otherwise exactly the same. If we have a directory stack, let’s see what it contains:
[alvin@VM_0_16_centos dir2]$ pwd
/home/alvin/test/dir2
[alvin@VM_0_16_centos dir2]$ dirs -v
0 ~/test/dir2
1 ~/test/dir1
2 ~/test/dir3
3 ~/test
Copy the code
Note that the top element is always the same as the current directory, and if you look at the directory stack in another directory, the first element will change accordingly. Similarly, if you use pushd and popd to manipulate the directory stack, the current directory will be switched to the address of the first element in the directory stack.
If we want to clear the directory stack, we simply use the -c option.
[alvin@VM_0_16_centos diff]$ dirs -c
[alvin@VM_0_16_centos diff]$ dirs -v
0 ~/projects/blogdemos/diff
Copy the code
Push the directory stack: pushd
By default, every time the pushd command is executed, a dirs command is executed to display the contents of the directory stack. Pushd can be used in the following ways:
1. Pushd + directory
Pushd, if used directly with a directory, switches to that directory and puts it at the top of the directory stack. Example:
[alvin@VM_0_16_centos test]$ pushd dir1 ~/test/dir1 ~/test [alvin@VM_0_16_centos dir1]$ pushd .. /dir2 ~/test/dir2 ~/test/dir1 ~/test [alvin@VM_0_16_centos dir2]$ pushd .. /dir3 ~/test/dir3 ~/test/dir2 ~/test/dir1 ~/test [alvin@VM_0_16_centos dir3]$ dirs -v 0 ~/test/dir3 1 ~/test/dir2 2 ~/test/dir1 3 ~/testCopy the code
2. Pushd (without any parameters)
The effect of pushd with no arguments is to swap the top two directories on the directory stack. As we emphasized earlier, the first element of the directory stack is related to the current directory, so when the first element changes, the current directory will be switched, and vice versa.
[alvin@VM_0_16_centos dir3]$ dirs -v 0 ~/test/dir3 1 ~/test/dir2 2 ~/test/dir1 3 ~/test [alvin@VM_0_16_centos dir3]$ pwd /home/alvin/test/dir3 [alvin@VM_0_16_centos dir3]$ pushd ~/test/dir2 ~/test/dir3 ~/test/dir1 ~/test [alvin@VM_0_16_centos dir2]$PWD /home/alvin/test/dir2 # The corresponding directory is changed [alvin@VM_0_16_centos dir2]$dirs -v 0 ~/test/dir2 1 2 ~/test/dir1 3 ~/testCopy the code
3. pushd +/-n
Pushd +/-n is the directory directly switched to the corresponding index value. Notice, you can use either a plus or a minus sign here. A plus sign will count from the top of the directory stack, while a minus sign will count from the bottom of the directory stack.
This brings us back to our question at the beginning of this article, what if we want to switch frequently between two or more directories with very long paths?
First, we add these paths to the directory stack as a pushd + directory;
Then, use pushd +/-n to quickly switch between different directories. Specific demonstration is as follows:
[alvin@VM_0_16_centos dir2]$ pwd
/home/alvin/test/dir2
[alvin@VM_0_16_centos dir2]$ dirs -v
0 ~/test/dir2
1 ~/test/dir3
2 ~/test/dir1
3 ~/test
[alvin@VM_0_16_centos dir2]$ pushd +2
~/test/dir1 ~/test ~/test/dir2 ~/test/dir3
[alvin@VM_0_16_centos dir1]$ pwd
/home/alvin/test/dir1
[alvin@VM_0_16_centos dir1]$ dirs -v
0 ~/test/dir1
1 ~/test
2 ~/test/dir2
3 ~/test/dir3
Copy the code
Pop directory stack: POPD
By default, after each popd command is executed, a dirs command is executed to display the contents of the directory stack. Popd can be used in the following ways:
1. Popd (with no arguments)
Popd takes no arguments and pushes the top element of the directory stack off the stack. At this point, the top of the stack changes, and the current directory changes accordingly.
[alvin@VM_0_16_centos dir3]$ dirs -v
0 ~/test/dir3
1 ~/test/dir1
2 ~/test
3 ~/test/dir2
[alvin@VM_0_16_centos dir3]$ popd
~/test/dir1 ~/test ~/test/dir2
[alvin@VM_0_16_centos dir1]$ dirs -v
0 ~/test/dir1
1 ~/test
2 ~/test/dir2
Copy the code
2. popd +/-n
Removes the NTH element in the directory stack. Again, plus or minus means counting from the top down, or counting from the bottom up.
[alvin@VM_0_16_centos dir1]$ dirs -v
0 ~/test/dir1
1 ~/test
2 ~/test/dir2
[alvin@VM_0_16_centos dir1]$ popd +1
~/test/dir1 ~/test/dir2
[alvin@VM_0_16_centos dir1]$ dirs -v
0 ~/test/dir1
1 ~/test/dir2
Copy the code