Navigating the Filesystem Using a Terminal
You need to know how to change directory and move about the fileystem using the Terminal.
The main command used for navigating the filesystem is cd (change directory). After cd you have to specify the directory that you want to change to. This can either be a relative path to a directory inside your current directory, or an absolute path to somewhere else on the filesystem. See “Discussion” on page 57.
To see what the current directory is, you can use the command pwd (print working directory).
Discussion
Try out a few examples. Open a Terminal session, and you should see a prompt like this:
pi@raspberrypi ~ $ |
The prompt that you will see after each command (pi@raspberrypi ~ $) is a reminder of your username (pi) and your computer name (raspberrypi). The ~ character is shorthand for your home directory (/home/pi). So, at any point, you can change your current directory to your home directory as follows:
$ cd ~ |
Throughout the book, I use a $ at the start of each line where you are expected to type a command. The response from the command line will not be prefixed by anything; it will appear just as it does on the Raspberry Pi’s screen.
You can confirm that the command did indeed set the directory to the home directory using the pwd command:
$ pwd /home/pi |
If you want to move up one level in the directory structure, you can use the special value .. (two dots) after the cd command, as shown here:
$ cd .. $ pwd /home |
As you may have deduced by now, the path to a particular file or directory is made up of words separated by a /. So the very root of the whole filesystem is /, and to access the home directory within / you would refer to /home/. Then, to find the pi directory within that, you would use /home/pi/. The final / can be omitted from a path.
Paths can also be absolute (starting with a / and specifying the full path from the root), or they can be relative to the current working directory, in which case they must not start with a /.
You will have full read and write access to the files in your home directory, but once you move into the places where system files and applications are kept, your access to some files will be restricted to read only. You can override this (Recipe 3.11), but some care is required.
Check out the root of the directory structure by entering the following commands:
$ cd / $ ls bin dev home lost+found mnt proc run selinux sys usr boot etc lib media opt root sbin srv tmp var |
The ls command (list) shows us all the files and directories underneath / the root directory. You will see that there is a home directory listed, which is the directory you have just come from.
Now change into one of those directories by using the command:
$ cd etc $ ls adduser.conf hosts.deny polkit-1 alternatives hp profile apm iceweasel profile.d apparmor.d idmapd.conf protocols apt ifplugd pulse asound.conf init python |
You will notice a couple of things. First, there are a lot of files and folders listed, more than can fit on the screen at once. You can use the scroll bar on the side of the Terminal window to move up and down.
Second, you will see that the files and folders have some color-coding. Files are displayed in white, while directories are blue.
Unless you particularly like typing, the Tab key offers a convenient short cut. If you start typing the name of a file, pressing the Tab key allows the autocomplete feature to attempt to complete the filename. For example, if you’re going to change directory to network, type the command cd netw and then press the Tab key. Because netw is enough to uniquely identify the file or directory, pressing the Tab key will autocomplete it.
If what you have typed is not enough to uniquely identify the file or directory name, then pressing the Tab key another time will display a list of possible options that match what you have typed so far. So, if you had stopped at net and pressed the Tab key, you would see the following:
$ cd net netatalk/ network/ |
You can provide an extra argument after ls to narrow down the things you want to list. Change directory to /etc and then run the following:
$ ls f* fake-hwclock.data fb.modes fstab fuse.conf fonts: conf.avail conf.d fonts.conf fonts.dtd foomatic: defaultspooler direct filter.conf fstab.d: pi@raspberrypi /etc $ |
The * character is called a wildcard. In specifying f* after ls, we are saying that we want to list everything that starts with an f.
Helpfully, the results first list all the files within /etc that start with f, and then the contents of all the directories in that folder starting with f.
A common use of wildcards is to list all files with a certain extension (e.g., ls *.docx).
A convention in Linux (and many other operating systems) is to prefix files that should be hidden from the user by starting their name with a period. Any so-named files or folders will not appear when you type ls unless you also supply ls with the option -a. For example:
$ cd ~ $ ls -a . Desktop .pulse .. .dillo .pulse-cookie Adafruit-Raspberry-Pi-Python-Code .dmrc python_games .advance .emulationstation sales_log .AppleDB .fltk servo.py .AppleDesktop .fontconfig .stella .AppleDouble .gstreamer-0.10 stepper.py.save Asteroids.zip .gvfs switches.txt.save atari_roms indiecity Temporary Items .bash_history .local thermometer.py .bash_logout motor.py .thumbnails .bashrc .mozilla .vnc .cache mydocument.doc .Xauthority .config Network Trash Folder .xsession-errors .dbus .profile .xsession-errors.old |
As you can see, most of the files and folders in your home directory are hidden.
See Also
See also Recipe 3.11.