Tips on using command line tools in Linux systems (2)
In the first part of this series of articles, we focused on command line navigation in Linux and discussed some related points/concepts by exploring the use of the cd – command. Now, we'll take a closer look at how to use the pushd and popd commands for a faster navigation experience in the Linux command line.

Before we begin, it’s worth stating that all instructions and commands mentioned hereafter have been tested on Ubuntu 14.04 and the Bash shell (4.3.11).
pushd and popd command basics
To better understand the role of pushd and popd commands, let us first discuss the concept of stack. Imagine you have an empty space on your kitchen countertop and you want to place a set of dishes on it. What do you do? Simple, put them on top one after the other.
So at the end of the whole process, the first plate on the chopping board is the last plate in the pile, and the last plate in your hand is the first plate in the pile. Now when you need a plate, you select the one at the top of the pile and use it, then select the next one when needed.
pushd and popd commands are similar concepts. On Linux systems there is a directory stack where you can stack directory paths for future use. You can use the dirs command to quickly view the contents of the stack at any point in time.
The following example shows the output of using the dirs command on my system immediately after starting the command line terminal:
$ dirs ~
The tilde (~) in the output indicates that the directory stack currently contains only the user's home directory.
Go ahead and use the pushd and popd commands to store the directory path and delete it. Using pushd is very easy - just pass the path to be stored in the directory stack as an argument to this command. Here is an example:
pushd /home/himanshu/Downloads/
The above command changes the current working directory to the directory you pass as a parameter, and also adds the path to the directory stack. For user convenience, the pushd command produces the contents of the directory stack in its output. Therefore, when running the above command, the following output is produced:
~/Downloads ~
The output shows that there are now two directory paths in the stack: one is the user's home directory, and the user's download directory. They are saved in the order that the main directory is at the bottom and the newly added Downloads directory is above it.
To verify that the output of pushd is correct, you can also use the dirs command:
$ dirs ~/Downloads ~
So you can see that the dirs command also produces the same output.
Let’s use the pushd command again:
$ pushd /usr/lib/; pushd /home/himanshu/Desktop/
/usr/lib ~/Downloads ~
~/Desktop /usr/lib ~/Downloads ~
So the directory stack now contains a total of four directory paths, with the home directory (~) at the bottom and the user's desktop directory at the top.
Be sure to remember that the head of the stack is your current directory. This means our current working directory is now ~/Desktop.
Now, let's say you want to get back to the /usr/lib directory, so all you have to do is execute the popd command:
$ popd /usr/lib ~/Downloads ~
The popd command not only changes the current directory to /usr/lib, it also removes ~/Desktop from the directory stack, as you can see from the command output. This way, the popd command will allow you to browse the directories in reverse order.
Some advanced usage
Now that we have discussed the basics of pushd and popd commands, let us move on to discuss some other details related to these commands. First, these commands also allow you to manipulate directory stacks. For example, let's say your directory stack looks like this:
$ dirs ~/Desktop /usr/lib ~ ~/Downloads
Now, our requirement is to change the order of directory paths in the stack, with the topmost element (~/Desktop) being moved to the bottom, and each of the remaining elements being moved up one position. This can be achieved using the following command:
pushd +1
The result of the above command on the directory stack:
$ dirs /usr/lib ~ ~/Downloads ~/Desktop
So we see that the order of elements in the directory stack has changed and is now the same as we wanted. Of course, you can move the directory stack elements any number of times. For example, the following command will move them up twice:
$ pushd +2 ~/Downloads ~/Desktop /usr/lib ~
You can also use negative index values:
$ pushd -1 /usr/lib ~ ~/Downloads ~/Desktop
Similarly, you can use this technique with the popd command to remove any entry from the directory stack without leaving the current working directory. For example, if you want to use popd to remove the third entry from the top (currently ~/Downloads), you would run the following command:
popd +2
Remember that the initial value of the stack index is 0, so we use 2 to access the third entry.
So the directory stack now contains:
$ dirs /usr/lib ~ ~/Desktop
Confirm that the entry has been removed.
如果由于某些原因,你发现你很难记住元素在目录堆栈中的位置以及它们的索引,你则可以对在 dirs 命令中使用 -v 选项。这里有一个例子:
$ dirs -v 0 /usr/lib 1 ~ 2 ~/Desktop
你可能已经猜到了,左边的数字是索引,接下来跟的是这个索引对应的目录路径。
注意: 在 dir 中使用 -c 选项清除目录堆栈。
现在让我们简要地讨论一下 popd 和 pushd 命令的实际用法。虽然它们第一眼看起来可能有点复杂,但是这些命令在编写 shell 脚本时会派上用场 – 你不需要记住你从哪里来;只要执行一下 popd,你就能回到你来的目录。
经验丰富的脚本编写者通常以以下方式使用这些命令:
popd >/dev/null 2>&1
上述命令确保 popd 保持静默(不产生任何输出)。同样,你也可以静默 pushd。
pushd 和 popd 命令也被 Linux 服务器管理员使用,他们通常在几个相同的目录之间移动。 在这里介绍了一些其他有用的使用场景。
总结
我同意 pushd 和 popd 的概念不是很直接。但是,它需要的只是一点练习 – 是的,你需要多实践。花一些时间在这些命令上,你就会开始喜欢它们,特别是当它们提供了方便时。
The above is the detailed content of Tips on using command line tools in Linux systems (2). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

VS Code One-step/Next step shortcut key usage: One-step (backward): Windows/Linux: Ctrl ←; macOS: Cmd ←Next step (forward): Windows/Linux: Ctrl →; macOS: Cmd →

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

There are six ways to run code in Sublime: through hotkeys, menus, build systems, command lines, set default build systems, and custom build commands, and run individual files/projects by right-clicking on projects/files. The build system availability depends on the installation of Sublime Text.

To install Laravel, follow these steps in sequence: Install Composer (for macOS/Linux and Windows) Install Laravel Installer Create a new project Start Service Access Application (URL: http://127.0.0.1:8000) Set up the database connection (if required)

Visual Studio Code (VSCode) is a cross-platform, open source and free code editor developed by Microsoft. It is known for its lightweight, scalability and support for a wide range of programming languages. To install VSCode, please visit the official website to download and run the installer. When using VSCode, you can create new projects, edit code, debug code, navigate projects, expand VSCode, and manage settings. VSCode is available for Windows, macOS, and Linux, supports multiple programming languages and provides various extensions through Marketplace. Its advantages include lightweight, scalability, extensive language support, rich features and version
