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!

The main difference between Linux and Windows in process management lies in the implementation and concept of tools and APIs. Linux is known for its flexibility and power, relying on kernel and command line tools; while Windows is known for its user-friendliness and integration, mainly managing processes through graphical interfaces and system services.

Linuxisidealforcustomization,development,andservermanagement,whileWindowsexcelsineaseofuse,softwarecompatibility,andgaming.Linuxoffershighconfigurabilityfordevelopersandserversetups,whereasWindowsprovidesauser-friendlyinterfaceandbroadsoftwaresupport

The main difference between Linux and Windows in user account management is the permission model and management tools. Linux uses Unix-based permissions models and command-line tools (such as useradd, usermod, userdel), while Windows uses its own security model and graphical user interface (GUI) management tools.

Linux'scommandlinecanbemoresecurethanWindowsifmanagedcorrectly,butrequiresmoreuserknowledge.1)Linux'sopen-sourcenatureallowsforquicksecurityupdates.2)Misconfigurationcanleadtovulnerabilities.Windows'commandlineismorecontrolledbutlesscustomizable,with

This guide explains how to automatically mount a USB drive on boot in Linux, saving you time and effort. Step 1: Identify Your USB Drive Use the lsblk command to list all block devices. Your USB drive will likely be labeled /dev/sdb1, /dev/sdc1, etc

Cross-platform applications have revolutionized software development, enabling seamless functionality across operating systems like Linux, Windows, and macOS. This eliminates the need to switch apps based on your device, offering consistent experien

Artificial Intelligence (AI) is rapidly transforming numerous sectors, from healthcare and finance to creative fields like art and music. Linux, with its open-source nature, adaptability, and performance capabilities, has emerged as a premier platfo

Looking for a fast, minimal, and efficient Linux distribution without a graphical user interface (GUI)? Lightweight, GUI-less Linux distros are perfect for older hardware or specialized tasks like servers and embedded systems. They consume fewer res


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

SublimeText3 Linux new version
SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version
Recommended: Win version, supports code prompts!

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
