pushd Command in Linux with Examples
pushd is a shell built-in command which allows us to easily manipulate the directory stack. This appends a directory to the top of the directory stack, or rotates the stack, making the new top of the stack the present working directory. The “d” in pushd stands for the directory as it pushes the directory path onto the stack. The directory stack increases in size after each pushd command. This stack is based on the Last In First Out (LIFO) principle. This command has an exit status 0 i.e, it returns success unless an invalid argument is supplied or the directory change fails.
Syntax of pushd command:
How pushd command functions?
- When pushd [directory] command is executed, the directory specified becomes the present working directory.
- The path and name of the directory are added to the top of the directory stack.
- The directory stack is displayed as a space-separated list of directories.
- If pushd command is executed without any directory name, then the directory at the top of the stack becomes the current working directory i.e, the first two directories exchange their position in the directory stack.
Advantage of pushd command over cd command
It is a very powerful tool for operation on directories as it stores the directories in the stack. Consider nested directories. We can navigate between the dictionaries using the cd command. But suppose, you are in the fourth directory. Then to navigate to the second directory, the cd command has to be used twice. But by using pushd command, it can be achieved in one step. In one step, we can navigate from any directory in the stack to another directory in the stack. Directory manipulation becomes easier and efficient.
Working with pushd command
1. Adding directories using pushd command:
pushd command pushes directories onto a stack. Execute the following commands:
Now, Desktop becomes the present working directory and is pushed onto the stack and the list of directories is displayed. The same operations will be performed for the other commands below.
pushd ~/Templates pushd ~/Videos pushd ~/Downloads pushd ~/Music pushd ~/Downloads
It can be observed that after each pushd command the directory specified becomes the new directory. It can be seen that ‘Downloads’ has been added to the directory stack twice but there was no error as the directory stack allows duplicate directories. The directory stack can be displayed using the following command. It can be observed that the command which is entered at the beginning is displayed at the last position and the most recent directory added is displayed at the top.
dirs -l -v
2. Adding a directory without changing the current directory:
Whenever a new directory is added onto the stack, the current directory is changed to the new one. But a new directory can be added keeping the current directory unchanged using “-n” along with pushd command. This command will push the directory to the second spot and the present directory remains unchanged at the first spot rotated. The syntax for the command is:
pushd -n [Directory]
3. Moving to a directory at any position in the stack:
We can use numeric parameters along with pushd command to move to any directory present in the stack. Here the numeric parameter represents the position of the directory in the stack. The directory at that position becomes the current directory and the stack is rotated. pushd +n counts n from the top of the directory stack. pushd -n counts n from the bottom of the directory stack.
pushd +N #N is a numeric parameter pushd -N #N is a numeric parameter