pushd Command in Linux with Examples
Last Updated :
10 Sep, 2024
The 'pushd' command is a built-in shell command that simplifies the navigation and management of directories by using a directory stack. This stack operates on the Last In, First Out (LIFO) principle, allowing users to push directories onto the stack and easily switch between them.
By appending a directory to the top of the stack, pushd makes it the current working directory. Unlike the cd command, which only changes the current directory, pushd maintains a stack of directory paths, making it highly efficient for navigating nested directories or frequently visited locations.
Syntax of 'pushd' command:
pushd [directory]
the type command shows that pushd is a shell built-in command. How does the 'pushd' Command Work?
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.
1. Adding directories using pushd command:
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.Â
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


Advantages of 'pushd' command over 'cd' command
The 'pushd' command offers significant advantages over the traditional cd command, particularly when dealing with complex directory structures:
- Efficiency in Navigation: Using 'cd', you may need to issue multiple commands to move up and down nested directories. With 'pushd', you can navigate directly between any directories in the stack, reducing the steps needed and saving time.
- Maintaining Context: 'pushd' allows you to maintain a stack of directories, making it easier to switch back and forth between frequently used paths. This is particularly useful in scripting and development workflows where you often need to return to previous directories.
- Flexibility: 'pushd' lets you add directories without changing the current directory using the '-n' option, providing greater flexibility compared to 'cd'.
Conclusion
The 'pushd' command is an incredibly versatile tool for managing directories in Linux. Its ability to push directories onto a stack and rotate between them makes it far superior to the 'cd' command for tasks that involve frequent switching between directories. Using 'pushd' along with other stack manipulation commands like 'popd' and 'dirs', can help achieve complex directory navigation and manipulation tasks with ease, making it an essential tool for any Linux user or system administrator.
Similar Reads
popd Command in Linux with Examples
popd command is used to remove directories from the directory stack. The "d" in popd stands for the directory as it removes the directory path onto the stack. After this command is executed, the present directory stack is displayed as a list of space-separated directories. The directory stack decrea
3 min read
shuf Command in Linux with Examples
The shuf command in Linux writes a random permutation of the input lines to standard output. It pseudo randomizes an input in the same way as the cards are shuffled. It is a part of GNU Coreutils and is not a part of POSIX. This command reads either from a file or standard input in bash and randomiz
4 min read
Shift command in Linux with examples
Shift is a built-in command in bash that after getting executed, shifts/moves the command line arguments to one position left. The first argument is lost after using the shift command. This command takes only one integer as an argument. This command is useful when you want to get rid of the command
3 min read
usermod command in Linux with Examples
usermod command or modify user is a command in Linux that is used to change the properties of a user in Linux through the command line. After creating a user we have to sometimes change their attributes like password or login directory etc. so in order to do that we use the Usermod command. The info
4 min read
rcp Command in Linux with examples
When working in a Linux environment, there often comes a time when you need to transfer files from one computer to another. While more secure options like scp or rsync exist, the rcp (Remote Copy Protocol) command offers a simple and efficient way to copy files between systems, especially for beginn
5 min read
od command in Linux with example
The od (octal dump) command in Linux is a versatile tool used to display file contents in various formats, with the default being octal. This command is particularly useful for debugging scripts, examining binary files, or visualizing non-human-readable data like executable code. It allows users to
6 min read
seq command in Linux with Examples
The 'seq' command in Linux is a powerful utility used to generate a sequence of numbers. It is particularly useful in scenarios where you need to create a list of numbers within loops, such as while, for, or until loops. With 'seq', you can quickly generate numbers from a starting value (FIRST) to a
4 min read
sync command in Linux with Examples
sync command in Linux is used to synchronize cached writes to persistent storage. If one or more files are specified, sync only them, or their containing file systems. Syntax: sync [OPTION] [FILE]... Note: Nothing is being shown in the screenshots just because sync command makes the cache in the bac
1 min read
read command in Linux with Examples
read command in the Linux system is used to read from a file descriptor. This command reads up the total number of bytes from the specified file descriptor into the buffer. If the number or count is zero, this command may detect errors. But on success, it returns the number of bytes read. Zero indic
3 min read
ln command in Linux with Examples
The 'ln' command in Linux is a powerful utility that allows you to create links between files. These links can either be hard links or soft (symbolic) links. If you're unfamiliar with these concepts, check out our detailed guide on Hard and Soft Links in Linux to understand their differences, use ca
3 min read