Working with the File System Hierarchy

To manage a Linux system effectively, it’s crucial to understand the default directories and the concept of mounts that create the file system hierarchy. Here’s an overview of the key directories and how mounts work.

Defining the File System Hierarchy

The layout of the Linux file system is defined by the Filesystem Hierarchy Standard (FHS). The main directories on a Linux system are as follows:

  • /: The root directory where the file system tree starts.
  • /dev: Contains device files for accessing physical devices, essential during boot.
  • /home: Stores local user home directories.
  • /opt: Used for optional packages that may be installed on your server.
  • /root: The home directory of the root user.
  • /srv: May be used for data by services like NFS, FTP, and HTTP.
  • /tmp: Contains temporary files that may be deleted without warning during boot.
  • /var: Contains files that may change dynamically, such as log files, mailboxes, and spool files.

Understanding Mounts

Mounting is the process of attaching a device to a specific directory, making the device’s contents accessible through that directory. This allows the Linux file system to be flexible and avoid issues that arise from using a single file system for all files.

Benefits of Using Multiple Mounts

  1. Preventing Overuse: High activity in one area won’t fill up the entire file system.
  2. Enhanced Security: Different areas of the file system can have different security needs.
  3. Additional Storage: Makes it easier to add storage space.

Common Mount Points

  • /boot: Often mounted separately because it contains essential boot information.
  • /boot/EFI: For systems using EFI, this contains files needed during the early boot process.
  • /var: Often on a separate device due to its dynamic growth, preventing it from filling up all storage.
  • /home: Separately mounted for security and ease of OS reinstallation.
  • /usr: Contains operating system files and is typically mounted read-only.

Getting an Overview of Mounts

Several commands can be used to view mounted devices:

  1. mount: Displays all mounted devices by reading the /proc/mounts file.
  2. df -Th: Shows available disk space on mounted devices in a human-readable format and includes the type of file system.
  3. findmnt: Displays mounts and their relationships.

Managing Files

Managing files involves tasks such as working with wildcards, directories, and pathnames, as well as listing, copying, moving, and deleting files.

Working with Wildcards

Wildcards make it easier to refer to multiple files:

  • *: Matches any number of any characters (e.g., ls * lists all files in the current directory).
  • [abc]: Matches one character from the specified set (e.g., ls c[auo]t matches cat, cut, and cot).

Managing and Working with Directories

Creating and Navigating Directories

  • pwd: Prints the current working directory.
  • cd /path/to/directory: Changes the current directory.
  • mkdir /path/to/newdir: Creates a new directory.
  • rmdir /path/to/dir: Removes an empty directory.

Examples

  1. Creating a Directory:
    • Create a directory named files in the current directory.
  2. Navigating Directories:
    • Changes the current directory to /tmp.

Working with Absolute and Relative Pathnames

  • Absolute Pathname: A full path from the root directory (e.g., /home/user/file).
  • Relative Pathname: A path relative to the current directory (e.g., ../user/file if you are in /home).

Listing Files and Directories

  • ls: Lists directory contents.
  • ls -l: Long listing format showing file properties.
  • ls -a: Includes hidden files (those starting with a dot).

Copying Files and Directories

  • cp /source/file /destination: Copies a file.
  • cp -R /source/dir /destination: Copies a directory and its contents recursively.
  • cp -a /source/dir /destination: Preserves file attributes during copy.

Moving Files and Directories

  • mv /source/file /destination: Moves a file.
  • mv /source/dir /destination: Moves a directory.

Deleting Files and Directories

  • rm /path/to/file: Deletes a file.
  • rm -r /path/to/dir: Deletes a directory and its contents recursively.

Example Workflow

  1. Create Directories:
    • Create directories named newfiles and oldfiles.
  2. Create Files:
    • Create files named .hidden and unhidden in the newfiles directory.
  3. Copy Files:
    • Copy all contents from newfiles to oldfiles.
  4. List Files:
    • List all files in the oldfiles directory.
  5. Delete Directory:
    • Delete the newfiles directory inside oldfiles.
  6. Move Files:
    • Move the unhidden file from newfiles to /tmp.

