Click ▲ to follow “CU Technology Community” and pin the public account
More exciting content delivered to you first
This article is authorized to be reproduced from | Liangxu Linux
ID | liangxuxiansheng
☞ Essential resources for programmers advancing to architects are available for free ☜
When it comes to switching directories in Linux, everyone will surely think of one command: <span>cd</span>
. This is one of the most basic commands in Linux; if you don’t know this command, you might as well just give up.
The <span>cd</span>
command is indeed very convenient, but if you need to frequently switch between the following directories, you might start questioning your life choices:
/home/alvin/projects/blogdemos/linux-system-programming/thread
/home/alvin/projects/blogdemos/diff
/home/harry/study/日本文化/中日交流/影视业/动作片
If you only know the <span>cd</span>
command, you will have to keep using <span>cd</span>
until you go crazy.
In this case, how can we efficiently switch directories? Liangxu will introduce three commands to you: <span>pushd</span>
, <span>popd</span>
, and <span>dirs</span>
.
These three commands actually operate on the <span>directory stack</span>
, which is a stack structure that saves directories. The top of this stack always holds the current directory (take note, this is important!!).
Those with a programming background know that <span>stacks</span>
follow the <span>Last In First Out (LIFO)</span>
principle. This means that in a stack structure, the last element added will be the first one to be removed.
After reviewing the basic concepts, let’s take a closer look at these three commands.
Display Directory Stack Contents: dirs
First, we have <span>dirs</span>
. This command is very simple; it displays the contents of the directory stack. It has the following three common options:
[alvin@VM_0_16_centos dir2]$ pwd Among them, the difference between the <span>-p</span>
and <span>-v</span>
options is that the <span>-v</span>
option will display the index of each record in the stack, but otherwise, they are identical. Suppose we currently have a directory stack; let’s see what it contains:
/home/alvin/test/dir2
[alvin@VM_0_16_centos dir2]$ dirs -v
0 ~/test/dir2
1 ~/test/dir1
2 ~/test/dir3
3 ~/test
Note that the top element always corresponds to the current directory. If you check the directory stack from another directory, the first element will change accordingly. Similarly, if you use the <span>pushd</span>
and <span>popd</span>
commands to manipulate the directory stack, the current directory will switch to the address corresponding to the first element of the directory stack.
If we want to clear the directory stack, simply use the <span>-c</span>
option.
[alvin@VM_0_16_centos diff]$ dirs -c
[alvin@VM_0_16_centos diff]$ dirs -v
0 ~/projects/blogdemos/diff
Push to Directory Stack: pushd
Every time the <span>pushd</span>
command is executed, it will automatically execute a <span>dirs</span>
command to display the contents of the directory stack. The usage of <span>pushd</span>
mainly includes the following:
1. pushd + directory
If you directly follow <span>pushd</span>
with a directory, it will switch to that directory and place 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 ~/test
2. pushd (without any parameters)
Executing <span>pushd</span>
without any parameters will swap the top two directories in the directory stack. As we have emphasized before, the first element of the directory stack is related to the current directory, so when the first element changes, the current directory will switch accordingly, 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 # Current directory changes
[alvin@VM_0_16_centos dir2]$ dirs -v
0 ~/test/dir2
1 ~/test/dir3 # Contents of index 0 and 1 are swapped
2 ~/test/dir1
3 ~/test
3. pushd +/-n
Using <span>pushd +/-n</span>
allows you to directly switch to the directory corresponding to the index value. Note that both plus and minus signs can be used. If you use a plus sign, it counts from the top of the directory stack, while a minus sign counts from the bottom.
Now, back to the question we posed at the beginning of this article: how do we efficiently switch between two or more long directory paths?
First, we use pushd + directory to add these paths to the directory stack;
Then, we can quickly switch between different directories using pushd +/-n. The 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
Pop from Directory Stack: popd
Every time the <span>popd</span>
command is executed, it will automatically execute a <span>dirs</span>
command to display the contents of the directory stack. The usage of <span>popd</span>
mainly includes the following:
1. popd (without any parameters)
Executing <span>popd</span>
without any parameters will remove the top element from the directory stack. At this point, the top element changes, and naturally, the current directory will also switch 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
2. popd +/-n
This will remove the nth element from the directory stack. Similarly, the plus and minus signs indicate whether to count from the top or the bottom.
[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
Click “Read the original text” for an 8.8% discount on tickets~