In the first article of this series, I told you that everything in Linux (and UNIX as well) is a file. Text data files, executable commands, shell scripts, and even hardware devices, all are represented as files. In this article, we will discuss file types, directories and the filesystem. Have a nice reading!
Files and Directories
A file can be defined as a collection of data stored as a unit, and has a name.
Files can be classified into the following types:
- Ordinary (Regular) Files: this type includes ASCII (text) files, and binary executables. In the long listing option of the ls command, ordinary files are distinguished by a hyphen (-) in the beginning of the line.
- Symbolic Links: are shortcuts to other files or directories. In the long listing, they start with lowercase L (stands for link).
- Device Files: Even hardware devices are also represented by a special type of files called device files. Device files are stored under a location (directory) named /dev. In turn, device files are divided into two main subtypes:
- Block Devices: like hard disks, CD-ROM. In the long listing, files of this type have letter b in the beginning of the line.
- Character Devices: like terminals, printers. These devices read and write one character at a time. They are distinguished by letter c.
- Directory Files: A special type of file that acts like a logical container for other files is called Directory. To organize things, it is wise to logically store similar files in directories. Directories are distinguished by the letter d in the beginning of the long listing.
The Linux Filesystem
In UNIX and Linux systems, files and directories are organized in an inverted tree structure. The top of the tree is called root directory. The root directory is written as single forward slash character /. A path to a file or directory is the route to it.
During the Linux installation, a number of built-in directories are created for you. The following table lists those directories with the usage of each:
|/||The root of the filesystem structure.|
|/bin, /usr/bin||The directories that contain user’s binary executable files.|
|/sbin, /usr/sbin||Directories that contain system executables.|
|/etc||System configuration files are stored under this directory.|
|/root||The home directory of the root (superuser).|
|/home/||The parent directory of all users’ home directories (other than the root user).|
|/tmp||A directory for temporary files.|
|/var||Files that are changed frequently (like log files) are stored under /var (from variable).|
|/usr||Directory to store user programs.|
|/lib, /usr/lib||Shared Libraries.|
|/opt||Optional Software files.|
|/boot||A directory that stores the kernel and boot loader files.|
|/mnt, /media||Two mount directories for removable media and file shares.|
|/proc||A pseudo file system that provides info about running processes.|
Absolute versus Relative Paths
The absolute path to file or directory starts from the root directory along to the target file (or directory). For this reason, all absolute pathnames start with /.
Relative Paths determine the route to a file or directory, starting from the current directory.
Although they provide a shorter alternative (instead of using long absolute pathnames), they are more error-prone.
The . and .. Directories
There are two special directories in UNIX and Linux: the . and .. directories.
The . refers to the current directory, while .. refers to the parent directory.
- Starting from /root as the current working directory, either of the following could list the contents of /root/bin:
- Starting from /etc/sysconfig/networking, the following will change the directory to /etc:
In UNIX and Linux shells, the tilde represents a short for the home directory of the current user. For example, if the current logged in user is mohamed, tilde ~ will mean /home/mohamed.
Files and Directories Naming
- All characters (including special characters and white space) are acceptable in a file or directory name, except the forward slash / (as it is used in path names to separate a parent directory from a sub-directory).
- File names and directory names are case sensitive. So, data.txt, Data.txt, DATA.txt, and data.TXT are completely different file names.
- Using special characters and white spaces in file and directory names are not recommended, and should be avoided where possible.
Displaying the Current Working Directory
To print the current working directory, use the pwd command.
Changing Between Directories
To change the current directory to another (say /etc/), use the command cd.
To change back to your home directory, use one of the following forms:
cd cd ~
Both are equivalent.
To change back to the last directory you were in, use the following command:
In this article, we have talked about the Linux filesystem. We started by introducing the file types and the usage of each. Then, we talked about the built-in directories in Linux systems, like /, /tmp, /home, /opt, /var, and /mnt.
When working with files and directories, we could use either absolute path names or relative path names. Finally, we talked about the cd command that is used to move between directories.
In the next article we will cover: Working with Files and Directories.