By mastering these file system and file management concepts, you can efficiently navigate and organize a Linux system.

Another important file-related task is managing archives and compressed files. To create an archive of files on a Linux computer, you often use the tar command. This command was originally designed to stream files to a tape without any compression of the files, and it still doesn’t compress anything by default. If you want to compress files as well, you have to either use a specific compression tool or specify an option that compresses the archive while it is created. In this section, you learn how to work with archives and compressed files.

Managing Archives with tar

The Tape ARchiver (tar) utility is used to archive files. Although originally designed to stream files to a backup tape, in its current use, tar is mostly used to write files to an archive file. You have to be able to perform four important tasks with tar:

  1. Create an archive
  2. List the contents of an archive
  3. Extract an archive
  4. Compress and uncompress archives

Creating Archives with tar

To create an archive, you use the following syntax: tar -cf archivename.tar /files-you-want-to-archive. If you want to see what is happening, use the -v option as well. To put files in an archive, you need at least read permissions to the file and execute permissions on the directory the file resides in. Use tar -cvf /root/homes.tar /home as user root to write the contents of the /home directory and everything below it to the file homes.tar in the directory /root. Notice the options that are used; the order in these options is important.

Originally, tar did not use the dash (-) in front of its options. Modern tar implementations use that dash, as do all other Linux programs, but they still allow the old usage without a dash for backward compatibility.

While you’re managing archives with tar, it is also possible to add a file to an existing archive or to update an archive. To add a file to an archive, you use the -r options. Use, for instance, tar -rvf /root/homes.tar /etc/hosts to add the /etc/hosts file to the archive.

To update a currently existing archive file, you can use the -u option. So, use tar -uvf /root/homes.tar /home to write newer versions of all files in /home to the archive.

Monitoring and Extracting tar Files

Before you extract a file, it is good to know what might be expected. The option -t can be used to find out. Type, for instance, tar -tvf /root/homes.tar to see the contents of the tar archive.

It is good practice to create archive files with an extension such as .tar or .tgz so that they can be easily recognized, but not everyone does that. If you think that a file is a tar archive but you are not sure, use the file command. If you type file somefile, for instance, the file command analyzes its contents and shows on the command line what type of file it is.

To extract the contents of an archive, use tar -xvf /archivename.tar. This extracts the archive in the current directory.

To extract one file out of the archive, use tar -xvf /archivename.tar file-you-want-to-extract.

Using Compression

Many files contain a lot of redundancy. Compression programs allow you to make files take less disk space by taking out that redundancy. In all examples of the tar command that you have seen so far, not a single byte has been compressed. Originally, after you created the archive, it had to be compressed with a separate compression utility, such as gzip or bzip2.

To decompress files that have been compressed with gzip or bzip2, you can use the gunzip and bunzip2 utilities.

As an alternative to using these utilities from the command line, you can include the -z (gzip), -J (xz), or -j (bzip2) option while creating the archive with tar. This will immediately compress the archive while it is created. There is no need to use this option while extracting. The tar utility will recognize the compressed content and automatically decompress it for you.

Summary

In this chapter, you learned how to work with essential file management tools. You learned how the Linux directory structure is organized by default, and you learned what file types to expect in which directories. You also learned how to find your way in the directory structure and to work with files.


Creating Symbolic Links

  • Action: Open a shell as the student user.
  • Description: Opens a shell prompt as the student user.
  • Purpose: To perform file operations as the student user.
  • Action: Type ln /etc/passwd ..
  • Description: Attempts to create a hard link to /etc/passwd in the current directory (operation not permitted).
  • Purpose: To demonstrate permission restrictions for creating hard links.
  • Action: Type ln -s /etc/passwd ..
  • Description: Creates a symbolic link to /etc/passwd in the current directory.
  • Purpose: To demonstrate the creation of symbolic links.
  • Action: Type ln -s /etc/hosts.
  • Description: Creates a symbolic link to /etc/hosts in the current directory (target not specified).
  • Purpose: To demonstrate creating symbolic links without specifying a target.

