Linux File Operation Commands Guide

File Operation Commands

Commands for creating, deleting, moving, and copying files and directories.

ls - List Files and Directories

Displays the contents of a directory.

Option Description
-l Display detailed information (permissions, owner, size, modification time, etc.)
-a Show all files including hidden files (files starting with .)
-h Display file sizes in human-readable format (KB, MB, GB, etc.)
-t Sort by modification time
-r Reverse the sort order

Examples:

ls -la - List all files including hidden files with detailed information
ls -lh - List files with detailed information and human-readable sizes
ls -lt - List files sorted by modification time with detailed information
ls -la | grep "^d" - List only directories (using pipe and grep)
ls -la | grep "^-" | sort -k 5 -n - List regular files sorted by size
find . -type f -name "*.log" | xargs ls -lh - List detailed information for all log files
ls -la --color=auto - List files with colors based on file type
ls -R - List files recursively including subdirectories

tree - Display Directory Structure in Tree Format

Displays directory and file hierarchy in a visual tree format.

Option Description
-a Show all files including hidden files
-d Show only directories (no files)
-L [level] Limit display to specified level of depth
-p Show file permissions after filenames
-s Show file sizes
-h Show file sizes in human-readable format
-C Colorize output for files and directories
-P [pattern] Show only files that match the specified pattern
-I [pattern] Exclude files that match the specified pattern
--du Show disk usage for each directory

Examples:

tree - Display the current directory structure in tree format
tree -L 2 - Display directory structure up to 2 levels deep
tree -d - Display only directories in tree format
tree -a - Display all files and directories including hidden ones
tree -sh - Display file sizes in human-readable format
tree -C | less -R - Display colorized output using a pager
tree -P "*.py" - Display only Python files
tree -I "node_modules|.git|*.tmp" - Exclude specific directories and files
tree -J - Output in JSON format
tree --du -h - Display disk usage for each directory in human-readable format

cd - Change Directory

Changes the current working directory.

Argument/Option Description
directory path Path to the target directory
~ Change to home directory
.. Change to parent directory
- Change to previous directory

Examples:

cd /usr/local/bin - Change to specified directory
cd ~ - Change to home directory
cd .. - Move up one level to parent directory
cd - - Return to previous directory
cd $(find . -name "target" -type d | head -1) - Change to first directory named "target"
cd "$(dirname "$(find . -name "*.conf" | head -1)")" - Change to directory containing first .conf file found
for dir in */; do (cd "$dir" && pwd && ls -la | head -5); done - Enter each subdirectory and show contents (using subshell)
OLDPWD=/tmp && cd - - Set OLDPWD variable and change to that directory

mkdir - Make Directory

Creates new directories.

Option Description
-p Create parent directories as needed (hierarchical directory creation)
-m Set permissions for created directories
-v Display information about created directories (verbose mode)

Examples:

mkdir new_directory - Create a new directory
mkdir -p parent/child/grandchild - Create hierarchical directory structure at once
mkdir -m 755 secure_dir - Create directory with permissions 755
mkdir dir1 dir2 dir3 - Create multiple directories at once
mkdir -p project/{src,lib,doc,test} - Create multiple subdirectories using brace expansion
mkdir -p project/{src/{main,test}/{java,resources},lib,doc} - Create complex directory structure at once
find . -type f -name "*.txt" | xargs dirname | sort | uniq | xargs mkdir -p - Create directory structure matching paths where text files exist

rm - Remove Files and Directories

Deletes files and directories.

Option Description
-r, -R Remove directories and their contents recursively
-f Force removal without confirmation
-i Prompt for confirmation before removal
-v Display progress of removal

Examples:

