Skip to content

Linux File Management Cheat Sheet

Overview

Linux file management encompasses the essential commands and techniques for navigating, organizing, and manipulating files and directories in Linux systems. This comprehensive guide covers everything from basic navigation to advanced file operations, permissions management, and file system organization strategies that every IT professional needs to master.

⚠️ Warning: File operations like rm -rf can permanently delete data. Always verify your commands and maintain backups of critical files.

Basic Navigation

bash
# Print current working directory
pwd

# List directory contents
ls
ls -la          # Detailed listing with hidden files
ls -lh          # Human-readable file sizes
ls -lt          # Sort by modification time
ls -lS          # Sort by file size

# Change directory
cd /path/to/directory
cd ~            # Go to home directory
cd -            # Go to previous directory
cd ..           # Go up one directory level
cd ../..        # Go up two directory levels

Advanced Navigation

bash
# Find files and directories
find /path -name "filename"
find . -type f -name "*.log"
find /home -user username
find . -size +100M
find . -mtime -7    # Modified in last 7 days

# Locate files using database
locate filename
updatedb           # Update locate database

# Which command shows path of executable
which command_name
whereis command_name

Directory Operations

Creating Directories

bash
# Create single directory
mkdir directory_name

# Create nested directories
mkdir -p path/to/nested/directories

# Create multiple directories
mkdir dir1 dir2 dir3

# Create directory with specific permissions
mkdir -m 755 directory_name

Removing Directories

bash
# Remove empty directory
rmdir directory_name

# Remove directory and contents (dangerous!)
rm -rf directory_name

# Remove multiple empty directories
rmdir dir1 dir2 dir3

# Interactive removal
rm -ri directory_name

File Operations

Creating Files

bash
# Create empty file
touch filename

# Create multiple files
touch file1 file2 file3

# Create file with specific timestamp
touch -t 202501011200 filename

# Create file and add content
echo "content" > filename
cat > filename << EOF
Multiple lines
of content
EOF

Copying Files

bash
# Copy file
cp source_file destination_file

# Copy file to directory
cp file /path/to/directory/

# Copy multiple files
cp file1 file2 file3 /destination/

# Copy directory recursively
cp -r source_directory destination_directory

# Copy preserving attributes
cp -p source_file destination_file

# Copy only if newer
cp -u source_file destination_file

# Interactive copy (prompt before overwrite)
cp -i source_file destination_file

# Verbose copy
cp -v source_file destination_file

Moving and Renaming Files

bash
# Move/rename file
mv old_name new_name

# Move file to directory
mv file /path/to/directory/

# Move multiple files
mv file1 file2 file3 /destination/

# Interactive move
mv -i source destination

# Verbose move
mv -v source destination

# Backup before overwrite
mv -b source destination

Deleting Files

bash
# Remove file
rm filename

# Remove multiple files
rm file1 file2 file3

# Remove with confirmation
rm -i filename

# Force remove (no confirmation)
rm -f filename

