How to Switch Directory Efficiently under Linux

  • 2021-08-17 01:51:22
  • OfStack

For directory switching under Linux, everyone will definitely think of a command: cd Orders. This is the basic command under Linux. If you don't know this command, you should commit suicide by Caesarean section.

The cd command is really convenient, but if you need to switch frequently in the following directories, 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 exchanges / Film and TV industry / Action film 

If you can only command cd, then you need to keep cd until you go crazy.

In this case, how can we switch directories efficiently? Liang Xu introduced three commands to everyone: pushd , popd , dirs .

These three orders are actually right 目录栈 Operate, and 目录栈 Is a store directory stack structure, the top of the stack structure will always store the current directory (knock on the blackboard, focus!!) .

Students with basic programming know that, They all follow 后进先出 The principle. That is to say, in the stack structure, the elements that enter the stack later will leave the stack first.

After reviewing the basic concepts, let's detail these three commands.

Display directory stack contents: dirs

The first is dirs . This command is very simple, that is, to display the contents of the directory stack. It has the following three common options:

选项 含义
-p 每行显示1条记录
-v 每行显示1条记录,同时展示该记录在栈中的index
-c 清空目录栈

Among them, -p And pushd0 Option is that, pushd0 Option will display the index of each record in the stack, except that it is completely 1. If there is a directory stack now, 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

Note that the topmost element is always the same as the current directory 1. If you look at the directory stack in another directory, the first element will change accordingly. By the same token, if you use the following pushd And popd To operate the directory stack, the current directory will be switched to the address corresponding to the first element of the directory stack.

If we want to empty the directory stack, use it directly -c Option will do.


[alvin@VM_0_16_centos diff]$ dirs -c
[alvin@VM_0_16_centos diff]$ dirs -v
 0 ~/projects/blogdemos/diff

Push Directory Stack: pushd

Each time the pushd command is executed, one dirs command is executed by default to display the contents of the directory stack. The main uses of pushd are as follows:

1. pushd + Directory

If pushd is used directly with a directory later, it switches to that directory and places it at the top of the directory stack. Examples:


[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)

The effect of pushd execution without any parameters is to swap the two directories at the top of 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 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	# The corresponding directory has changed 
[alvin@VM_0_16_centos dir2]$ dirs -v
 0 ~/test/dir2
 1 ~/test/dir3		# Index  0  And  1  Content exchange of 
 2 ~/test/dir1
 3 ~/test

**3. pushd +/-n **

pushd +/-n is to switch directly to the directory of the corresponding index value. Note that you can use either a plus sign or a minus sign here. If it is a plus sign, it will be counted from top to bottom of the directory stack, while if it is a minus sign, it will be counted from bottom to top of the directory stack.

Next, we return to the question at the beginning of this article. What should we do if we want to switch frequently between two or more directories with long paths?

First, we add these paths to the directory stack in the way of pushd + directory;

Then, use pushd +/-n to quickly switch between different directories. 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-up directory stack: popd

Each time the popd command completes execution, one dirs command is executed by default to display the contents of the directory stack. The main uses of popd are as follows:

1. popd (without any parameters)

The effect of popd execution without any parameters is to remove the top element from the directory stack. At this time, the top element of the stack changes, and naturally the current directory will be switched 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

Delete the n element in the directory stack. Similarly, the addition and subtraction sign indicates whether to count from top to bottom or from bottom to top.


[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

Related articles: