2.4.6. Moving about the file system#
Youâve explored the Linux file system using the graphical user interface in Section 2.3.3. Of course, you can also do this using the shell. Weâll first have to become a bit more specific about how we represent files and directories.
The naming of directories and files is as follows:
a single
/at the beginning denotes the root directory;a name followed by a
/indicates a directory;a name not followed by a
/indicates a file.
For example, the command date is stored in /usr/bin/date. There is a subdirectory below the root directory called usr. This directory itself has a subdirectory called bin, and in this directory there is a file called date. We call /usr/bin/date the path to the command date; itâs the path you have to take through the tree to get to the sequence of bytes called date.
The path to date is an example of an absolute path. No matter where you currently are in the tree, you will first have to return to the root (/) and from there go to /usr/bin to find date. Itâs also possible to have a relative path. Say you are currently in the directory /usr, then itâs not necessary to go all the way back to the root. Instead, you can simply specify that date is in the bin subdirectory of the directory youâre in. The relative pathname then would be bin/date. Note that thereâs no / at the beginning of a relative path.
The relative path above only refered to a file âdownwardâ in the tree, away from the root. However, itâs also possible to refer to higher directories. The first directory above the one youâre currently in is called the parent directory and can be referenced by ... For example, say we are in /usr, then the path to the root directory could be written as ... If weâre in /usr/bin, the path to the root directory is ../...
Exercise 2.31
Starting in /usr/bin, where do you think you will end up if you go to ../../usr? And if you go to ../bin/../../usr/bin/../bin/?
Where do you think you end up if you go to ../../../?
Note that we have introduced a directory we âare inâ now. This is called the current working directory. By using commands to change this, you will be able to walk around in the filesystem tree. Unix has a number of commands to do this.
2.4.6.1. Print the working directory#
The first is used to find out where you are:
$ pwd
/home/c_unx001
The command is short for print working directory. If you log in, you start in your home directory. This is the directory in the filesystem which is yours to do with what you want: you can create subdirectories and files as you please. This is certainly not the case in other parts of the filesystem!
A âshorthandâ notation for the home directory is a tilde, â~â. (this also explains why thereâs a tilde in your prompt while your home directory is your working directory).
2.4.6.2. Change the working directory#
To change directories, you can use the cd command (short for change directory). It simply takes as argument the place you want to go, e.g.:
$ cd /usr/bin
or
$ cd ..
Exercise 2.32
Use the cd command to verify the answers you gave to Exercise 2.31.
Two final useful properties of cd are:
cdwithout any options will take you back to your home directory;cd -will take you back to the directory you were last.
Exercise 2.33
$ cd /bin
$ cd /usr/bin
$ cd -
Where should you now be? Verify this using $ pwd. Go back to your home directory by typing:
$ cd
2.4.6.3. Listing the content of a directory#
To have a look at your directory, use the command ls (short for âlist filesâ):
$ ls
This will print all files and directories in the directory youâre currently working in. You canât see any files and directories above or below.
ls is probably one of the commands with the largest number of options in all of Unix . The most important one lets you specify another directory to look in:
$ ls /bin
Now ls shows you which files are present in the directory /bin, rather than the directory youâre currently in.
Exercise 2.34
Inspect the ls man page and try some of the options, such as -lS and -lt.
We will look at more advanced uses of ls later in the manual in Section 2.5.1.