# Remove files matching pattern
rm *.tmp
rm -f /tmp/*.log

# Verbose removal
rm -v filename

File Permissions and Ownership

Understanding Permissions

bash
# View file permissions
ls -l filename

# Permission format: drwxrwxrwx
# d = directory, - = file
# rwx = read, write, execute for owner
# rwx = read, write, execute for group  
# rwx = read, write, execute for others

Changing Permissions

bash
# Numeric permissions
chmod 755 filename      # rwxr-xr-x
chmod 644 filename      # rw-r--r--
chmod 600 filename      # rw-------
chmod 777 filename      # rwxrwxrwx

# Symbolic permissions
chmod u+x filename      # Add execute for owner
chmod g-w filename      # Remove write for group
chmod o=r filename      # Set others to read only
chmod a+r filename      # Add read for all

# Recursive permissions
chmod -R 755 directory/

# Copy permissions from another file
chmod --reference=file1 file2

Changing Ownership

bash
# Change owner
chown username filename
chown username:groupname filename

# Change group only
chgrp groupname filename

# Recursive ownership change
chown -R username:groupname directory/

# Change ownership to current user
chown $USER filename

File Linking

bash
# Create hard link
ln source_file link_name

# View link count
ls -l filename

# Find all hard links to a file
find / -samefile filename
bash
# Create symbolic link
ln -s /path/to/source link_name

# Create symbolic link in different directory
ln -s /full/path/to/source /path/to/link

# View symbolic link target
ls -l link_name
readlink link_name

# Remove symbolic link
rm link_name
unlink link_name

File Content Operations

Viewing File Content

bash
# Display entire file
cat filename

# Display with line numbers
cat -n filename

# Display file page by page
less filename
more filename

# Display first lines
head filename
head -n 20 filename

# Display last lines
tail filename
tail -n 20 filename
tail -f filename        # Follow file changes

# Display specific lines
sed -n '10,20p' filename

File Comparison

bash
# Compare files line by line
diff file1 file2

# Unified diff format
diff -u file1 file2

# Compare directories
diff -r dir1 dir2

# Ignore case differences
diff -i file1 file2

# Side-by-side comparison
diff -y file1 file2

File Searching and Filtering

bash
# Search for pattern in file
grep "pattern" filename

# Case-insensitive search
grep -i "pattern" filename

# Recursive search in directories
grep -r "pattern" /path/

# Show line numbers
grep -n "pattern" filename

# Show only matching filenames
grep -l "pattern" *.txt

# Invert match (show non-matching lines)
grep -v "pattern" filename

# Extended regular expressions
grep -E "pattern1|pattern2" filename

File Type and Information

bash
# Determine file type
file filename

# Display file statistics
stat filename

# Calculate file checksums
md5sum filename
sha256sum filename

# Count lines, words, characters
wc filename
wc -l filename          # Lines only
wc -w filename          # Words only
wc -c filename          # Characters only

Archive and Compression

Creating Archives

bash
# Create tar archive
tar -cf archive.tar files/

# Create compressed tar archive
tar -czf archive.tar.gz files/
tar -cjf archive.tar.bz2 files/

# Create zip archive
zip -r archive.zip files/

Extracting Archives

bash
# Extract tar archive
tar -xf archive.tar

# Extract compressed tar archive
tar -xzf archive.tar.gz
tar -xjf archive.tar.bz2

# Extract to specific directory
tar -xzf archive.tar.gz -C /destination/

# Extract zip archive
unzip archive.zip

Archive Information

bash
# List archive contents
tar -tf archive.tar
tar -tzf archive.tar.gz
unzip -l archive.zip

# Verbose extraction
tar -xvf archive.tar

Disk Usage and Space Management

Checking Disk Usage

bash
# Display disk usage by directory
du -h directory/
du -sh directory/       # Summary only
du -ah directory/       # Include files

# Display largest directories
du -h | sort -hr | head -10

# Check filesystem usage
df -h
df -i                   # Inode usage

Finding Large Files

bash
# Find files larger than size
find / -size +100M
find / -size +1G

# Find largest files in directory
find . -type f -exec ls -lh {} \; | sort -k5 -hr | head -10

# Find files by age
find / -mtime +30       # Older than 30 days
find / -atime -7        # Accessed in last 7 days

File System Monitoring

Real-time Monitoring

bash
# Monitor file changes
inotifywait -m /path/to/watch

# Monitor directory recursively
inotifywait -mr /path/to/watch

# Watch file modifications
watch -n 1 'ls -la /path/to/file'

File System Information

bash
# Display mounted filesystems
mount
findmnt

# Show filesystem type
lsblk -f
blkid

# Check filesystem
fsck /dev/device

Troubleshooting

Permission Issues

bash
# Fix common permission problems
chmod 644 /path/to/files/*
chmod 755 /path/to/directories/

# Reset ownership to user
chown -R $USER:$USER /path/to/directory/

# Find files with specific permissions
find / -perm 777
find / -perm -4000      # Find SUID files

Recovery Operations

bash
# Recover deleted files (if possible)
lsof | grep deleted

# Check for file system errors
dmesg | grep -i error

# Force filesystem check on next boot
touch /forcefsck

Resources


This cheat sheet provides comprehensive file management commands for Linux systems. Always test commands in a safe environment before using them on production systems.