After completing this lab, students will be able to:
A file system is a method for organizing and storing data. Without a file sytem, the operating sytem (and the users) would have a hard time locating files that are needed to perform tasks. The file system consists of files and their attributes and relationships between files.
The Linux file system is composed of files and directories.
A file is a collection of data that is logically related, and it is the smallest unit of storage in the Unix file system.
Directories are also file, but they are special since they may contain other files and other directories (a physical analogy isa folder or envelope where you can use to organize and store papers, or other envelopes).
In this way, the files sytem is organized in a hierarchical structure that resembles a tree, typically represented upside down (in the same fashion as a family tree).
The top directory in a Linux file system is called the root directory, or simply, the root, and it is represented by the /
(slash) character.
The standard hierarchy of Linux Filesystems is published by the Linux Foundation as a specification (available under https://refspecs.linuxfoundation.org/fhs.shtml). The following directories are required by the standard (Refer to Table 3-4 in TLCL for more details)
Directory | Description |
/ | root directory |
/bin | Essential command that must be present for boot/recovery |
/boot | Contains the boot loader and kernel files |
/dev | Device files. |
/etc | System configuration |
/home | Users directories and files |
/lib | Essential shared libraries and kernel modules |
/media | Mount point for removable media (USB Drives, CD-ROMS) |
/mnt | Mount point for mounting a filesystem manually |
/opt | Add-on application software packages (“optional”) |
/proc | Process information pseudo-file system (contains runtime information). |
/root | Home directory for the “root” user. |
/run | Data required by running processes |
/sbin | System binaries. |
/tmp | Temporary files |
/usr | All the programs and support files to be used by regular users |
/var | Variable data (logs, user mail, spool files, etc). |
The mkdir
utility creates new directories. It requires as arguments the pathnames of the directories to be created. The following example shows the commands required to generate the directory structure shown below (the shown output assumes that your home directory is empty. If that is not the case, then existing files will show in the output of the ls
command.)
[user@blue ~]$ ls [user@blue ~]$ mkdir vehicles [user@blue ~]$ ls vehicles [user@blue ~]$ mkdir -p vehicles/cars/sedan [user@blue ~]$ ls vehicles/cars sedan [user@blue ~]$ mkdir -p vehicles/cars/pickup vehicles/cars/suv [user@blue ~]$ ls vehicles/cars pickup sedan suv
Notice that we did not create the cars
directory directly, and instead we used the -p
option to instruct the mkdir
command to make any non-existing intermeditate directories.
Note also that you can create more than one directory at a time.
The tree
command is useful to have a directory listing in a tree like structure:
[user@blue ~]$ tree vehicles vehicles └── cars ├── pickup ├── sedan └── suv 4 directories, 0 files
To remove directories you can use the rmdir
command.
Just as mkdir
, it requires the pathnames of the directories to be removed.
However, rmdir
has the limitation that it only works with empty directories.
To remove directories that are not empty, you can use the rm -r
command.
Let’s remove the ./vehicles/cars/suv
directory:
[user@blue ~]$ rmdir vehicles/cars/suv [user@blue ~]$ rm -r vehicles/cars/pickup [user@blue ~]$ tree vehicles vehicles └── cars └── sedan 2 directories, 0 files
The mv
command can be used for two purposes: rename a file/directory, or move the file/directory to another path.
Let’s rename the sedan
directory to coupe
:
[user@blue ~]$ mv vehicles/cars/sedan vehicles/cars/coupe [user@blue ~]$ tree vehicles vehicles └── cars └── coupe 2 directories, 0 files
Noticed that the first argument is the source file or directory and the second is the destination directory or file. In this case, since the destination did not exist, then it perfoms a “rename”
To see how the mv
command can be used to move files, let’s recreate the sedan
directory and and empty file under it:
[user@blue ~]$ mkdir vehicles/cars/sedan [user@blue ~]$ touch vehicles/cars/sedan/shelby [user@blue ~]$ tree vehicles vehicles └── cars ├── coupe └── sedan └── shelby 3 directories, 1 file
Let’s move shelby
from the sedan
directory to the coupe
directory (where it belongs, obviously).
[user@blue ~]$ mv vehicles/cars/sedan/shelby vehicles/cars/coupe [user@blue ~]$ tree vehicles vehicles/ └── cars ├── coupe │ └── shelby └── sedan 3 directories, 1 file
Notice that in this case, the destination exists, and it is a directory, so the result of the command is that the file is move to the destination directory.
The cp
command is used to copy files or directories. In the following example, a file is created by redirecting the output of the echo
command into a file called original
.
The cat
command reads that file and prints that on the screen, this is done as a verification step.
original
is then copied into a file called clone
. Notice how the contents of clone
are the same as the contents of original
.
[user@blue ~]$ echo "This text is the content" > original [user@blue ~]$ ls original vehicles [user@blue ~]$ cat original This text is the content [user@blue ~]$ cp original clone [user@blue ~]$ ls clone original vehicles [user@blue ~]$ cat clone This text is the content
In order to copy directories, you need to provide the -r
option to the cp
command. This option stands for recursive, and basically it means that it will traverse the whole tree under the directory that you want to copy.
Lab Report
mail
inside the /var/log
directory. What is the relative path to that directory if you are in your home directory?/usr/include/curl/./././///../boost/random/../math
. Simplify that path so it does not use relative references (hint: try it with the cd
command).The following diagram corresponds to a portion of the filesystem in blue
. Fill out the empty elements in A
, B
, C
and D
.
/usr/bin
directory?vmlinuz
)?vehicles
directory into a directory called vehicles_backup
. What command will serve this purpose?transport
├── bicycles
│ └── mountain
├── cars
│ ├── pickup
│ ├── sedan
│ │ └── compact
│ └── suv
└── jet
├── cargo
└── fighter
transport
directory (durango is an SUV, prius a compact sedan, and stumpjumper is a mountain bike).prius
is incorrect. Provide a single command that will make the file name reflect the content of the file (e.g. the file name should be fseries
instead of prius
) and will place the file under the correct directory (~/transport/cars/pickup
)