Skip to main content

Working with a file system

6. Working with a File System:

Working with a file system in Linux involves managing files and directories, performing operations like creating, deleting, moving, copying, and modifying files. Additionally, you can use advanced tools like awk and sed for text processing and manipulation. Let's delve into working with a file system with examples:

Basic File System Operations:

  1. Navigating the File System:

    • pwd (Print Working Directory): Displays the current directory.

      pwd
    • ls (List Files and Directories): Lists the contents of a directory.

      ls
    • cd (Change Directory): Moves to a different directory.

      cd /path/to/directory
  2. Creating and Deleting Directories:

    • mkdir (Make Directory): Creates a new directory.

      mkdir my_directory
    • rmdir (Remove Directory): Deletes an empty directory.

      rmdir my_directory
    • rm -r (Remove Recursive): Deletes a directory and its contents recursively.

      rm -r my_directory
  3. Creating and Deleting Files:

    • touch: Creates an empty file.

      touch myfile.txt
    • rm: Deletes a file.

      rm myfile.txt

Advanced File System Operations:

  1. Moving and Copying Files:

    • mv (Move): Moves or renames files and directories.

      mv file1.txt /path/to/destination/
    • cp (Copy): Copies files and directories.

      cp file1.txt /path/to/destination/
  2. Text Processing with awk and sed:

    • awk is a versatile text processing tool for extracting and manipulating data from text files.

      # Example: Extract and print the second column of a CSV file
      awk -F',' '{print $2}' data.csv
    • sed is a stream editor for performing basic text transformations on an input stream.

      # Example: Replace all occurrences of "old" with "new" in a file
      sed 's/old/new/g' myfile.txt
  3. Archiving and Compression:

    • tar (Tape Archive): Used for archiving files and directories.

      # Create a tarball of a directory
      tar -czvf archive.tar.gz my_directory/
    • gzip and gunzip: Compress and decompress files.

      # Compress a file
      gzip myfile.txt
      # Decompress a compressed file
      gunzip myfile.txt.gz
  4. File Permissions:

    • chmod (Change Mode): Modify file permissions.

      # Give read and write permissions to the owner of a file
      chmod u+rw myfile.txt
  5. File Searching:

    • find: Search for files and directories.

      # Find all files with a specific extension in the current directory and subdirectories
      find . -type f -name "*.txt"
  6. Disk Usage:

    • du (Disk Usage): Display disk usage for files and directories.

      # Show disk usage for a directory
      du -h /path/to/directory

These are some of the fundamental and advanced file system operations in Linux. The awk and sed commands are particularly powerful for text processing tasks, making them essential tools for working with textual data. Remember to exercise caution when performing operations like file deletion and use appropriate options to prevent data loss.

Hard links and symbolic links (soft links) are two types of links in Unix-like file systems that allow you to reference files or directories by different names. They serve different purposes and have distinct characteristics. Let's explore both types of links in detail, along with examples:

1. Hard Links:

  • Characteristics:

    • Hard links are multiple directory entries (filenames) that point to the same inode (data structure representing a file on disk).
    • All hard links to the same file share the same inode number.
    • Changes to the file's content or attributes through one hard link are reflected in all other hard links pointing to the same file.
    • Hard links do not consume additional disk space because they share the same data blocks.
    • Hard links continue to work when you move the target within the same file system but become "dangling" if you move or delete the target, especially across file systems.
    • Hard links cannot point to directories.
  • Example: Let's say you have a file named "file.txt" in your home directory. You create a hard link called "backup.txt" to this file.

    $ touch file.txt
    $ ln file.txt backup.txt

    Both "file.txt" and "backup.txt" now point to the same data on disk. If you edit "file.txt" or "backup.txt," the changes will be visible in both files because they are essentially two names for the same file.

    $ echo "Hello, world!" > file.txt
    $ cat backup.txt
    Hello, world!

    Deleting one of the hard links will not affect the other link until the last hard link is removed, at which point the data is marked as available for reuse.

2. Symbolic Links (Soft Links):

  • Characteristics:

    • Symbolic links (often called "symlinks" or "soft links") are separate files that contain a reference to the path of the target file or directory.
    • They do not share the same inode as the target file or directory.
    • Symbolic links can span different file systems and even point to non-existent targets.
    • Symbolic links become "broken" or "dangling" when you move or delete the target, and they require updating to point to the new location for them to work again.
  • Example: Let's create a symbolic link from "link_to_file.txt" to "file.txt":

    $ ln -s file.txt link_to_file.txt

    Now, "link_to_file.txt" is a symbolic link to "file.txt."

    $ cat link_to_file.txt
    Hello, world!

    Unlike hard links, symbolic links can point to directories as well. For instance, creating a symbolic link to a directory:

    $ ln -s /path/to/directory my_directory_link

    Here, "my_directory_link" is a symbolic link to the specified directory.