How To Use Pushd & Podp Command.
In this tutorial, we will show you how to use the pushd and popd commands to navigate your system’s directory tree.
pushd and popd are commands that allow you to work with directory stack and change the current working directory in Linux and other Unix-like operating systems. Although pushd and popd are very powerful and useful commands, they are underrated and rarely used.
The directory stack is a list of directories you have previously navigated to. The contents of the directory stack can be seen using the dirs command. Directories are added to the stack when changing to a directory using the pushd command and removed with the popd command.
The current working directory is always on the top of the directory stack. The current working directory is the directory (folder) in which the user is currently working in. Each time you interact with the command line, you are working within a directory.
The pwd command allows you to find out what directory you are currently in.
When navigating through the file system, use the Tab key to autocomplete the names of directories. Adding a slash at the end of the directory name is optional.
pushd, popd and dirs are shell builtins, and its behavior may slightly differ from shell to shell. We will cover the Bash builtin version of the commands.
The syntax for the pushd command is as follows:
For example to save the current directory to the top of the directory stack and change to /var/www you would type:
On success, the command above will print the directory stack.
~ is the directory in which we executed the pushd command. The tilde symbol
~ means home directory.
pushd first saves the current working directory to the top of the stack and then navigates to the given directory. As the current directory must always be on the top of the stack, once changed the new current directory goes to the top of the stack but it is not saved in the stack. To save it you must invoke pushd from it. If you use cd to change to another directory, the top item of the stack will be lost,
Let’s add another directory to the stack:
To suppress changing to directory, use the -n option. For example, to add the /usr/local directory to the stack but not change into it you would type:
As the current directory (which is always in the top) is not changed, the /usr/local directory is added second from the top of the stack:
The pushd accepts two options, +N and -N that allows you to navigate to Nth directory of the stack. The +N option changes to Nth element of the stack list counting from left to right starting with zero. When -N is used the direction of the count is from right to left.
To better illustrate the options, let’s print the current directory stack:
The output will show an indexed list of the directory stack:
If you want to change to the /var/www directory, and bring it to the top of the stack you will use one of the following.
When counting from top to bottom (or left to right), the index of the directory is 2.
When counting from bottom to top the index of the /var/www directory is 1.
When used without any argument, pushd will toggle the top two directories and makes the new top the current directory. This is the same as when using the cd – command.
The popd command takes the form:
When used with no argument, popd removes the top directory from the stack and navigates to the new top directory.
Let’s say we have the following directory stack:
If you run the popd command it will remove the /opt from the stack and change to the /usr/local directory:
The output will show the new directory stack:
The -n option suppresses the default directory change and removes the second item from the stack:
Same as pushd, popd also accepts the +N and -N options that can be used to remove the Nth directory of the stack.
Normally, you would use the cd command to move from one directory to another. However, if you spend a lot of time on the command line, pushd and popd commands will increase your productivity and efficiency.