Creating Hard Links

  • Action: Type touch newfile.
  • Description: Creates a new file named newfile.
  • Purpose: To create a file for linking demonstration.
  • Action: Type ln newfile linkedfile.
  • Description: Creates a hard link named linkedfile pointing to newfile.
  • Purpose: To demonstrate the creation of hard links.
  • Action: Type ls -l.
  • Description: Lists files in the current directory with detailed information.
  • Purpose: To verify the creation of hard links.
  • Action: Type ln -s newfile symlinkfile.
  • Description: Creates a symbolic link named symlinkfile pointing to newfile.
  • Purpose: To create a symbolic link to newfile.
  • Action: Type rm newfile.
  • Description: Removes the original file newfile.
  • Purpose: To remove the original file linked to by the symbolic link.
  • Action: Type cat symlinkfile.
  • Description: Attempts to read the content of newfile using the symbolic link (no such file or directory error).
  • Purpose: To demonstrate the effect of removing the original file on symbolic links.
  • Action: Type cat linkedfile.
  • Description: Reads the content of linkedfile.
  • Purpose: To verify the content is accessible through the hard link.
  • Action: Type ls -l.
  • Description: Lists files in the current directory with detailed information.
  • Purpose: To verify the link counters and file properties.
  • Action: Type ln linkedfile newfile.
  • Description: Creates a hard link named newfile pointing to linkedfile.
  • Purpose: To restore the original situation with hard links.
  • Action: Type ls -l.
  • Description: Lists files in the current directory with detailed information.
  • Purpose: To verify the restoration of the original situation.

Using tar

1. Creating and Manipulating Tar Archives

  • Action: Open a root shell on your server.
  • Description: Opens a root shell prompt for performing administrative tasks.
  • Purpose: To execute commands with root privileges.
  • Action: Type tar cvf etc.tar /etc.
  • Description: Creates a tar archive named etc.tar containing the contents of the /etc directory.
  • Purpose: To create a tar archive of system configuration files.
  • Action: Type file etc.tar.
  • Description: Displays information about the etc.tar file.
  • Purpose: To verify the file type of the tar archive.
  • Action: Type gzip etc.tar.
  • Description: Compresses the etc.tar file using gzip.
  • Purpose: To compress the tar archive.
  • Action: Type tar tvf etc.tar.gz.
  • Description: Lists the contents of the gzip-compressed tar archive.
  • Purpose: To view the contents of the compressed tar archive.
  • Action: Type tar xvf etc.tar.gz etc/hosts.
  • Description: Extracts the hosts file from the gzip-compressed tar archive.
  • Purpose: To extract a specific file from the compressed tar archive.
  • Action: Type ls -R.
  • Description: Lists all files and directories recursively.
  • Purpose: To verify the extracted files and directories.
  • Action: Type gunzip etc.tar.gz.
  • Description: Decompresses the gzip-compressed tar archive.
  • Purpose: To decompress the compressed tar archive.
  • Action: Type tar xvf etc.tar -C /tmp etc/passwd.
  • Description: Extracts the passwd file to the /tmp directory.
  • Purpose: To extract a specific file to a different directory.
  • Action: Type tar cjvf homes.tar /home.
  • Description: Creates a compressed tar archive named homes.tar containing the contents of the /home directory.
  • Purpose: To create a compressed tar archive of user home directories.
  • Action: Type rm -f *gz *tar.
  • Description: Removes all files resulting from the exercises in this chapter.
  • Purpose: To clean up the root directory from temporary files and archives.
Share the Post:

Leave a Reply

Your email address will not be published. Required fields are marked *

Join Our Newsletter

Delivering Exceptional Learning Experiences with Amazing Online Courses

Join Our Global Community of Instructors and Learners Today!