Home >System Tutorial >LINUX >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).
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.
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!