Linux| |How to switch directories efficiently

How Linxu switches directories efficiently


Preface

For directory switching under Linux, you will definitely think of a command: cd command

The cd command is really convenient, but when you need to switch directories frequently, the cd command may be more troublesome.

such as:

/home/yk/Practice/DayTopic/5.WarCode
/home/yk/LinuxDir/LinuxCode/IO/epoll/new

When you need to switch between these two directories all the time, it is very troublesome every time.

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

So in this case, how do we efficiently switch directories? Next to introduce three pushdcommands: popd, dirs, .

These three commands actually operate on the directory stack , and the directory stack is a stack structure that stores directories. The top of the stack structure always stores the current directory (knocking on the blackboard, focus).

The stack follows the first-in -last-out principle. In other words, in the stack structure, the elements that are pushed into the stack later will be popped out first.

Let's introduce these three commands in detail:

Display the contents of the directory stack: dirs

The first is dirs. 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, -pand -vthe difference is that -voption will show an index for each record in the directory stack, in addition to exactly the same.

If there is a directory stack now, let's take a look at its contents:

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 pushdsum of the following text popdto manipulate the directory stack, the current directory will switch to the address corresponding to the first element of the directory stack.

Direct use The dirsdefault is to directly display the left and right contents of the stack, put on a line of output, and there is no index (index)

If you clear the directory stack, use the -coption directly .

It can be seen that dirs -cafter use , the contents of the directory stack will be emptied, but the current directory will be kept, which is the top directory of the directory stack.

Push into the directory stack: pushd

After each pushd command is executed, a dirscommand will be executed by default to display the contents of the directory stack. pushdThere are mainly the following usages:

1. pushd + directory

pushdIf it is directly used with a directory later, it will switch to that directory and place the directory on the top of the directory stack.

For example:

2. pushd (without any parameters)

The effect of executing pushd without any parameters is to swap the top two directories of the directory stack . As we have already 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.

3. pushd +/-n

pushd +/-nIt is to switch directly to the directory corresponding to the index value . Note that this can be either a plus sign or a minus sign. If it is a plus , it will count down from the directory stack from top , but with a minus sign , then the number from the directory stack from bottom to top .

Next, we will 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, how should we operate?

First of all, we use push + 目录the method to add these several paths to the directory stack;

Then, use push +/-n` to quickly switch between unused directories.

The specific demonstration is as follows:

Pop the directory stack: popd

After each popdcommand is executed, a dirscommand will be executed by default to display the contents of the directory stack. popdThe main usages are as follows:

1. popd (without parameters)

popdThe effect of execution without any parameters is to pop the top element in the directory stack . This is when the top element of the stack changes, and naturally the current directory will switch accordingly.

2.popd +/- n

Delete the nth element in the directory stack. Similarly, the plus and minus sign means counting from top to bottom or from bottom to top.

 

to sum up:

Three dirscommands: pushd, ,popd

dirs has three parameters:

  • -c clear all directories

  • -p displays the directory stack without index

  • -v display the directory stack with index

Pushd has three uses:

  • Used directly without parameters , a top element and the top element exchange time

  • Followed by directory , will indicate the directory push, push and default end once dirs command

  • +/-n , + sign means counting from top to bottom,-sign means counting from bottom to top, to switch the top element of the stack . Default counting starts from 0

There are two uses for popd:

  • Without parameters , the top element of the stack will be popped out of the stack . Since the top element of the stack is associated with the directory, the directory is switched

  • +/-n , +- sign means counting from top to bottom, or counting from bottom to top, to pop the directory out of the stack. Default counting starts from 0

Reference materials:

[1]: Liang Xu Linux

Guess you like

Origin blog.csdn.net/qq_40399012/article/details/89053183