Filesystem Commands

The Linux filesystem is a foundational aspect of the operating system, providing the structure for data storage and organization. Understanding how to navigate and manage the filesystem using command-line tools is essential for system administrators, developers, and anyone working with Linux environments. This guide covers some of the most important filesystem commands that help you interact with files, directories, and storage devices.

pwd - Print Working Directory

The pwd command prints the full path of the current working directory. It's a simple but essential command for confirming your location within the filesystem.

pwd

This command is particularly useful when navigating deeply nested directories, helping you keep track of where you are in the directory structure.

ls - List Directory Contents

The ls command lists the contents of a directory, providing information about files and subdirectories.

ls
  • Options:
    • -l: Displays a detailed list with file permissions, ownership, size, and modification date.
    • -a: Includes hidden files (those starting with a dot .).
    • -h: Shows file sizes in human-readable format (e.g., KB, MB).

These options are often combined (e.g., ls -lah) to get a comprehensive view of directory contents.

cd - Change Directory

The cd command changes the current working directory, allowing you to move around the filesystem.

cd /path/to/directory
  • Shortcuts:
    • cd ~: Moves to your home directory.
    • cd ..: Moves up one directory level.
    • cd -: Switches to the previous directory.

These shortcuts are handy for quickly navigating to commonly used locations.

Managing Files and Directories

mkdir - Make Directories

The mkdir command creates one or more directories.

mkdir directory_name
  • Options:
    • -p: Creates parent directories as needed (e.g., mkdir -p /parent/child/grandchild).

This is especially useful when you need to create nested directories in a single command.

rmdir - Remove Empty Directories

The rmdir command removes empty directories.

rmdir directory_name

If the directory contains files or other directories, use rm -r instead.

rm - Remove Files or Directories

The rm command removes files or directories. This command is powerful and should be used with caution.

rm file_name
  • Options:
    • -r: Recursively removes directories and their contents.
    • -i: Prompts before each removal, providing an additional layer of safety.

Caution: The rm command is irreversible. Double-check your command before execution, especially when using the -r option.

cp - Copy Files and Directories

The cp command copies files or directories from one location to another.

cp source_file destination_file
  • Options:
    • -r: Recursively copies entire directories.
    • -i: Prompts before overwriting existing files.

This command is useful for duplicating files or creating backups.

mv - Move or Rename Files and Directories

The mv command moves or renames files and directories.

mv old_name new_name
  • Use cases:
    • Renaming a file: mv old_name new_name
    • Moving a file to a different directory: mv file_name /path/to/directory

This command is commonly used for organizing files or restructuring directory layouts.

The ln command creates links between files, either as hard links (which point directly to the file data) or symbolic (soft) links (which point to another file).

ln source_file link_name
ln -s source_file symbolic_link_name
  • Hard Link: ln source_file link_name creates a direct reference to the original file.
  • Symbolic Link: ln -s source_file symbolic_link_name creates a pointer to the original file, allowing for more flexible file management.

Viewing and Manipulating File Content

cat - Concatenate and Display File Content

The cat command displays the content of a file.

cat file_name
  • Options:
    • -n: Numbers the output lines, which is helpful for viewing line numbers in a file.

less and more - View File Content

less and more allow you to view large files one page at a time.

less file_name
more file_name
  • Navigation:
    • Space: Move to the next page.
    • q: Quit the viewer.

These commands are essential when dealing with large log files or lengthy documents.

head and tail - View Beginning or End of Files

head shows the first few lines of a file, while tail shows the last few lines.

head file_name
tail file_name
  • Options:
    • -n X: Displays the first/last X lines (e.g., head -n 10 file_name).

tail is particularly useful with the -f option to monitor files in real-time (e.g., tail -f logfile).

touch - Update File Timestamps or Create New Files

The touch command changes the access and modification times of a file. If the file doesn't exist, touch creates it.

touch file_name

This command is commonly used to create empty files or update timestamps.

Filesystem Information and Management

df - Disk Space Usage

The df command displays disk space usage for the filesystem.

df -h
  • Options:
    • -h: Displays sizes in a human-readable format (e.g., GB, MB).

This command is critical for monitoring available disk space, especially on servers or systems with limited storage.

du - Estimate File or Directory Space Usage

The du command shows disk usage of files and directories.

du -h directory_name
  • Options:
    • -h: Displays sizes in a human-readable format.
    • -s: Summarizes the total space used by a directory.

du is helpful for identifying which directories are consuming the most space.

mount - Mount Filesystems

The mount command attaches a filesystem to the directory tree.

mount /dev/sda1 /mnt
  • Unmounting: To detach the filesystem, use umount:
umount /mnt

Mounting is essential for accessing different filesystems, such as external drives or network shares.

fsck - Filesystem Check

The fsck command checks and repairs filesystem inconsistencies.

fsck /dev/sda1

This command is crucial for maintaining filesystem integrity, especially after improper shutdowns or power failures.

mkfs - Make Filesystems

The mkfs command formats a partition with a specified filesystem.

mkfs.ext4 /dev/sda1
  • Common Filesystems:
    • ext4: The most common Linux filesystem.
    • xfs: A high-performance filesystem, often used in enterprise environments.

Formatting partitions is a fundamental task when setting up new storage devices.

find - Search for Files

The find command searches for files in a directory hierarchy.

find /path/to/search -name "file_name"
  • Options:
    • -name: Search by file name.
    • -type f: Search for files only.
    • -type d: Search for directories only.

find is incredibly powerful for locating files based on various criteria, such as name, type, or modification time.

locate - Quickly Find Files

The locate command searches a pre-built database for files, making it much faster than find but slightly less up-to-date.

locate file_name

Run updatedb to refresh the database, ensuring locate returns the most accurate results.