Linux File Operations Basics Introduction
In Linux professional work, file operations are the most frequently used skills. In this basics edition, you can safely learn basic commands like cp, mv, rm, cat along with dangerous pitfalls and common mistakes.
β οΈ Important Notice
Commands covered in this article include dangerous operations that permanently delete data. Before working in production environments, practice thoroughly in a safe practice environment.
Table of Contents
1. cp - Safe and Reliable File Copying
The cp command stands for "copy" and copies files or directories. In professional work, it's a critical command frequently used for backup creation and environment setup.
Basic Syntax
cp [options] source destination
π Basic File Copy
$ cp original.txt backup.txt
Copy original.txt as backup.txt
π Copy to Directory
$ cp document.pdf ~/Documents/
Copy document.pdf to Documents directory
β Essential Options for Professional Work
π -i (interactive): Overwrite Confirmation
Essential option in professional work. Asks for confirmation before overwriting existing files.
$ cp -i source.txt target.txt overwrite target.txt? (y/n)
π‘ Pro Tip: Set alias cp='cp -i' to always get confirmation
π -r (recursive): Recursive Directory Copy
Copies directories and all their contents.
$ cp -r project_folder/ backup_folder/
Note: Copying large directories takes time. Check the size beforehand.
$ du -sh project_folder/
π -v (verbose): Detailed Display
Displays copy operation details. You can see what's being copied.
$ cp -v *.txt backup/ 'file1.txt' -> 'backup/file1.txt' 'file2.txt' -> 'backup/file2.txt'
π -p (preserve): Preserve Attributes
Preserves timestamps and permissions when copying. Important for backups.
$ cp -p important.log backup_important.log
π― Common Combinations in Professional Work
π Safe File Backup
$ cp -ivp config.conf config.conf.backup
Explanation: Safe backup with confirmation(-i), verbose(-v), preserve attributes(-p)
π Duplicate Entire Project
$ cp -rvp project/ project_backup/
Explanation: Copy entire project with recursive(-r), verbose(-v), preserve(-p)
π― Create Configuration Template
$ cp sample.conf.template actual.conf
Explanation: Create actual configuration file from template
β οΈ Common Mistakes and Pitfalls
β Mistake: Forgetting -r for directory copy
$ cp folder/ backup/ cp: -r not specified; omitting directory 'folder/'
Correct: cp -r folder/ backup/
β Mistake: Destroying important files without overwrite confirmation
$ cp temp.txt important.txt # important.txt gets overwritten!
Correct: cp -i temp.txt important.txt
2. mv - Practical File Move/Rename Techniques
The mv command stands for "move" and performs file moves and renames. Unlike cp, the original file is deleted, requiring more careful operations.
Basic Syntax
mv [options] source destination
π·οΈ File Rename
$ mv old_name.txt new_name.txt
Change filename from old_name.txt to new_name.txt
π File Move
$ mv document.pdf ~/Documents/
Move document.pdf to Documents directory
β Critical Options for Professional Work
π -i (interactive): Overwrite Confirmation
Overwrite confirmation is essential for mv too. Prevents accidents when destination file exists.
$ mv -i source.txt target.txt overwrite target.txt? (y/n)
π -v (verbose): Detailed Display
Confirms what was moved. Especially useful when handling multiple files.
$ mv -v *.log archive/ 'access.log' -> 'archive/access.log' 'error.log' -> 'archive/error.log'
π« -n (no-clobber): Prevent Overwrite
Use when you absolutely don't want to overwrite existing files.
$ mv -n temp.txt important.txt # Does nothing if important.txt exists
π― Common Patterns in Professional Work
ποΈ File Organization/Classification
# Classify files by extension $ mv *.jpg images/ $ mv *.pdf documents/ $ mv *.txt text_files/
π Rename to Dated Filename
$ mv report.txt report_$(date +%Y%m%d).txt
Result: report.txt β report_20250111.txt
π Create Backup File and Replace
# Backup current config then replace with new one $ mv config.conf config.conf.old $ mv new_config.conf config.conf
π‘οΈ Safety Measures When Using mv
1. Habit of Prior Confirmation
# Check target files before moving $ ls -la source_files/ $ ls -la destination/ # Execute move $ mv -iv source_files/* destination/
2. Backup Important Files
# Backup important files before moving $ cp -p important.txt important.txt.backup $ mv important.txt new_location/
3. Execute Critical Operations Step by Step
# Don't move large quantities at once, test gradually $ mv -v file1.txt backup/ # First, just one $ mv -v file*.txt backup/ # If successful, move the rest
3. rm - Deletion Basics and the Terror of rm -rf
The rm command stands for "remove" and deletes files or directories. Once deleted, recovery is basically impossible, making it one of the most dangerous Linux commands.
Basic Syntax
rm [options] file/directory
π Basic File Deletion
$ rm file.txt
Delete file (normally without confirmation)
π¨ rm -rf : System-Destroying Dangerous Command
rm -rf has "nuclear weapon" level danger. One mistake can destroy entire systems.
π Actual Disaster Examples
- GitLab's 12-hour Data Loss Incident: Admin's rm -rf error
- Pixar's Toy Story 2 Deletion: Accidental rm -rf * execution
- Multiple Companies' Complete DB Deletion: Script variable errors
β‘ Particularly Dangerous Patterns
$ rm -rf / # Delete entire system $ rm -rf /* # Delete everything under root $ rm -rf $HOME # Delete entire home directory $ rm -rf ./* # Delete everything in current directory
π‘οΈ Safe rm Usage
π -i (interactive): Confirmation Before Deletion
Essential when using rm. Asks for confirmation before deleting each file.
$ rm -i file.txt remove file.txt? (y/n)
π -r (recursive): Directory Deletion
Deletes directory and its contents. Always combine with -i.
$ rm -ri old_project/
Absolutely avoid: rm -rf (recursive delete without confirmation)
π -v (verbose): Display Deletion Contents
Displays what's being deleted. Important for safety confirmation.
$ rm -iv *.tmp removed 'temp1.tmp' removed 'temp2.tmp'
π¨ Essential Checks Before Using rm
Safety Confirmation Procedure Before Deletion
- Check Current Location
$ pwd
- Clearly Confirm Deletion Target
$ ls -la target_files*
- Check for Important Files
# Check if important files are included $ ls -la | grep important
- Verify Backup Exists
$ ls -la *.backup
- Execute Step-by-Step Deletion
# First test delete one file $ rm -iv test_file.txt # If no problem, delete the rest $ rm -iv other_files*
π‘ Safer Alternatives to rm
ποΈ Move to Trash (trash-cli)
Move to trash instead of permanent deletion. Recoverable.
# Install trash-cli (Ubuntu/Debian) $ sudo apt install trash-cli # Move file to trash $ trash file.txt # Check trash contents $ trash-list # Restore from trash $ trash-restore
π Move to Deletion Directory
Move to dedicated directory before deletion. Delete after manual confirmation.
# Create deletion directory $ mkdir ~/.to_delete # Move files to deletion directory $ mv unwanted_files* ~/.to_delete/ # Actually delete after checking later $ rm -ri ~/.to_delete/*
π‘οΈ Additional Measures for System Protection
Improve Safety with Alias Settings
# Add to ~/.bashrc alias rm='rm -i' alias cp='cp -i' alias mv='mv -i'
Protect Important Directories
# Make important directory read-only $ chmod -w important_directory/
4. cat/less/more - Choosing File Content Viewing Tools
There are multiple commands for displaying file contents, but each has different characteristics and appropriate use cases. In professional work, it's important to choose appropriately based on file size and purpose.
cat - Simple Full Content Display
cat stands for "concatenate" and displays file contents concatenated together.
π Basic File Display
$ cat config.txt
Display entire file contents at once
β οΈ Cautions When Using cat
Don't use cat for large files!
$ cat huge_log.txt # Danger! Terminal may freeze
Files over several MB may freeze the terminal or slow down the system.
less - Professional's Favorite High-Function Viewer
less is a highly functional command for viewing files page by page.
π Safe Viewing of Large Files
$ less large_file.log
Display contents page by page, memory efficient
Operation Keys in less
| Key | Action | Usage Frequency |
|---|---|---|
Space or f |
Next page | β β β |
b |
Previous page | β β β |
/string |
Forward search | β β β |
?string |
Backward search | β β |
n |
Next search result | β β β |
N |
Previous search result | β β |
g |
Go to file beginning | β β |
G |
Go to file end | β β β |
q |
Quit | β β β |
Usage Guidelines
π When to Use cat
- Small config files (under 100 lines)
- When passing entire file contents to other commands
- When concatenating multiple files
$ cat small_config.txt $ cat file1.txt file2.txt > combined.txt
π When to Use less
- Viewing large log files
- When searching within files
- When wanting to save memory
- When wanting vi-like operations
$ less /var/log/system.log $ less +G access.log # Display from end
πΊοΈ No Need to Use more
more is a downgraded version of less and is rarely used nowadays. Always use less.
π‘ Professional File Viewing Techniques
Check File Size Beforehand
# Check size before using cat $ ls -lh large_file.txt -rw-r--r-- 1 user group 50M Jan 11 10:00 large_file.txt # 50MB so use less $ less large_file.txt
Combining with Real-time Log Monitoring
# Check latest logs with less, then monitor with tail -f $ less +G /var/log/app.log # Display from end # After exiting with q $ tail -f /var/log/app.log # Real-time monitoring
π Complete File Operations Series
- Basics Edition (This Article) - Basic operations and safety measures for cp, mv, rm, cat
- Advanced Edition - head, tail, file, stat, pipes, practical exercises