how Linux organizes files and the purposes of various directories in the file-system hierarchy
The file system hierarchy
All files on a Linux system are stored on file systems, which are organized into a single inverted tree of directories, known as a file-system hierarchy. This tree is inverted because the root of the tree is said to be at the top of the hierarchy, and the branches of directories and subdirectories stretch below the root.
The / directory is the root directory at the top of the file-system hierarchy. The / character is also used as a directory separator in file names. For example, if etc is a subdirectory of the / directory, you could refer to that directory as /etc. Likewise, if the /etc directory contained a file named issue, you could refer to that file as /etc/issue.
Subdirectories of / are used for standardized purposes to organize files by type and purpose. This makes it easier to find files. For example, in the root directory, the subdirectory /boot is used for storing files needed to boot the system.
- static content remains unchanged until explicitly edited or reconfigured.
- dynamic or variable content may be modified or appended by active processes.
- persistent content remains after a reboot, like configuration settings.
- runtime content is process- or system-specific content that is deleted by a reboot.
The following lists some of the most important directories on the system by name and purpose.
/usr
Installed software, shared libraries, include files, and read-only program data. Important subdirectories include:
- /usr/bin: User commands.
- /usr/sbin: System administration commands.
- /usr/local: Locally customized software.
/etc
Configuration files specific to this system.
/var
Variable data specific to this system that should persist between boots. Files that dynamically change, such as databases, cache directories, log files, printer-spooled documents, and website content may be found under /var.
/run
Runtime data for processes started since the last boot. This includes process ID files and lock files, among other things. The contents of this directory are recreated on reboot. This directory consolidates /var/run and /var/lock from earlier versions of CentOS Linux.
/home
Home directories are where regular users store their personal data and configuration files.
/root
The home directory for the administrative superuser, root.
/tmp
A world-writable space for temporary files. Files that have not been accessed, changed, or modified for 10 days are deleted from this directory automatically. Another temporary directory exists, /var/tmp, in which files that have not been accessed, changed, or modified in more than 30 days are deleted automatically.
/boot
Files needed in order to start the boot process.
/dev
Contains special device files that are used by the system to access hardware.
- /binand/usr/bin
- /sbinand/usr/sbin
- /liband/usr/lib
- /lib64and/usr/lib64
In earlier versions of CentOS/RHEL, these were distinct directories containing different sets of files. In CentOS/RHEL 7 and later, the directories in / are symbolic links to the matching directories in /usr.
Absolute Paths and Relative Paths
The path of a file or directory specifies its unique file system location. Following a file path traverses one or more named subdirectories, delimited by a forward slash (/), until the destination is reached. Directories, also called folders, contain other files and other subdirectories. They can be referenced in the same manner as files.
Absolute Paths
An absolute path is a fully qualified name, specifying the files exact location in the file system hierarchy. It begins at the root (/) directory and specifies each subdirectory that must be traversed to reach the specific file. Every file in a file system has a unique absolute path name, recognized with a simple rule: A path name with a forward slash (/) as the first character is an absolute path name. For example, the absolute path name for the system message log file is /var/log/messages. Absolute path names can be long to type, so files may also be located relative to the current working directory for your shell prompt.
The Current Working Directory and Relative Paths
When a user logs in and opens a command window, the initial location is normally the user’s home directory. System processes also have an initial directory. Users and processes navigate to other directories as needed; the terms working directory or current working directory refer to their current location. Like an absolute path, a relative path identifies a unique file, specifying only the path necessary to reach the file from the working directory. Recognizing relative path names follows a simple rule: A path name with anything other than a forward slash as the first character is a relative path name. A user in the /var directory could refer to the message log file relatively as log/messages.
Linux file systems, including, but not limited to, ext4, XFS, GFS2, and GlusterFS, are case-sensitive. Creating FileCase.txt and filecase.txt in the same directory results in two unique files. Non-Linux file systems might work differently. For example, VFAT, Microsoft’s NTFS, and Apple’s HFS+ have case preserving behavior. Although these file systems are not case-sensitive, they do display file names with the original capitalization used when the file was created. Therefore, if you tried to make the files in the preceding example on a VFAT file system, both names would be treated as pointing to the same file instead of two different files.
Navigating Paths
The pwd command displays the full path name of the current working directory for that shell. This can help you determine the syntax to reach files using relative path names. The ls command lists directory contents for the specified directory or, if no directory is given, for the current working directory.
[user@host ~]$ pwd
/home/user
[user@host ~]$ ls
Desktop Documents Downloads Music Pictures Public Templates Videos
[user@host ~]$
Use the cd command to change your shell’s current working directory. If you do not specify any arguments to the command, it will change to your home directory. In the following example, a mixture of absolute and relative paths are used with the cd command to change the current working directory for the shell.
[user@host ~]$ pwd
/home/user
[user@host ~]$ cd Videos
[user@host Videos]$ pwd
/home/user/Videos
[user@host Videos]$ cd /home/user/Documents
[user@host Documents]$ pwd
/home/user/Documents
[user@host Documents]$ cd
[user@host ~]$ pwd
/home/user
[user@host ~]$
As you can see in the preceding example, the default shell prompt also displays the last component of the absolute path to the current working directory. For example, for /home/user/Videos, only Videos displays. The prompt displays the tilde character (~) when your current working directory is your home directory. The touch command normally updates a file’s timestamp to the current date and time without otherwise modifying it. This is useful for creating empty files, which can be used for practice, because “touching” a file name that does not exist causes the file to be created. In the following example, the touch command creates practice files in the Documents and Videos subdirectories.
[user@host ~]$ touch Videos/blockbuster1.ogg
[user@host ~]$ touch Videos/blockbuster2.ogg
[user@host ~]$ touch Documents/thesis_chapter1.odf
[user@host ~]$ touch Documents/thesis_chapter2.odf
[user@host ~]$
The ls command has multiple options for displaying attributes on files. The most common and useful are -l (long listing format), -a (all files, including hidden files), and -R (recursive, to include the contents of all subdirectories).
# ls -l
total 0
drwxr-xr-x. 2 user user 6 May 8 2019 Desktop
drwxr-xr-x. 2 user user 6 May 8 2019 Documents
drwxr-xr-x. 2 user user 6 May 8 2019 Downloads
drwxr-xr-x. 2 user user 6 May 8 2019 Music
drwxr-xr-x. 2 user user 6 May 8 2019 Pictures
drwxr-xr-x. 2 user user 6 May 8 2019 Public
drwxr-xr-x. 2 user user 6 May 8 2019 Templates
drwxr-xr-x. 2 user user 6 May 8 2019 Videos
# ls -la
total 32
drwx------. 18 user user 4096 Oct 2 14:30 .
drwxr-xr-x. 4 root root 40 May 8 2019 ..
-rw-r--r--. 1 user user 1 Sep 11 20:30 .bash_history
-rw-r--r--. 1 user user 18 Jan 14 2019 .bash_logout
-rw-r--r--. 1 user user 141 Jan 14 2019 .bash_profile
-rw-r--r--. 1 user user 312 Jan 14 2019 .bashrc
drwx------. 10 user user 154 Oct 2 14:30 .cache
drwx------. 12 user user 229 Jun 12 14:48 .config
drwx------. 3 user user 25 May 8 2019 .dbus
drwxr-xr-x. 2 user user 6 May 8 2019 Desktop
drwxr-xr-x. 2 user user 6 May 8 2019 Documents
drwxr-xr-x. 2 user user 6 May 8 2019 Downloads
-rw-------. 1 user user 16 May 8 2019 .esd_auth
-rw-------. 1 user user 310 Oct 2 14:30 .ICEauthority
drwx------. 3 user user 19 May 8 2019 .local
drwxr-xr-x. 2 user user 6 May 8 2019 Music
drwxr-xr-x. 2 user user 6 May 8 2019 Pictures
drwxrw----. 3 user user 19 May 8 2019 .pki
drwxr-xr-x. 2 user user 6 May 8 2019 Public
drwx------. 2 user user 29 May 8 2019 .ssh
drwxr-xr-x. 2 user user 6 May 8 2019 Templates
drwxr-xr-x. 2 user user 6 May 8 2019 Videos
drwxrwxr-x. 2 user user 248 Oct 2 14:29 .vnc
-rw-------. 1 user user 3623 Oct 2 14:29 .Xauthority
The two special directories at the top of the listing refer to the current directory (.) and the parent directory (..). These special directories exist in every directory on the system. You will discover their usefulness when you start using file management commands.
# ls -R
.:
Desktop Documents Downloads Music Pictures Public Templates Videos
./Desktop:
./Documents:
./Downloads:
./Music:
./Pictures:
./Public:
./Templates:
./Videos:
The cd command has many options. A few are so useful as to be worth practicing early and using often. The command ‘cd -’ changes to the previous directory; where the user was previously to the current directory. The following example illustrates this behavior, alternating between two directories, which is useful when processing a series of similar tasks.
# cd Videos/
# pwd
/home/user/Videos
# cd /home/user/Documents/
# pwd
/home/user/Documents
# cd -
/home/user/Videos
# pwd
/home/user/Videos
# cd -
/home/user/Documents
# pwd
/home/user/Documents
The cd .. command uses the .. hidden directory to move up one level to the parent directory, without needing to know the exact parent name. The other hidden directory (.) specifies the current directory on commands in which the current location is either the source or destination argument, avoiding the need to type out the directory’s absolute path name.
# pwd
/home/user/Videos
# cd .
# pwd
/home/user/Videos
# cd ..
# pwd
/home/user
# cd ..
# pwd
/home