How to switch directories efficiently in Linux

How to switch directories efficiently in Linux

When it comes to switching directories under Linux, everyone will definitely think of a command: the cd command. This is a very basic command under Linux. If you don’t know this command, you might as well commit suicide by cutting open your stomach.

The cd command is indeed very convenient, but if you need to frequently switch between the following directories, you may 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 and Television Industry/Action Films

If you only know the cd command, then you will need to cd over and over again until you go crazy.

In this case, how can we switch directories efficiently? Liang Xu would like to introduce three commands to you: pushd , popd , and dirs .

These three commands actually operate on目錄棧, which is a stack structure that stores目錄棧. The current directory is always stored at the top of the stack structure (pay attention, this is the key point!!).

Students with a basic knowledge of programming know thatfollow后進先出principle. That is to say, in the stack structure, the elements pushed into the stack later will be popped out first.

After reviewing the basic concepts, let's take a closer look at these three commands.

Display the contents of the directory stack: dirs

First, dirs . This command is very simple, it just displays the contents of the directory stack. It has the following three common options:

Options meaning
-p Display one record per line
-v Each line displays a record and shows the index of the record in the stack
-c Clear the directory stack

The difference between -p and -v options is that the -v option will display the index of each record in the stack, and other than that, they are exactly the same. Suppose there is a directory stack now, let's take a look at what's 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 topmost element is always the same as the current directory. If you view the directory stack in another directory, the first element will change accordingly. Similarly, if you use pushd and popd introduced later 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 clear the directory stack, just use the -c option.

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

Pushing onto the directory stack: pushd

Each time the pushd command is executed, a dirs command is executed by default to display the contents of the directory stack. There are several main uses for pushd:

1. pushd + directory

If pushd is used directly with a directory, it will switch to that directory and put it 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 two top directories on the directory stack. We have emphasized before 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 #The corresponding directory changes [alvin@VM_0_16_centos dir2]$ dirs -v
 0 ~/test/dir2
 1 ~/test/dir3 #Swap the contents of index 0 and 1 2 ~/test/dir1
 3 ~/test

**3. pushd +/-n **

pushd +/-n means directly switching to the directory of the corresponding index value. Note that you can use either a plus sign or a minus sign. If it is a plus sign, it will count from the top of the directory stack to the bottom, and if it is a minus sign, it will count from the bottom of the directory stack to the top.

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

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

Each time the popd command is executed, a dirs command is executed by default to display the contents of the directory stack. The usage of popd is mainly as follows:

1. popd (without any parameters)

The effect of executing popd 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 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

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

This concludes this article on how to efficiently switch directories under Linux. For more information on efficient directory switching in Linux, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to determine whether files and directories exist in Linux shell
  • How to extract file names and directory names from Linux shell
  • How to change the MySQL database directory location under Linux (CentOS) system
  • Find the running nginx directory in Linux system
  • Batch extract all directories and file names under a folder in Linux
  • Detailed explanation of how to find files filtered by time in a directory in Linux
  • How to enter directory/folder in Linux without using CD command
  • Linux directory switching implementation code example

<<:  Detailed explanation of JavaScript array deduplication

>>:  MySQL 8.0.21 installation tutorial under Windows system (illustration and text)

Recommend

Echarts legend component properties and source code

The legend component is a commonly used component...

Detailed explanation of new relational database features in MySQL 8.0

Preface The latest version of MySQL 8.0 is 8.0.4 ...

HTML set as homepage and add to favorites_Powernode Java Academy

How to implement the "Set as homepage" ...

Steps to install RocketMQ instance on Linux

1. Install JDK 1.1 Check whether the current virt...

List rendering instructions for efficient development of Vue front-end

v-for directive Speaking of lists, we have to men...

JavaScript function call classic example code

Table of contents JavaScript function call classi...

How to modify the firewall on a Linux server to allow remote access to the port

1. Problem Description For security reasons, the ...

MySQL permissions and database design case study

Permissions and database design User Management U...

960 Grid System Basic Principles and Usage

Of course, there are many people who hold the oppo...

Detailed explanation of how to use WeChat mini program map

This article example shares the specific implemen...

Complete step record of Vue encapsulation of general table components

Table of contents Preface Why do we need to encap...