Skills of using command line tools in Linux system (2)

In the first part of this series, we  focused on  command line navigation  in Linux by discussing the usage of the cd -command . Some other related points/concepts are also discussed. Moving on now, in this article we will discuss how to use the pushd and popd commands for a faster navigation experience on the Linux command line.

Before we start, 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 what the pushd and popd commands do, let's first discuss the concept of a stack. Imagine you have a blank area on your kitchen chopping board and you want to put a set of plates on it. How would you do it? Simple, put one on top of the other.

So at the end of the process, the first plate on the chopping board is the last plate in the stack, and the last plate in your hand is the first plate in the stack. Now when you need a plate, you pick the one that is at the top of the pile and use that, then pick the next one when needed.

The pushd and popd commands are similar concepts. On a Linux system 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 the command-line terminal starts:

$dirs
 ~

A tilde (~) in the output indicates that the directory stack currently only contains 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's an example:

pushd /home/himanshu/Downloads/

What the above command does is, change the current working directory to the directory you passed as an argument and also add the path to the directory stack. As a convenience to the user, the pushd command produces the contents of the directory stack in its output. So, when the above command was run, the following output was produced:
~/Downloads ~
The output shows that there are now two directory paths in the stack: one for the user's home directory, and one for the user's downloads directory. They are saved in this order: the main directory at the bottom, with the newly added Downloads directory 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.

Do remember that the head of the stack is your current directory. This means that our current working directory is now ~/Desktop.

Now, suppose you want to go 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 can be seen from the command output. This way, the popd command will allow you to browse these directories in reverse order.

some advanced usage

Now that we've covered the basics of the pushd and popd commands, let's move on to some additional details related to these commands. First, these commands also allow you to manipulate the directory stack. For example, suppose your directory stack looks like this:

$dirs
~/Desktop /usr/lib ~ ~/Downloads

Now, our requirement is to change the order of the directory paths in the stack, with the topmost element (~/Desktop) placed at the bottom, and each of the remaining ones 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 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 two times:

$ 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 entries 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 can 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.

If for some reason you find it difficult to remember where elements are in the directory stack and their indices, you can use the -v option to the dirs command. Here's an example:

$ lesson -v
0 /usr/lib
1 ~
2 ~/Desktop

As you may have guessed, the number on the left is the index, followed by the directory path corresponding to this index.

NOTE: Use the -c option with dir to clear the directory stack.

Now let's briefly discuss the practical usage of the popd and pushd commands. While they might look a bit complicated at first glance, these commands  come in handy when writing shell scripts - you don't need to remember where you came from; just do a popd and you'll be back in the directory you came from.

Experienced scripters typically use these commands in the following way:

popd >/dev/null 2>&1

The above command ensures that popd remains silent (produces no output). Likewise, you can also silence pushd.

The pushd and popd commands are also used by Linux server administrators, who usually move between several identical directories. Some other useful usage scenarios are described here.

Summarize

I agree that the concept of pushd and popd is not very straightforward. But, all it takes is a little practice - and yes, you need a lot of practice. Spend some time with these commands and you'll start to like them, especially when they provide convenience.

 

Guess you like

Origin blog.csdn.net/yaxuan88521/article/details/131654196