rm file.txt - Remove a file
rm -r directory - Remove a directory and its contents
rm -rf directory - Force remove a directory and its contents without confirmation (use with caution)
rm -i *.txt - Prompt for confirmation before removing each text file
find . -name "*.tmp" -type f -delete - Find and remove all .tmp files recursively
find . -name "*.log" -type f -mtime +30 -exec rm {} \; - Remove log files older than 30 days
find . -type f -size +100M -exec rm -i {} \; - Interactively remove files larger than 100MB
ls | grep -E "^temp.*\.txt$" | xargs rm - Remove text files starting with "temp" using pipe
rm -v !(*.txt|*.pdf) - Remove all files except text and PDF files (using extended globbing)

Warning: rm -rf is a very powerful command that can delete files irreversibly. If used incorrectly, it can delete important or system files. Never run commands like rm -rf / or rm -rf /*.

cp - Copy Files and Directories

Copies files and directories.

Option Description
-r, -R Copy directories recursively
-i Prompt for confirmation before overwriting
-u Copy only when the source file is newer than the destination file or when the destination file doesn't exist
-v Display progress of copying
-p Preserve file attributes (permissions, timestamps, etc.)

Examples:

cp file.txt backup.txt - Copy a file
cp -r source_dir destination_dir - Copy a directory and its contents recursively
cp -rp source_dir destination_dir - Copy a directory preserving attributes
cp -a source_dir destination_dir - Copy in archive mode (equivalent to -dpr, preserves symlinks and special files)
cp -u *.txt backup/ - Copy only updated files (useful for backups)
find . -name "*.jpg" -exec cp {} /backup/images/ \; - Find all JPG files and copy them to backup directory
find . -mtime -7 -type f | xargs cp -t /backup/recent/ - Copy files modified in the last 7 days to backup
cp --parents src/config/app.conf /backup/ - Copy preserving directory structure
ls *.log | xargs -I{} cp {} backup/{}.bak - Copy all log files to backup with changed extension

mv - Move/Rename Files and Directories

Moves or renames files and directories.

Option Description
-i Prompt for confirmation before overwriting
-f Force move/rename without confirmation
-u Move only when the source file is newer than the destination file or when the destination file doesn't exist
-v Display progress of moving/renaming

Examples:

mv file.txt new_name.txt - Rename a file
mv file.txt /path/to/directory/ - Move a file to another directory
mv directory new_directory - Rename or move a directory
mv -i *.txt /backup/ - Move all text files with confirmation before overwriting
mv -u *.log /archive/ - Move only updated log files
find . -name "*.tmp" -exec mv {} /tmp/ \; - Find and move all temporary files to /tmp directory
find . -type f -name "*.jpg" | xargs -I{} mv {} photos/ - Move all JPG files to photos directory
for f in *.txt; do mv "$f" "${f%.txt}.bak"; done - Change extension of all text files from .txt to .bak
ls -1 | grep -E "^[0-9]+" | xargs -I{} mv {} numbered/{} - Move files starting with numbers to numbered directory

touch - Create Files/Change Timestamps

Creates empty files or updates timestamps of existing files.

Option Description
-a Change only the access time
-m Change only the modification time
-c Do not create new files if they don't exist
-t [[CC]YY]MMDDhhmm[.ss] Set timestamp to specified time

Examples:

touch new_file.txt - Create an empty file or update timestamp if it exists
touch -a file.txt - Update only the access time of a file
touch -t 202507190200 file.txt - Set timestamp to July 19, 2025, 2:00 AM
touch file{1..10}.txt - Create 10 files from file1.txt to file10.txt at once
find . -name "*.log" -exec touch {} \; - Update timestamps of all log files to current time
find . -type f -mtime +30 | xargs touch - Update timestamps of files older than 30 days
ls -1 *.txt | xargs -I{} touch -r reference.txt {} - Set timestamps of all text files to match reference.txt
for i in {1..12}; do touch -d "2025-$i-01" "report-$(printf '%02d' $i).txt"; done - Create report files with timestamps for the first day of each month
find . -type f -name "*.bak" | xargs touch -d "$(date -d '-1 year')" - Set timestamps of all backup files to 1 year ago