Creating, copying, moving, and deleting files are the most common operations you'll perform on any Linux system. But doing them efficiently—especially when working with hundreds of files—requires mastering wildcards and understanding the subtle differences between commands. In this hands-on guide, you'll learn the file operations you'll use dozens of times every day, plus the safety practices that separate professionals from amateurs.
1. Learning Objectives
By the end of this lesson, you will be able to:
- Create files with touch and directories with mkdir
- Copy files and directories using cp with the -r flag
- Move and rename files with mv
- Delete files safely and understand why rm -rf is dangerous
- Use wildcards (*, ?, []) to work with multiple files at once
- Create a practical script to organize messy folders
2. Why This Matters (Real-World Scenario)
Real-world scenario: Your monitoring system just alerted you that your server's disk is 95% full. You need to find and delete old log files, archive completed reports, and move backup files to another location. You have 10 minutes before the server crashes.
Knowing file operations and wildcards allows you to:
- Delete all .log files older than 30 days with one command
- Move 500 images from Downloads/ to images/ instantly
- Rename a pattern of files (e.g., report_v1.txt → report_v2.txt) in seconds
This is not theoretical—you will face this exact situation.
3. Core Concepts: File Operations
Creating Files and Directories
Touch Command (touch)
Creates an empty file or updates a file's timestamp if it already exists.
# Create a single file
touch file1.txt
# Create multiple files at once
touch file1.txt file2.txt file3.txt
# Create files with spaces (not recommended but possible)
touch "my important file.txt"
Make Directory (mkdir)
Creates directories (folders).
# Create a single directory
mkdir projects
# Create multiple directories
mkdir docs images backups
# Create nested directories (parents) - THE MAGIC FLAG
mkdir -p projects/2025/reports/q1
# This creates projects, then 2025 inside it, then reports, then q1
# Verify it worked
ls -R projects/
$ mkdir -p projects/2025/reports/q1
$ ls -R projects/
projects/:
2025
projects/2025:
reports
projects/2025/reports:
q1
projects/2025/reports/q1:
Copying Files and Directories
Copy Command (cp)
Copies files or directories from source to destination.
# Copy a file to a new name (same directory)
cp file1.txt file1_backup.txt
# Copy a file to another directory
cp file1.txt ../backups/
# Copy multiple files to a directory (RECURSIVE FLAG IS CRUCIAL)
cp file1.txt file2.txt file3.txt documents/
# Copy a directory AND ALL ITS CONTENTS - MUST use -r
cp -r projects/ projects_backup/
# Copy with preservation of timestamps, ownership, permissions
cp -rp projects/ projects_backup/
# Copy interactively (asks before overwriting) - SAFE MODE
cp -i file1.txt file1_backup.txt
$ cp -i important.txt important_backup.txt
cp: overwrite 'important_backup.txt'? n
$ # Enter 'y' to overwrite, 'n' to skip
Moving and Renaming Files
Move Command (mv)
Moves files/directories or renames them (moving within same directory = rename).
# Rename a file (move within same directory)
mv oldname.txt newname.txt
# Move a file to another directory
mv file1.txt documents/
# Move multiple files to a directory
mv file1.txt file2.txt file3.txt archive/
# Move a directory (no -r needed! mv is simpler than cp)
mv projects/ archive/projects/
# Move interactively (asks before overwriting)
mv -i important.txt documents/important.txt
$ ls
file1.txt file2.txt documents/
$ mv file1.txt file1_renamed.txt
$ ls
file1_renamed.txt file2.txt documents/
Deleting Files (Handle With Extreme Care)
Remove Command (rm)
Deletes files or directories. THERE IS NO UNDO. THIS IS PERMANENT.
# Delete a single file
rm file1.txt
# Delete multiple files
rm file1.txt file2.txt file3.txt
# Delete interactively (ALWAYS USE THIS FOR IMPORTANT FILES)
rm -i file1.txt
# Prompts: "rm: remove regular file 'file1.txt'?"
# Delete a directory AND all contents (RECURSIVE + FORCE = DANGEROUS)
rm -r directory_name/
# THE NUCLEAR OPTION (use with EXTREME caution)
rm -rf directory_name/
# -r = recursive (go inside directories)
# -f = force (don't ask, ignore errors)
4. Mastering Wildcards (Work With Multiple Files)
Wildcards are patterns that match multiple filenames. They make you 10x more efficient.
The Asterisk (*) - Match Any Characters
# List all .txt files
ls *.txt
# Delete all .log files
rm *.log
# Copy all files starting with 'report' to backup/
cp report* backup/
# Move all files ending with '.jpg' to images/
mv *.jpg images/
# Count all Python files
wc -l *.py
$ ls
file1.txt file2.txt file3.log file4.log report.pdf report.txt
$ ls *.txt
file1.txt file2.txt
$ ls *.log
file3.log file4.log
$ ls report*
report.pdf report.txt
The Question Mark (?) - Match Exactly One Character
# Match file1.txt, file2.txt, but NOT file10.txt
ls file?.txt
# Match image1.jpg, image2.jpg, image3.jpg
ls image?.jpg
# Find all 3-character file names
ls ???
$ ls
file1.txt file2.txt file10.txt image1.jpg image2.jpg image10.jpg
$ ls file?.txt
file1.txt file2.txt
$ ls image?.jpg
image1.jpg image2.jpg
Character Sets ([ ]) - Match Specific Characters
# Match file1.txt, file2.txt, but NOT file3.txt
ls file[12].txt
# Match any file with a digit
echo file[0-9].txt
# Match any file with a lowercase letter
echo [a-z].txt
# Match any file with uppercase A,B, or C
echo [ABC]*
# Exclude characters (^ means NOT)
ls [^0-9]* # All files NOT starting with a digit
$ ls
file1.txt file2.txt file3.txt file4.txt config.ini
$ ls file[12].txt
file1.txt file2.txt
$ ls file[1-4].txt
file1.txt file2.txt file3.txt file4.txt
$ ls [c]*
config.ini
5. Hands-on Practice Lab
Setup Your Practice Environment
# Create a practice directory
mkdir ~/linux-file-lab
cd ~/linux-file-lab
# Create a messy set of files to practice with
mkdir messy downloads archive images documents logs
touch messy/file1.txt messy/file2.txt messy/file3.txt
touch messy/report1.log messy/report2.log messy/report3.log
touch messy/image1.jpg messy/image2.png messy/photo1.jpg messy/photo2.png
touch downloads/software.deb downloads/file.pdf downloads/README.md
touch logs/error_2024_01.log logs/error_2024_02.log logs/access.log
# Verify your lab
ls -R
Exercise 1: Copy Files
# Exercise: Copy all .txt files from messy/ to documents/
cp messy/*.txt documents/
# Exercise: Copy all .log files to logs/archive/ (create archive first)
mkdir -p logs/archive
cp messy/*.log logs/archive/
# Exercise: Copy all .jpg files to images/ (create images if needed)
mkdir -p images
cp messy/*.jpg images/
Exercise 2: Move Files
# Exercise: Move all .png files from messy/ to images/
mv messy/*.png images/
# Exercise: Move all files starting with 'report' to archive/
mkdir archive
mv messy/report* archive/
# Exercise: Move all .log files that contain 'error' to logs/error/
mkdir -p logs/error
mv messy/*error*.log logs/error/
Exercise 3: Delete Files (SAFELY)
# Exercise: Delete all .tmp files (create some first)
touch messy/test.tmp messy/cleanup.tmp messy/data.tmp
# Delete them
rm -i messy/*.tmp # Use -i to confirm each deletion
# Exercise: Delete empty directories
rmdir empty_directory # Only works if directory is empty
# Exercise: Delete a directory with contents (use with caution)
rm -r messy/ # This deletes the whole messy folder
6. Real-World Project: Messy Downloads Organizer
Goal: Create a script that automatically organizes a messy Downloads folder by file type.
Requirements:
- Images (.jpg, .png, .gif) → images/
- Documents (.pdf, .docx, .txt) → documents/
- Archives (.zip, .tar.gz) → archives/
- Scripts (.sh, .py) → scripts/
- Logs (.log) → logs/
#!/bin/bash
# File Organizer Script - Save as organize.sh
echo "Starting file organization..."
# Create directories if they don't exist
mkdir -p ~/Downloads/{images,documents,archives,scripts,logs}
# Organize by file extension
mv ~/Downloads/*.jpg ~/Downloads/images/ 2>/dev/null
mv ~/Downloads/*.png ~/Downloads/images/ 2>/dev/null
mv ~/Downloads/*.gif ~/Downloads/images/ 2>/dev/null
mv ~/Downloads/*.pdf ~/Downloads/documents/ 2>/dev/null
mv ~/Downloads/*.docx ~/Downloads/documents/ 2>/dev/null
mv ~/Downloads/*.txt ~/Downloads/documents/ 2>/dev/null
mv ~/Downloads/*.zip ~/Downloads/archives/ 2>/dev/null
mv ~/Downloads/*.tar.gz ~/Downloads/archives/ 2>/dev/null
mv ~/Downloads/*.sh ~/Downloads/scripts/ 2>/dev/null
mv ~/Downloads/*.py ~/Downloads/scripts/ 2>/dev/null
mv ~/Downloads/*.log ~/Downloads/logs/ 2>/dev/null
echo "Organization complete!"
ls -la ~/Downloads/
To run the script:
# Make script executable
chmod +x organize.sh
# Run the script (but test in your practice directory first!)
./organize.sh
7. Common Errors & Solutions
8. Summary Checklist
9. Practice Exercise (Try Without Looking)
Challenge: Create the following structure using a single command:
project/ ├── src/ │ ├── main.py │ ├── utils.py │ └── test.py ├── docs/ │ ├── README.md │ └── setup.md └── data/ ├── input.csv └── output.csv
Hints: Use mkdir -p for directories, touch for files
10. Next Steps
Next lesson: Linux Lesson 1.3: File Permissions & Ownership
You'll learn to:
- Understand read, write, execute permissions (rwx)
- Use chmod 755 vs chmod 644
- Change ownership with chown
- Set the SUID, SGID, and Sticky bits
Practice resources:
- Linux Journey - File Operations
- ExplainShell - See what each flag does
Common pitfalls to avoid before Lesson 3:
- ❌ Forgetting -r with cp on directories
- ❌ Using rm -rf without checking your current directory first (pwd is your friend!)
- ❌ Using spaces in filenames (use underscores instead: my_file.txt)
- ❌ Not using -i flag when experimenting with rm
Comments (0)
This is exactly what I needed! The initContainer approach solved our migration issues completely. Thanks for the detailed guide!
ReplyLeave a Comment