Filesystem Tools

Filesystem Manipulation Tools

Overview

This section details the fundamental tools for interacting with the filesystem. These commands allow you to navigate directories, create, delete, copy, and move files and directories, as well as view and manipulate their contents and metadata.

Mastering these tools is crucial for efficient system administration, development workflows, and everyday command-line usage.

Core Commands

ls - List Directory Contents

The ls command is used to list information about files and directories. It's one of the most frequently used commands.

Usage:

ls [OPTIONS] [FILE...]

Common Options:

  • -l: Use a long listing format.
  • -a: Do not ignore entries starting with . (hidden files).
  • -h: With -l, print sizes in human readable format (e.g., 1K, 234M, 2G).
  • -t: Sort by modification time, newest first.

Example:

ls -lah

Lists all files and directories in the current directory, including hidden ones, in a long format with human-readable sizes.

cd - Change Directory

The cd command changes the current working directory.

Usage:

cd [DIRECTORY]

Special Directories:

  • .: The current directory.
  • ..: The parent directory.
  • ~: The user's home directory.

Example:

cd /var/log

Changes the current directory to /var/log.

cd ..

Moves up one directory level.

mkdir - Make Directories

Creates new directories.

Usage:

mkdir [OPTIONS] DIRECTORY...

Common Options:

  • -p: Create parent directories as needed.

Example:

mkdir project_files backups/daily

Creates two directories: project_files and backups/daily (creating backups if it doesn't exist due to the -p flag, though not explicitly used here, it's implied for nested paths).

mkdir -p src/components/ui

Creates the nested directory structure src/components/ui.

rm - Remove Files or Directories

Removes (deletes) files and directories.

Usage:

rm [OPTIONS] FILE...

Common Options:

  • -r or -R: Remove directories and their contents recursively.
  • -f: Ignore nonexistent files and arguments, never prompt.
  • -i: Prompt before every removal.

Example:

rm temp.txt old_log.log

Removes the files temp.txt and old_log.log.

rm -rf build/

Caution: Recursively removes the build directory and all its contents without prompting. Use with extreme care!

cp - Copy Files and Directories

Copies files and directories from a source to a destination.

Usage:

cp [OPTIONS] SOURCE DESTINATION

Common Options:

  • -r or -R: Copy directories recursively.
  • -i: Prompt before overwriting an existing file.
  • -v: Verbose, explain what is being done.

Example:

cp report.txt documents/backup/

Copies report.txt into the documents/backup/ directory.

cp -r src/ app/components/

Recursively copies the entire src/ directory into app/components/.

mv - Move or Rename Files and Directories

Moves files or directories from one location to another, or renames them.

Usage:

mv [OPTIONS] SOURCE DESTINATION

Common Options:

  • -i: Prompt before overwriting an existing file.
  • -v: Verbose, explain what is being done.

Example:

mv draft.txt final_report.txt

Renames draft.txt to final_report.txt in the same directory.

mv config.yaml settings/

Moves the file config.yaml into the settings/ directory.

cat - Concatenate and Display Files

Concatenates files and prints them to the standard output. Often used to display the content of a single file.

Usage:

cat [OPTIONS] [FILE...]

Example:

cat README.md

Displays the content of the README.md file.

cat file1.txt file2.txt > combined.txt

Concatenates the contents of file1.txt and file2.txt and saves the result into combined.txt.

Advanced Features

find - Search for Files

Recursively searches for files in a directory hierarchy based on various criteria.

Usage:

find [PATH...] [EXPRESSION]

Common Expressions:

  • -name PATTERN: Find files whose base name matches PATTERN.
  • -type c: Find files of type c (e.g., f for regular files, d for directories).
  • -mtime n: Find files modified n*24 hours ago.
  • -exec COMMAND {} \;: Execute COMMAND for each found file.

Example:

find . -name "*.log" -type f -mtime +7

Finds all regular files ending in .log in the current directory and its subdirectories that were last modified more than 7 days ago.

find . -type f -name "temp_*" -delete

Finds all files starting with temp_ and deletes them.

grep - Search for Patterns

Searches for lines containing a given pattern within files.

Usage:

grep [OPTIONS] PATTERN [FILE...]

Common Options:

  • -i: Ignore case distinctions.
  • -v: Invert the sense of matching, to select non-matching lines.
  • -r or -R: Recursively search directories.
  • -n: Prefix each line of output with the 1-based line number within its input file.

Example:

grep "error" /var/log/syslog

Searches for lines containing the word "error" in the syslog file.

grep -ri "deprecated" src/

Recursively searches for lines containing "deprecated" (case-insensitive) in all files within the src/ directory.

Symbolic links (or symlinks) are special file types that act as pointers to other files or directories. They are created using the ln command.

Creating a symbolic link:

ln -s TARGET LINK_NAME

TARGET is the existing file or directory you want to link to.

LINK_NAME is the name of the symbolic link you are creating.

Example:

ln -s /path/to/original/file.txt /path/to/shortcut.txt

Creates a symbolic link named shortcut.txt that points to /path/to/original/file.txt.

Use ls -l to see symbolic links, indicated by an l at the beginning of the permissions string and an arrow pointing to the target.

File Permissions

Files and directories have permissions that control who can read, write, and execute them. These are typically represented in two ways: symbolic (rwxr-xr-x) and octal (755).

Understanding Permissions:

  • User (u): The owner of the file.
  • Group (g): Members of the file's group.
  • Others (o): Everyone else.
  • Read (r): Allows viewing the file's contents or listing directory contents.
  • Write (w): Allows modifying the file or creating/deleting files in a directory.
  • Execute (x): Allows running a file or entering a directory.

Changing Permissions (chmod):

The chmod command is used to change file permissions.

Usage:

chmod [OPTIONS] MODE[,MODE]... FILE...

MODE can be symbolic (e.g., u+x, go-w) or octal (e.g., 755, 644).

Example (Octal):

chmod 755 script.sh

Gives the owner read, write, and execute permissions (7), and the group and others read and execute permissions (5).

chmod 644 config.yaml

Gives the owner read and write permissions (6), and the group and others only read permissions (4).

Example (Symbolic):

chmod u+x my_program

Adds execute permission for the user (owner) to my_program.

Practical Examples

Common Scenarios

  1. Backing up a directory:
    cp -r /path/to/important_data /path/to/backup_location/$(date +%Y-%m-%d)_backup/
  2. Finding and deleting large temporary files older than 30 days:
    find /tmp -type f -size +100M -mtime +30 -exec ls -lh {} \; -delete

    (Note: The -exec ls -lh {} \; part is for demonstration to show which files will be deleted. Remove it for just deletion.)

  3. Searching for a specific configuration setting across multiple files:
    grep -r "database_host" /etc/myapp/conf/
  4. Creating a project directory structure:
    mkdir -p my_new_project/{src/main,src/utils,tests,docs,config}