Commands for creating, deleting, moving, and copying 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 informationls -lh - List files with detailed information and human-readable sizesls -lt - List files sorted by modification time with detailed informationls -la | grep "^d" - List only directories (using pipe and grep)ls -la | grep "^-" | sort -k 5 -n - List regular files sorted by sizefind . -type f -name "*.log" | xargs ls -lh - List detailed information for all log filesls -la --color=auto - List files with colors based on file typels -R - List files recursively including subdirectories
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 formattree -L 2 - Display directory structure up to 2 levels deeptree -d - Display only directories in tree formattree -a - Display all files and directories including hidden onestree -sh - Display file sizes in human-readable formattree -C | less -R - Display colorized output using a pagertree -P "*.py" - Display only Python filestree -I "node_modules|.git|*.tmp" - Exclude specific directories and filestree -J - Output in JSON formattree --du -h - Display disk usage for each directory in human-readable format
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 directorycd ~ - Change to home directorycd .. - Move up one level to parent directorycd - - Return to previous directorycd $(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 foundfor 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
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 directorymkdir -p parent/child/grandchild - Create hierarchical directory structure at oncemkdir -m 755 secure_dir - Create directory with permissions 755mkdir dir1 dir2 dir3 - Create multiple directories at oncemkdir -p project/{src,lib,doc,test} - Create multiple subdirectories using brace expansionmkdir -p project/{src/{main,test}/{java,resources},lib,doc} - Create complex directory structure at oncefind . -type f -name "*.txt" | xargs dirname | sort | uniq | xargs mkdir -p - Create directory structure matching paths where text files exist
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 filerm -r directory - Remove a directory and its contentsrm -rf directory - Force remove a directory and its contents without confirmation (use with caution)rm -i *.txt - Prompt for confirmation before removing each text filefind . -name "*.tmp" -type f -delete - Find and remove all .tmp files recursivelyfind . -name "*.log" -type f -mtime +30 -exec rm {} \; - Remove log files older than 30 daysfind . -type f -size +100M -exec rm -i {} \; - Interactively remove files larger than 100MBls | grep -E "^temp.*\.txt$" | xargs rm - Remove text files starting with "temp" using piperm -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 /*.
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 filecp -r source_dir destination_dir - Copy a directory and its contents recursivelycp -rp source_dir destination_dir - Copy a directory preserving attributescp -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 directoryfind . -mtime -7 -type f | xargs cp -t /backup/recent/ - Copy files modified in the last 7 days to backupcp --parents src/config/app.conf /backup/ - Copy preserving directory structurels *.log | xargs -I{} cp {} backup/{}.bak - Copy all log files to backup with changed extension
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 filemv file.txt /path/to/directory/ - Move a file to another directorymv directory new_directory - Rename or move a directorymv -i *.txt /backup/ - Move all text files with confirmation before overwritingmv -u *.log /archive/ - Move only updated log filesfind . -name "*.tmp" -exec mv {} /tmp/ \; - Find and move all temporary files to /tmp directoryfind . -type f -name "*.jpg" | xargs -I{} mv {} photos/ - Move all JPG files to photos directoryfor f in *.txt; do mv "$f" "${f%.txt}.bak"; done - Change extension of all text files from .txt to .bakls -1 | grep -E "^[0-9]+" | xargs -I{} mv {} numbered/{} - Move files starting with numbers to numbered directory
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 existstouch -a file.txt - Update only the access time of a filetouch -t 202507190200 file.txt - Set timestamp to July 19, 2025, 2:00 AMtouch file{1..10}.txt - Create 10 files from file1.txt to file10.txt at oncefind . -name "*.log" -exec touch {} \; - Update timestamps of all log files to current timefind . -type f -mtime +30 | xargs touch - Update timestamps of files older than 30 daysls -1 *.txt | xargs -I{} touch -r reference.txt {} - Set timestamps of all text files to match reference.txtfor 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 monthfind . -type f -name "*.bak" | xargs touch -d "$(date -d '-1 year')" - Set timestamps of all backup files to 1 year ago