pushd and popd

Both pushd and popd are shell builtin commands. The pushd command is used to save the current directory into a stack and move to a new directory. Furthermore, popd can be used to return back to the previous directory that is on top of the stack.

It is very useful when we have to switch between two directories frequently.

The syntax of using pushd is as follows:

pushd [directory]

If no directory is specified, pushd changes the directory to whatever is on the top of the stack.

The syntax of using popd is as follows:

popd

Using the popd switch, we can go back to the previous directory that is on top of the stack and pop that directory from stack.

The following example counts the number of files or directories in a specified directory ...

Get Linux Shell Scripting Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.