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
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
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
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
setsid command in Linux with Examples setsid command in Linux system is used to run a program in a new session. The command will call the fork(2) if already a process group leader. Else, it will execute a program in the current process. The main advantage of using 'setsid' is that it allows programs to run independently of the terminal
3 min read
uname command in Linux with Examples Linux, renowned for its open-source flexibility and powerful performance, offers a range of commands that reveal the inner workings of your system. Among these, the 'uname' command stands out as a versatile tool that provides key details about your Linux machine. Here, we will learn the basics of th
4 min read
Sed Command in Linux/Unix With Examples The SED command is one of the most powerful commands used during the process of text processing in Linux/Unix operating systems. The SED command is typically invoked for executing operations such as replace and search, text manipulation, and stream editing.With SED, you can manipulate text files wit
9 min read
whatis Command in Linux with Examples whatis command in Linux is used to get a one-line manual page description. In Linux, each manual page has some sort of description within it. So, this command search for the manual pages names and show the manual page description of the specified filename or argument. Syntax of the `whatis` command
5 min read
printf command in Linux with Examples The 'printf' command in Linux is a versatile tool used to display formatted text, numbers, or other data types directly in the terminal. Like the 'printf' function in programming languages like C, the Linux printf command allows users to format output with great precision, making it ideal for script
4 min read