Effective file management is crucial for productivity in Linux. Whether you're organizing source code, managing configuration files, or handling large datasets, understanding Linux's file management capabilities will transform how you work with your system.
Linux follows a hierarchical file system structure that might seem different if you're coming from Windows, but it's logical and consistent once you understand the principles.
- Everything is a file: In Linux, directories, devices, and processes are all treated as files
File.txt
and file.txt
are different files/
.
are hidden by defaultWildcards are powerful pattern-matching tools that let you work with multiple files at once:
# Basic wildcards
ls *.txt # All files ending with .txt
ls file*.txt # Files starting with "file" and ending with .txt
ls file?.txt # Single character wildcard (file1.txt, filea.txt)
ls file[1-3].txt # Files with numbers 1, 2, or 3
ls file[abc].txt # Files with letters a, b, or c
ls *.{jpg,png,gif} # Multiple extensions
# Copy all images to a backup directory
cp *.{jpg,jpeg,png,gif} ~/Pictures/backup/
# Remove all temporary files
rm *.tmp *.temp
# List all configuration files
ls *.conf *.cfg *.ini
# Move all documents to Documents folder
mv *.{doc,docx,pdf,txt} ~/Documents/
# Count all source code files
wc -l *.{c,cpp,h,py,js}
# Preserve timestamps, permissions, and ownership
cp -p source.txt destination.txt
# Copy recursively with all attributes
cp -a source_directory/ destination_directory/
# Interactive copy (confirm overwrites)
cp -i *.txt ~/backup/
# Verbose copy (show what's being copied)
cp -v *.log /var/log/backup/
# Move with backup (if destination exists)
mv --backup=numbered file.txt ~/Documents/
# Interactive move (confirm overwrites)
mv -i *.txt ~/Documents/
# Move only if source is newer
mv -u *.txt ~/Documents/
The find
command is incredibly powerful for locating files based on various criteria:
find [path] [expression] [action]
# Find files by exact name
find /home/username -name "config.txt"
# Case-insensitive search
find /home/username -iname "CONFIG.TXT"
# Find files with patterns
find /home/username -name "*.py"
find /home/username -name "test*"
# Find files NOT matching pattern
find /home/username -not -name "*.tmp"
# Find only directories
find /home/username -type d
# Find only regular files
find /home/username -type f
# Find symbolic links
find /home/username -type l
# Find executable files
find /home/username -type f -executable
# Files larger than 100MB
find /home/username -size +100M
# Files smaller than 1KB
find /home/username -size -1k
# Files exactly 50MB
find /home/username -size 50M
# Empty files
find /home/username -empty
# Modified in last 7 days
find /home/username -mtime -7
# Modified more than 30 days ago
find /home/username -mtime +30
# Accessed in last 24 hours
find /home/username -atime -1
# Changed in last hour
find /home/username -ctime -1
# Large image files modified recently
find ~/Pictures -name "*.jpg" -size +5M -mtime -7
# Python files in project directories
find ~/projects -name "*.py" -type f
# Executable files owned by current user
find /usr/local/bin -type f -executable -user $(whoami)
# Configuration files not accessed in 6 months
find /etc -name "*.conf" -atime +180
# Delete all .tmp files
find /tmp -name "*.tmp" -delete
# Execute command on each found file
find ~/Documents -name "*.txt" -exec wc -l {} \;
# Move all .log files to archive
find /var/log -name "*.log" -exec mv {} /archive/ \;
# Change permissions of all .sh files
find ~/scripts -name "*.sh" -exec chmod +x {} \;
# List details of large files
find /home/username -size +100M -exec ls -lh {} \;
The locate
command searches a pre-built database, making it much faster than find
:
# Update the locate database (run as root)
sudo updatedb
# Find files containing "python" in the name
locate python
# Case-insensitive search
locate -i PYTHON
# Limit results to 10
locate -l 10 "*.conf"
# Count matching files
locate -c "*.py"
# Show only existing files (database might be outdated)
locate -e "config"
# Find the location of a command
which python3
which gcc
# Find command, source, and manual locations
whereis python3
whereis gcc
# Find all instances of a command
which -a python
~/projects/
├── web-development/
│ ├── project1/
│ │ ├── src/
│ │ ├── tests/
│ │ ├── docs/
│ │ └── config/
│ └── project2/
├── python-scripts/
├── learning/
└── archived/
~/admin/
├── scripts/
│ ├── backup/
│ ├── monitoring/
│ └── maintenance/
├── configs/
├── logs/
└── documentation/
- Use lowercase letters and underscores or hyphens
my_file.txt
instead of my file.txt
)# Good naming examples
backup_2025-07-19.tar.gz
web_server_config.conf
user_management_script.py
project_documentation_v2.md
# Avoid these patterns
My File.txt # Spaces
CONFIG.TXT # All caps
temp123 # Non-descriptive
file (copy) # Parentheses
# Create tar archive
tar -cvf archive.tar directory/
# Create compressed tar archive
tar -czvf archive.tar.gz directory/
# Create zip archive
zip -r archive.zip directory/
# Extract tar archive
tar -xvf archive.tar
# Extract compressed tar archive
tar -xzvf archive.tar.gz
# Extract zip archive
unzip archive.zip
# List archive contents without extracting
tar -tvf archive.tar.gz
zip -l archive.zip
# Compare two files
diff file1.txt file2.txt
# Side-by-side comparison
diff -y file1.txt file2.txt
# Compare directories
diff -r directory1/ directory2/
# Create patch file
diff -u original.txt modified.txt > changes.patch
# Apply patch
patch original.txt < changes.patch
# Generate MD5 checksum
md5sum file.txt
# Generate SHA256 checksum
sha256sum file.txt
# Verify checksums
md5sum -c checksums.md5
sha256sum -c checksums.sha256
# Compare two files by checksum
md5sum file1.txt file2.txt
# Create symbolic link
ln -s /path/to/original /path/to/link
# Create symbolic link in current directory
ln -s /etc/hosts hosts_link
# Link to directory
ln -s ~/Documents/projects projects_link
# Create hard link
ln /path/to/original /path/to/hardlink
# Show file link count
ls -l filename
# Create convenient shortcuts
ln -s ~/Documents/important_project ~/Desktop/project_shortcut
ln -s /var/log/nginx/access.log ~/logs/nginx_access
# Link configuration files
ln -s ~/.config/app/config.conf ~/project/config.conf
# Create backup-friendly links
ln important_file.txt important_file_backup.txt
# Install inotify-tools (Ubuntu/Debian)
sudo apt install inotify-tools
# Watch for file changes
inotifywait -m ~/Documents/
# Watch specific events
inotifywait -m -e modify,create,delete ~/project/
# Recursive monitoring
inotifywait -r -m ~/project/
# Follow file changes in real-time
tail -f /var/log/syslog
# Follow multiple files
tail -f /var/log/syslog /var/log/auth.log
# Follow with line numbers
tail -n +1 -f file.txt
# View detailed permissions
ls -la filename
# Change permissions
chmod 755 script.sh # rwxr-xr-x
chmod u+x script.sh # Add execute for user
chmod g-w file.txt # Remove write for group
chmod a+r file.txt # Add read for all
# Change ownership
chown user:group file.txt
chown user file.txt
chgrp group file.txt
# Create a development project structure
mkdir -p ~/projects/web-app/{src,tests,docs,config,logs}
cd ~/projects/web-app
# Create sample files
touch src/{index.html,app.js,style.css}
touch tests/{test_app.js,test_utils.js}
touch docs/{README.md,API.md}
touch config/{dev.conf,prod.conf}
# Verify structure
tree . # or ls -la if tree isn't installed
# Create test files
mkdir ~/file_exercise
cd ~/file_exercise
# Create various file types
touch document{1..5}.txt
touch image{1..3}.{jpg,png}
touch script{1..2}.{py,sh}
touch config{1..2}.{conf,cfg}
# Practice searches
find . -name "*.txt"
find . -name "image*"
find . -type f -name "*.py" -o -name "*.sh"
ls *.{jpg,png}
# Create backup directory
mkdir ~/backup
# Copy all text files
cp *.txt ~/backup/
# Rename all .cfg files to .config
for file in *.cfg; do
mv "$file" "${file%.cfg}.config"
done
# Or using rename command (if available)
rename 's/\.cfg$/.config/' *.cfg
#!/bin/bash
# backup_important.sh
BACKUP_DIR="$HOME/backups/$(date +%Y-%m-%d)"
mkdir -p "$BACKUP_DIR"
# Backup important directories
cp -r ~/Documents "$BACKUP_DIR/"
cp -r ~/projects "$BACKUP_DIR/"
cp ~/.bashrc "$BACKUP_DIR/"
echo "Backup completed to $BACKUP_DIR"
#!/bin/bash
# cleanup_temp.sh
# Remove temporary files older than 7 days
find /tmp -name "*.tmp" -mtime +7 -delete
find ~/Downloads -name "*.tmp" -mtime +7 -delete
# Clean up empty directories
find ~/projects -type d -empty -delete
echo "Cleanup completed"
#!/bin/bash
# organize_downloads.sh
DOWNLOADS="$HOME/Downloads"
cd "$DOWNLOADS"
# Organize by file type
mkdir -p Images Documents Archives Scripts
mv *.{jpg,jpeg,png,gif,bmp} Images/ 2>/dev/null
mv *.{pdf,doc,docx,txt,odt} Documents/ 2>/dev/null
mv *.{zip,tar,gz,rar,7z} Archives/ 2>/dev/null
mv *.{sh,py,pl,rb} Scripts/ 2>/dev/null
echo "Downloads organized"
- Clean up temporary files weekly
- Regular automated backups
- Consistent naming conventions
- Appropriate file permissions
# Check file permissions
ls -la problematic_file
# Fix common permission issues
chmod 644 regular_file # Read/write for owner, read for others
chmod 755 executable_file # Execute permissions for all
chmod 600 private_file # Read/write for owner only
# Check disk usage
df -h
# Find large files
find / -size +100M 2>/dev/null | head -20
# Check directory sizes
du -sh */ | sort -hr
# Clean package cache (Ubuntu/Debian)
sudo apt clean
# Attempt to recover deleted files (install testdisk)
sudo apt install testdisk
sudo photorec
# Check for file fragments
sudo grep -a -B 25 -A 25 'unique_text_from_file' /dev/sda1
- Master wildcards for efficient bulk operations
find
for complex searches and locate
for quick lookups# Wildcards
* # Match any characters
? # Match single character
[abc] # Match any of a, b, c
[0-9] # Match any digit
{jpg,png} # Match jpg or png
# Find Operations
find . -name "pattern" # Find by name
find . -type f # Find files
find . -size +10M # Find large files
find . -mtime -7 # Modified in last 7 days
find . -exec command {} \; # Execute on results
# Bulk Operations
cp *.txt backup/ # Copy all txt files
mv *.log archives/ # Move all log files
rm *.tmp # Remove temp files
chmod +x *.sh # Make scripts executable
# Archives
tar -czvf archive.tar.gz directory/ # Create archive
tar -xzvf archive.tar.gz # Extract archive
zip -r archive.zip directory/ # Create zip
unzip archive.zip # Extract zip
With these file management skills, you'll be able to handle large projects, maintain organized systems, and work efficiently with files and directories in Linux. The key is practice and gradually incorporating these techniques into your daily workflow.
---
This is Part 4 of our comprehensive Linux mastery series.
Previous: File System Structure - Understand Linux directory organization
Next: Permissions & Security - Master file permissions and access control
Beginner Foundation:
Ready for Security? Continue with permissions to learn how to control access to your files and directories!
---
Next up: Understanding Linux file permissions and security - learn how to control access to your files and directories.