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 ../...
A common âshorthandâ notation for the userâs home directory is a tilde, â~â.
So, if your username is johndoe, then ~ would be equivalent to the absolute path /home/johndoe/.
Exercise 2.34
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 ../../../?
Exercise 2.35
Which of the following is a relative path?
/usr/bin/datebin/date../home/student/../student~/Documentsfile.txt
Exercise 2.36
Assume your current working directory is /home/student/ros2_ws/src.
What would be the relative path to /home/student?
../../..../..../home/student/home/student/~./ros2_ws/src././../..
Solution to Exercise 2.36
../..
Exercise 2.37
What is the shorthand absolute path notation for a projects/ subdirectory in your home directory in the shell?
projects./projects/home/student/projects/projects-/projects~/projects
Solution to Exercise 2.37
~/projects
Note that we have introduced the concept that, when using the shell, at any moment we âare inâ a certain directory. This is called the current working directory. It is possible to change the working directory with shell commands, enabling you to execute commands to inspect or perform file operations in different parts of 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/jkooij
The command is short for print working directory.
When you log in, the current working directory is by default 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!
In the example above, /home/jkooij/ would be the home directory of the user jkooij.
A user account student would have as home directory /home/student/
In some bash configurations, including the default one of Ubuntu, the current working directory is shown in the prompt.
This helps you to always keep track of the current working directory when executing commands.
The bash prompt may even be configured to display the ~ notation for your home directory to save screen space.
Exercise 2.38
This bash shell is not configured to show the current working directory in the prompt, or to start up in the default working directory.
{
"filesystem": {
"/opt/code/src": null,
"/home/student/.bashrc": "export PS1='$ '\ncd /opt/code/src/\n"
}
}
Use pwd to determine the current working directory.
What is the absolute path of the working directory of the terminal at startup?
/home/student/
/home/student/code/
/home/student/code/
/home/student/code/src
/home/student/opt/
/home/student/opt/code/
/home/student/opt/code/src
/home/student/src/code/
/home/student/projects
/home/
/home/user/
/home/user/code/
/home/user/code/src
/home/user/src/code/
/home/user/projects
/home/user/opt/
/home/user/opt/code/
/home/user/opt/code/src
/usr/
/usr/bin/
/opt/
/opt/code/
/opt/code/src/
/code/
/code/src/
/pwd
src/
~
.
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.39
Use the cd command to verify the answers you gave to Exercise 2.34.
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.40
$ cd /bin
$ cd /usr/bin
$ cd -
Where should you now be? Verify this using $ pwd. Go back to your home directory by typing:
$ cd
Exercise 2.41
Navigate to the projects directory in your home, and then to /usr/bin.
Afterwards, use cd - to go back to the projects directory, and
then use cd - again to move directly back to /usr/bin.
{
"checks": [
{ "type": "commandEvent", "eventType": "builtin.cd",
"match": { "absPath": "/home/student/projects" },
"desc": "cd to projects in your home" },
{ "type": "commandEvent", "eventType": "builtin.cd",
"match": { "absPath": "/usr/bin" },
"desc": "cd to /usr/bin" },
{ "type": "commandEvent", "eventType": "builtin.cd",
"match": { "absPath": "/home/student/projects", "form": "previous" },
"desc": "use cd - to ~/projects" },
{ "type": "commandEvent", "eventType": "builtin.cd",
"match": { "absPath": "/usr/bin", "form": "previous" },
"desc": "use cd - to switch back to /usr/bin" },
{ "type": "tabCompletionHint" }
]
}
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.42
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.
Exercise 2.43
Starting in /usr/bin, what is your working directory after running cd ../../usr/bin/../bin?
Enter the result as an absolute path.
Solution to Exercise 2.43
/usr/bin
Exercise 2.44
List the contents of /usr/share/common-licenses/ in long format, sorted by file size (largest first), in a single command.
{
"filesystemZip": "bash-exercise-fs/fs-usr-share-common-licenses.zip",
"checks": [
{ "type": "commandEvent", "eventType": "coreutil.ls",
"match": { "flags": ["l", "S"], "absPaths": ["/usr/share/common-licenses"] },
"desc": "use ls with -l and -S flags on /usr/share/common-licenses" }
]
}