pushd Command in Linux with Examples - GeeksforGeeks

02 Nov, 2020
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:
pushd [directory]

the type command shows that pushd is a shell built-in 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.
pushd command pushes directories onto a stack.  Execute the following commands:
pushd ~/Desktop

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. 
dir -v -l

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]

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

linux-command
Linux-directory-commands
Linux-Unix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Thread functions in C/C++
Basic Operators in Shell Scripting
nohup Command in Linux with Examples
Array Basics in Shell Scripting | Set 1
chown command in Linux with Examples
mv command in Linux with examples
Named Pipe or FIFO with example C program
SED command in Linux | Set 2
scp command in Linux with Examples
Docker - COPY Instruction