Working in multiple directories from a Linux command prompt can be a pain, but these quick tips on using the cd and pushd directory navigation commands can save you time and effort.
Directory changing basics
At the Linux command prompt, use the cd command to change the working directory, where actions take place by default. Here are some basic uses for the cd command:
- To change to your home directory, type cd and press [Enter].
- To change to a subdirectory, type cd, a space, and the name of the subdirectory (e.g., cd Documents) and then press [Enter].
- To change to the current working directory’s parent directory, type cd followed by a space and two periods and then press [Enter].
- To change to a directory specified by a path name, type cd followed by a space and the path name (e.g., cd /usr/local/lib) and then press [Enter].
- To confirm that you’ve switched to the directory you wanted, type pwd and press [Enter]. You’ll see the path name of the current directory.
Switching between different directories with pushd
When working with two directories in command-line mode, you may find yourself switching between them—and doing a lot of time-consuming retyping in the process. If the directories aren’t located within the same parent directory, you’ll have to type the whole path name to change from one directory to the next, which can be tedious. However, you can eliminate all that extra typing by using one of two approaches: Either use the shell’s history list or use the pushd shell function, which are part of the Go-Funcs utilities.
Taking advantage of the shell’s history list is simply a matter of a few keystrokes. Just push the Up Arrow key to evoke a cd command with the previously visited directory at the command line. Keeping pressing the Up Arrow key until you see the previously visited directory you need and then press [Enter]. If the two directories you are switching between are relatively close to each other in the drive tree, the shell’s history list is probably your best bet.
However, if you are bouncing between two directories with highly distinct paths, you might want to take advantage of the extremely handy pushd function. Pushd enables you to create a stack (a list kept in memory) of directory locations. NOTE: By default, the pushd function may not be available with your Linux distribution; it’s installed by default with Red Hat and Red Hat-derived systems. If you don’t have pushd on your system, you can get the Go-Funcs utilities from ibiblio.org.
Here’s how to use pushd. Suppose you’re currently working in /usr/share/fonts. You need to do some work in /opt/wonderword/fonts, and you’ll frequently be switching back and forth between the two directories. Beginning in one of the directories, use pushd to switch to the other directory. In our example, starting from /usr/share/fonts, you’d type pushd /opt/wonderword/fonts and press [Enter]. Now you’ll see the contents of the stack on the next line, as in:
As you can see, when the pushd function is typed with a path name, it automatically creates a stack consisting of the named directory (/opt/wonderword/fonts) and the directory where you issued the command (/usr/share/fonts). In our example, the first directory indicated (/opt/wonderword/fonts) is at the “top” (or beginning) of the stack.
To quickly switch back to the previous directory, type pushd again. When typed without a directory path, pushd simply switches the positions of the two directories at the top of the stack and reverts to the new top-level directory.
If you need to remove a directory from the stack, type popd followed by the name of the directory and press [Enter]. To see the list of directories in the stack, type dirs and press [Enter]. The commands popd and dirs also are part of the Go-Funcs utilities.
Free Linux help
Are you looking for a simple way to improve your Linux skills? Our Linux TechMail is the answer, containing valuable information that can save you time and effort. This article offers a good example of what this TechMail has to offer. Get tips on printing, networking, security, and much more, all delivered straight to your inbox—absolutely free! Sign up for the Linux TechMail today!
Subscribe to the Developer Insider Newsletter
From the hottest programming languages to commentary on the Linux OS, get the developer and open source news and tips you need to know. Delivered Tuesdays and Thursdays