Even if you've been using the cd command for years, you might be
surprised at some of its features. After all, when did you last run man cd?
(For a link to the documentation for the cd command, see Resources.)
The cd command has a few little tricks that can save you lots of
unnecessary typing. Considering how often you have to change
directories, that's good news for you.
This article looks at the UNIX® directory structure and shows how you
can see your current directory path and add your directory to your shell
prompt. After looking at some simple cd examples, the article explains
what is meant by the absolute path and the relative path and
when to use each one. Then, I share some of my favourite time-saving cd
shortcuts, such as how to get to your home directory and how to
identify the home directory of any user. I show how to toggle back and
forth between two directories and provide a little-known gem about cd
that I like to call the cd shuffle. It's a simple way of moving between two similar directory paths.
Before looking at changing directories, however, it's important to understand a bit about the UNIX directory structure.
Uprooting the directory tree
What you may know as folders in Windows® operating systems are called directories in UNIX. A directory is a container that holds groups of files and other directories.
All directories in UNIX branch downward from the root directory, denoted by the forward slash (/). For example:
- The directory /usr is a subdirectory of the root directory (/).
- The /usr/spool directory is a subdirectory of /usr.
- The /usr/spool/mail directory is a subdirectory of /usr/spool.
. . . and so on.
What directory am I in?
From a shell prompt, you can display the path name of the directory you're in by running the command pwd, shown in Listing 1. Remember this command as the present working directory.
Listing 1. Display the present working directory (pwd)
# pwd # /home/surya
The abc of cd
To change to another directory, use cd followed by a space, and then the
directory you want to go to. Remember that UNIX commands are
case-sensitive, so make it all lowercase unless the directory name
actually has uppercase letters in it. In the examples in Listing 2, each
directory I'm changing to starts with a slash (/), because I'm using the absolute path, tracing the trail of directories all the way from the root directory.
Listing 2. The cd command using an absolute path
# cd /var # cd /usr/spool/mail # cd /home/surya
You can get to any directory at all—if you have permission (you need
execute permission)—using its absolute path name. The commands
in Listing 2 are provided as examples. You wouldn't ordinarily run two
cd commands in a row, because the point of changing your working
directory is to do some work in it, not to move on to somewhere else
straight away.
If the directory you entered isn't a valid directory or you don't have
permission to go there, the cd command reports an error. If your cd
command fails, then you stay in the directory you started from.
Where am I?
Okay. Your cd command didn't report any failures, so you assume that it
worked. But it would be nice to know for sure which directory you're now
in.
Of course, you could run the pwd command
every time you need to check the current working directory, but there's
a better way. Whenever you run the cd command successfully, the new
working directory is stored in the environmental variable $PWD (Present
Working Directory). Note that this variable is in uppercase letters,
unlike the pwd command. So, you could display the value of $PWD using echo, as you can see in Listing 3.
Listing 3. Display $PWD
# echo $PWD # /home/surya
Doesn't seem much easier than running that pwd command
you saw earlier, does it? Still, it's helpful to know that your
directory is stored in a variable. Here's why: You can display the value
of $PWD as part of the shell prompt.
Prompt my working directory
In the examples used so far, the shell prompt is set to the hash, or pound, symbol (#).
But if you include $PWD in your shell prompt, you always know what
directory you're in. Then, when you run cd, the variable PWD will be
updated and displayed as part of the shell prompt.
You can set the shell prompt variable PS1 in your .profile in your home directory by adding the lines shown in Listing 4.
Listing 4. Include $PWD in the shell prompt
PS1='${PWD} > ' export PS1
When you next log in, your .profile should execute as part of the login
process, and this should display the working directory as part of the
shell prompt. If you know how to get to your home directory, you could
execute the .profile right now (see Listing 5).
Listing 5. Execute your new .profile with the new shell prompt
# . ./.profile /home/surya >
You can tailor the prompt to include the host name, your login name, or
some other display characters. For details about enhancing the shell
prompt, see the link to the article "Tip: Prompt magic" in Resources.
Relatively better
It's a bit cumbersome using absolute paths when you only want to jump
from one branch of the tree to another nearby. That's why cd allows you
to use relative paths. By default, the relative path refers to the
directory relative to the current directory you are in. Using the
relative path often means fewer keystrokes, although that depends on the
directory you are going from and the one you're headed to.
To get from /home/surya to /home/surya/bin, for example, you don't have
to enter the absolute path of the target directory (/home/surya/bin);
it's enough to enter the new path relative to the one you're already in,
as you can see in Listing 6.
Listing 6. The cd command using a relative path
/home/surya > cd bin /home/surya/bin >
Notice how the shell prompt shows the new value for $PWD.
Visit your parents
If you run the ls command using the -a flag, you can see an entry for . (dot) and another for .. (dot-dot).
The single dot represents the current directory. The two dots are for
the parent directory—the one immediately above the directory you're in.
Using the parent directory is handy when you want to go up a level via cd. Listing 7 shows how.
Listing 7. Using the cd command to go to the parent directory (dot-dot)
/var/spool/mqueue > cd .. /var/spool >
You can then head down to a new sub-directory (see Listing 8 ).
Listing 8. Using cd to go to a sub-directory
/var/spool > cd mail /var/spool/mail >
Or, you could do all of that in a single command, as you see in Listing 9.
Listing 9. Branch to branch in one command
/var/spool/mqueue > cd ../mail /var/spool/mail >
You can even jump up a couple of levels, and then down a couple, as shown in Listing 10.
Listing 10. Jump through branches
/usr/IBM/WebSphere/AppServer/profiles > cd ../../PortalServer/log /usr/IBM/WebSphere/PortalServer/log >
Once you get used to using the relative path, it becomes second nature.
The shortcut home
Every UNIX user has a home directory that is defined when the user is
created. You could look up your home directory in /etc/passwd or use smit, but there's a better way of getting home.
Using cd straight to home
If you want to get to your own home directory, use the cd command without any parameters, as shown in Listing 11.
Listing 11. Fast-track home with cd
/usr/IBM/WebSphere/PortalServer/log > cd /home/surya >
Your home directory is stored in the variable $HOME. That means that the cd command without parameters is equivalent to typing cd $HOME (see Listing 12).
Listing 12. cd $HOME
/var/spool/mail > cd $HOME /home/surya >
That $HOME variable is useful for knowing your home directory even if
you're not headed there just yet. In fact, the $HOME variable can be so
helpful that it's got an alias: the tilde (~).
Call home, tilde
You may want to view or work on files in your home directory. If you're
in some other directory, there's no need to go home first or to type the
full directory path. Just use the tilde character. In Listing 13, I
make a copy of my .profile in my home directory, all from the comfort of
somewhere else.
Listing 13. Tilde shortcut for $HOME
/usr/IBM/WebSphere > cp ~/.profile ~/.profile.save
Remote access to your neighbour's home
You can also use tilde to list or work with files in another user's home
directory (if your permissions allow it). To do this, just use tilde
followed by the user's login name, as Listing 14 shows.
Listing 14. Tilde is everyone's HOME
/home/surya > cp ~john/.profile ~john/.profile.save
This is safer than guessing the user's home directory and easier than looking it up in /etc/passwd.
Dashing back
Quite often, you need to change directory only to run a command or two,
and then return to the directory you were in previously ($OLDPWD). To do
that, use the cd dashback. That's cd followed by a dash (cd -). In Listing 15, notice how the $PS1 shell prompt displays the new directory each time I run cd.
Listing 15. Return to previous directory
/home/surya > cd /usr/sys/inst.images /usr/sys/inst.images > cd - /home/surya >
The toggle switch
A consequence of this cd dashback is that if you enter it twice, you can
toggle back and forth between two directories. This functionality could
be useful if you wanted to change a program or configuration file in
one directory and see the results in a log file in a different
directory. Listing 16 shows the toggle between two directories. As with
the other examples, I'm skipping the commands you might run right after
you've actually changed directory.
Listing 16. Toggle between $PWD and $OLDPWD
/data/log > cd /apps/config /apps/config > cd - ps/config > /data/log > cd - /a p
The cd shuffle
The feature that I find especially helpful is the cd shuffle. It's a
simple way of switching from an old directory to a new one when the two
directory paths have only one difference, such as a single word.
The syntax may look odd to UNIX old hands if they have never used it, but it works. See Listing 17.
Listing 17. cd shuffle syntax
cd directorya directoryb
The first parameter is the string you want to replace in the current
directory path. The second parameter is the replacement string. For
example, to move from v7 to v8, you just type cd v7 v8, as you can see in Listing 18.
Listing 18. Using cd shuffle
/programs/v7/reports/monthly > cd v7 v8 /programs/v8/reports/monthly >
That single command has saved 19 keystrokes! That's much simpler than
going up three parent directories, and then heading back down the
directory tree or using the absolute path.
This two-parameter cd command has lots of uses: swapping between similar
directory paths where the only difference is a database instance name, a
branch name, or maybe a date. The cd shuffle can save you thousands of
keystrokes in a very short time.
Jump through history
If you have a directory for each year and each month of history, cd
shuffle allows you to jump around from one year to another. See how it
works in Listing 19.
Listing 19. New year
/hist/2010/april/reports > cd 2010 2011 /hist/2011/april/reports >
If you want to change to a different month within the same year, use cd shuffle with the from month and the to month as its parameters, as shown in Listing 20.
Listing 20. Swap month directory
/hist/2011/april/reports > cd april may /hist/2011/may/reports >
If two directory paths have only one string different, cd shuffle is ideal.
Is cd okay?
When I use cd in scripts, I always verify that the change directory has
worked before continuing with the next command. I once saw an operating
system wiped out by a two-line cleanup script that had been working
every day for two years. An NFS-mounted directory became unavailable
when a remote host was turned off. The cd command failed, and the
cleanup script continued anyway until there was nothing left on the
system to clean up.
A simple way of verifying that cd worked before proceeding with something else is to use the shell short-circuit && straight after a cd command. If the cd command fails, the next command won't continue. See Listing 21.
Listing 21. cd and short-circuit
cd /some/dir && rm *.log
Conclusion
In this article, you learned about the cd command. You saw how to change
directories using the absolute path and the relative path, and you
learned how to display the working directory you are in and toggle back
and forth between two directories. You saw different ways of referring
to home directories, and you learned about the little-known cd shuffle,
which allows you to substitute a string in your current path and switch
to a new path.
The cd command is so important on the command line, and it's used so often, that it's worth knowing some of its many shortcuts.
No comments:
Post a Comment