How to switch directories efficiently under Linux?

For directory switching under Linux, everyone will definitely think of a command: cdcommand. This is the most basic command under Linux. If you don't know this command, you can do it quickly.

The cd command is indeed very 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/日本文化/中日交流/影视业/动作片

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

In this case, how do we efficiently switch directories? Liang promised to introduce three pushdcommands: popd, dirs, .

These three commands are actually to 目录栈operate, and 目录栈is a directory to save the stack structure, the top of the stack structure always kept the current directory (knock on the blackboard, the focus !!).

There are students know basics of programming, it is to follow the 后进先出principles. In other words, in the stack structure, the elements that are pushed into the stack later will be pushed out of the stack first.

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

Display the contents of the directory stack: dirs

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

Options meaning
-p Display one record per line
-v Display one record per line, and display the index of the record in the stack
-c Empty the directory stack

Among them, -pthe -vdifference between the options is that -vthe index each record in the stack options will be displayed, in addition to exactly the same. If there is a directory stack now, let's see what is in it:

[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

Please note that the top element is always consistent with the current directory. If you view the directory stack in another directory, the first element will change accordingly. Similarly, if you use the text presentation pushdand popdto operate the directory stack, then the current directory to the first switching element corresponding to the address of the directory stack.

If we want to clear the directory stack, the direct use -coption.

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

Push into the directory stack: pushd

After each pushd command is executed, a dirs command will be 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, it will switch to that directory and place the directory on 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)

The effect of executing pushd without any parameters is to swap the top two directories of the directory stack. We have already emphasized that 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    #对应目录发生改变
[alvin@VM_0_16_centos dir2]$ dirs -v
 0  ~/test/dir2
 1  ~/test/dir3        #索引 0 和 1 的内容对调
 2  ~/test/dir1
 3  ~/test

*3. pushd +/-n *

Pushd +/-n is to switch directly to the directory corresponding to the index value. Note that both the plus sign and the minus sign can be used here. If it is a plus sign, it will count from the top of the directory stack, and if it is a minus sign, it will count from the bottom of the directory stack.

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

First, we use pushd + directory to add these paths to the directory stack;

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 the directory stack: popd

After each popd command is executed, a dirs command will be executed by default to display the contents of the directory stack. The main usages of popd are as follows:

1. popd (without any parameters)

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

Delete the nth element in the directory stack. Similarly, the plus and minus signs indicate 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

Finally, recently many friends asked me for the Linux learning roadmap , so based on my experience, I spent a month staying up late in my spare time and compiled an e-book. Whether you are in an interview or self-improvement, I believe it will help you! The directory is as follows:

Give it to everyone for free, just ask you to give me a thumbs up!

Ebook | Linux development learning roadmap

I also hope that some friends can join me to make this e-book more perfect!

Gain? I hope the old irons will have a three-strike combo so that more people can read this article

Recommended reading:

Guess you like

Origin blog.csdn.net/yychuyu/article/details/108229198