This objective is about navigating the filesystem through the command line. Unlike Windows systems, Linux systems don’t use drive letters. Instead, they use mount points. On a Linux system, the filesystem begins at /, known as the root directory, instead of the respective drive letter for the storage device or network share. You access the underlying storage by using the directory or subdirectory it is mounted on. For example, you may have a USB hard drive that you decide to mount on /usb_drive
. Any time you read or right from a file in that directory, it is using that drive. I explain this concept in my guide covering the Linux Filesystem Hierarchy. You don’t need to memorize the entire FHS, but I recommend becoming familiar with it. You should also understand what mounting and mount points are.
There are only three commands that you need know for this objective. These commands are cd
, pwd
, and ls
. The cd
command is used to change your working directory. Use ls
to list the contents of your current working directory or another directory. pwd
outputs your current working directory. They will be covered in more detail later on.
The best way to learn Linux is to get your hands dirty. If you don’t have access to a Linux system, you can always run an emulated system in your browser provided by bellard.org. I recommend the Fedora console for my Linux Essential study guides.
Absolute vs Relative Paths
The location of a file is known as its path. There are two types. The first is the absolute path. The second is the relative path.
The absolute path is the path to a file or directory starting from /
. For example, the ls
command is usually located at /bin/ls
or /usr/bin/ls
. Absolute paths will always begin with /
.
The relative path depends on your current working directory. If your current directory is /usr
, and the absolute path to ls
is /usr/bin/ls
, the relative path to the ls
program file is bin/ls
. Relative paths will never begin with /
.
Absolute paths always start with /
. Relative paths never start with /
.
The Special .
and ..
Directories
The .
and ..
directories are special directories inside every directory on the system, including /
. The .
is a shortcut for the current directory. The ..
is short for the directory one level up from your current directory. This should be made clear by the examples shown later in the study guide.
Home Directories
Each user on a Linux system has a home directory specified in their account settings. This directory is the current directory when the user logs in. It is typically in /home/username
, where username
is the username of the user. For example, my user account on the Linux machine I am writing this guide on is tyler
. My home directory is /home/tyler
.
Home directories are not just a place for users to keep their files. They are also used to store user specific application configuration and data files. For example, your web browser will store your history and settings in your home directory.
There is a shortcut, ~
, to your home directory that you can use to save some typing, or if you are writing a shell script, have a simple way to refer to the running user’s home directory. To use the shortcut, prepend it to a path name. For example, if I wanted to refer to the scripts
directory within my home directory, I would use ~/scripts
. This is interpreted as /home/tyler/scripts
.
Using the cd
and pwd
Commands
The cd
command changes your current directory. The pwd
command outputs your current directory. The examples below demonstrate everything I have covered so far.
$ pwd
/home/tyler
$ cd ..
$ pwd
/home
$ cd .
$ pwd
/home
$ cd /usr/local/bin
$ cd ../lib
$ pwd
/usr/local/lib
$ cd ~/lpi
$ pwd
/home/tyler/lpi
$ cd ../.././tyler/./Downloads
$ pwd
/home/tyler/Downloads
$ cd ~/.gnucash
$ pwd
/home/tyler/.gnucash
$ cd ../../
$ pwd
/home
$ cd ~/..
$ pwd
/home
Notice you can combine the special directories and the home shortcut? The best way to get a grasp on navigating Linux systems from the command line is practice. Remember, if you don’t have access to a Linux system, you can always use the JSLinux browser based console to practice. Practice combining different combinations of the special directories and the home directory shortcut.
Hidden Files
Files that begin with .
are considered hidden and won’t be shown in most situations unless you explicitly instruct the system do so. The method of viewing hidden files is program specific. E.g. you use the -a
or -A
with ls
.
Using the ls
Command
If you want to view the contents of a directory, use the ls
command. By default ls
only shows the names of files in your current working directory. If you wish to see the contents of another directory, you must cd
into it or specify it on the command line.
$ pwd
/var
$ ls
backups cache games lib local lock log mail opt run spool tmp www
$ ls spool
anacron caldavd cron cups exim4 lpd mail rsyslog
$ cd spool
$ ls
anacron caldavd cron cups exim4 lpd mail rsyslog
You are probably wondering how you can tell which listing is a file, and which is a directory. Fortunately, ls
has options you can pass to it that will instruct it to display this information. The -F
option does just that.
$ ln -s b link
$ ls -F
a* b c dir/ link@ named_pipe| socket=
Notice how some of the directory’s contents have non alpha-numeric characters at the end? Those characters indicate that it is not just a regular file. The table below describes their meaning.
Symbol | Meaning |
---|---|
/ |
The listing is a directory. |
* |
It is a regular file with the execute permissions set. Permissions are beyond the scope of this lesson, but I have a guide covering them if you want to learn more. |
@ |
The file is symbolic link. A symbolic link is a file that points to another file and behaves the same as the actual file. This is common on systems with multiple versions of a program. On my machine, I have two variants of ksh . There is a symbolic link /bin/ksh that points to /bin/ksh93 . I wrote a guide covering links if you want to learn more. |
| |
This means the file is a named piped, aka First in First Out (FIFO). A FIFO is special file processes use for one way communication. |
= |
A socket file is used by programs for bi-directional communication with other processes on the same system. |
NONE | If the file name doesn’t end with one of the characters above, it is a regular file. |
The ls
command has a lot of options. Some of the common ones are described in the table below, followed by some examples.
OPTION | DESCRIPTION |
---|---|
-l |
Shows detailed information about files including, permissions, size, and modification time. |
-A |
Includes hidden files in the output, with the exception of the special . and .. directories. |
-a |
Outputs all files with no exceptions. |
-t |
Sorts output by modification time, newest first. |
-r |
Reversed the order of sorting options. |
-h |
When used with detailed listing options, converts sizes to human readable units. |
-S |
Sorts files by size. The largest files are shown first. |
-F |
Appends characters to the output to show the file type. |
Options can be combined. My favorite set of ls
options is -lArth
. This set displays detailed information, most recently modified files shown last, all files except the special directories, and human readable sizes.
$ ls
a b c dir link named_pipe
$ ls -A
a b c dir .hidden link named_pipe
$ ls -a
. .. a b c dir .hidden link named_pipe
$ ls -l
total 7172
-rwxr-xr-x 1 tyler tyler 2097152 Sep 27 09:02 a
-rw-r--r-- 1 tyler tyler 1048576 Sep 27 09:02 b
-rw-r--r-- 1 tyler tyler 4194304 Sep 27 09:02 c
drwxr-xr-x 2 tyler tyler 4096 Sep 26 11:51 dir
lrwxrwxrwx 1 tyler tyler 1 Sep 26 11:52 link -> b
prw-r--r-- 1 tyler tyler 0 Sep 26 12:02 named_pipe
$ ls -lS
total 7172
-rw-r--r-- 1 tyler tyler 4194304 Sep 27 09:02 c
-rwxr-xr-x 1 tyler tyler 2097152 Sep 27 09:02 a
-rw-r--r-- 1 tyler tyler 1048576 Sep 27 09:02 b
drwxr-xr-x 2 tyler tyler 4096 Sep 26 11:51 dir
lrwxrwxrwx 1 tyler tyler 1 Sep 26 11:52 link -> b
prw-r--r-- 1 tyler tyler 0 Sep 26 12:02 named_pipe
$ ls -lSr
total 7172
prw-r--r-- 1 tyler tyler 0 Sep 26 12:02 named_pipe
lrwxrwxrwx 1 tyler tyler 1 Sep 26 11:52 link -> b
drwxr-xr-x 2 tyler tyler 4096 Sep 26 11:51 dir
-rw-r--r-- 1 tyler tyler 1048576 Sep 27 09:02 b
-rwxr-xr-x 1 tyler tyler 2097152 Sep 27 09:02 a
-rw-r--r-- 1 tyler tyler 4194304 Sep 27 09:02 c
$ ls -lt
total 7172
-rw-r--r-- 1 tyler tyler 1048576 Sep 27 09:02 b
-rw-r--r-- 1 tyler tyler 4194304 Sep 27 09:02 c
-rwxr-xr-x 1 tyler tyler 2097152 Sep 27 09:02 a
prw-r--r-- 1 tyler tyler 0 Sep 26 12:02 named_pipe
lrwxrwxrwx 1 tyler tyler 1 Sep 26 11:52 link -> b
drwxr-xr-x 2 tyler tyler 4096 Sep 26 11:51 dir
$ ls -lth
total 7.1M
-rw-r--r-- 1 tyler tyler 1.0M Sep 27 09:02 b
-rw-r--r-- 1 tyler tyler 4.0M Sep 27 09:02 c
-rwxr-xr-x 1 tyler tyler 2.0M Sep 27 09:02 a
prw-r--r-- 1 tyler tyler 0 Sep 26 12:02 named_pipe
lrwxrwxrwx 1 tyler tyler 1 Sep 26 11:52 link -> b
drwxr-xr-x 2 tyler tyler 4.0K Sep 26 11:51 dir
$ ls -lArth
total 7.1M
drwxr-xr-x 2 tyler tyler 4.0K Sep 26 11:51 dir
lrwxrwxrwx 1 tyler tyler 1 Sep 26 11:52 link -> b
prw-r--r-- 1 tyler tyler 0 Sep 26 12:02 named_pipe
-rw-r--r-- 1 tyler tyler 0 Sep 27 09:01 .hidden
-rwxr-xr-x 1 tyler tyler 2.0M Sep 27 09:02 a
-rw-r--r-- 1 tyler tyler 4.0M Sep 27 09:02 c
-rw-r--r-- 1 tyler tyler 1.0M Sep 27 09:02 b
$ ls -larth
total 7.1M
lrwxrwxrwx 1 tyler tyler 1 Sep 26 11:52 link -> b
prw-r--r-- 1 tyler tyler 0 Sep 26 12:02 named_pipe
-rw-r--r-- 1 tyler tyler 0 Sep 27 09:01 .hidden
drwxr-xr-x 167 tyler tyler 12K Sep 27 09:02 ..
drwxr-xr-x 3 tyler tyler 4.0K Sep 27 09:02 .
-rwxr-xr-x 1 tyler tyler 2.0M Sep 27 09:02 a
-rw-r--r-- 1 tyler tyler 4.0M Sep 27 09:02 c
-rw-r--r-- 1 tyler tyler 1.0M Sep 27 09:02 b
drwxr-xr-x 2 tyler tyler 4.0K Sep 27 09:04 dir
$ ls dir
e f g
$ ls -F
a* b c dir/ link@ named_pipe|
Summary
I recommend studying for this section by using the command line to explore a Linux system's file system. Any version of any distribution will do. The web based option listed earlier will suffice. Don't forget about the LPI learning material. Take some time to at least skim the lesson. Go through the exercises as well.