Linux File Operations Basics: A Practical Guide to Using cp / mv / rm Safely
What You'll Learn
- Safely copy, move, and delete files and directories
- Prevent accidents like "thought I deleted it but didn't" or "deleted too much"
- Troubleshoot common issues like Permission denied and path errors on your own
Target Audience: Beginners starting to work with Ubuntu servers or local environments
Important: Since rm involves dangerous operations, only practice within your practice directory
The Accident-Free Pattern (TL;DR)
1) pwd → 2) ls -la → 3) Confirm target → 4) cp -i / mv -i → 5) rm -i
Table of Contents
First, Set Up a Safe Practice Environment
To prevent accidents, all operations in this article should be performed within this directory.
$ mkdir -p ~/pgym/file-operations $ cd ~/pgym/file-operations $ pwd $ ls -la
/home/user/pgym/file-operations total 8 drwxr-xr-x 2 user user 4096 Dec 17 11:00 . drwxr-xr-x 3 user user 4096 Dec 17 11:00 ..
1) cp: Copy (Most Common Overwrite Accidents)
Basic Usage
$ touch a.txt $ cp a.txt b.txt $ ls -la
-rw-r--r-- 1 user user 0 Dec 17 11:01 a.txt -rw-r--r-- 1 user user 0 Dec 17 11:01 b.txt
Safe Pattern (with overwrite confirmation)
$ cp -i a.txt b.txt
cp: overwrite 'b.txt'?
Directory Copy (-r)
$ mkdir dir1 $ touch dir1/file1.txt $ cp -r dir1 dir1-backup
Common Issues → Recovery
cp: cannot stat 'xxx': No such file or directory
→ Cause: Path/spelling mistake
→ Fix:pwd→ls -la- Overwrite accident
→ Prevention: Always usecp -i
▶ cp -r vs cp -a: Preventing Permission Issues
Real Incident
Backed up web server files with cp -r:
$ cp -r /var/www/html ~/backup/
→ The copied files' ownership changed to your user, and the web server couldn't read them anymore.
Difference Between -r and -a
| Option | Behavior | Use Case |
|---|---|---|
-r | Recursive copy (permissions follow user's default) | Personal working copies |
-a | Archive mode (preserves permissions, timestamps, symlinks) | Backups, production migrations |
When to Use Which
- Personal working copies →
cp -ris fine - Backups or production migrations → Use
cp -a
$ sudo cp -a /var/www/html ~/backup/
2) mv: Move/Rename (Often Mistaken as "Deleted")
Rename
$ mv a.txt a-renamed.txt
Move
$ mkdir moved $ mv a-renamed.txt moved/ $ ls -la moved
Safe Pattern
$ mv -i b.txt moved/
Common Issues → Recovery
- Lost track of file
→ Fix: Check the destination withls - Overwrite accident
→ Prevention:mv -i
▶ Unexpected mv Accidents (Cross-Filesystem Moves)
How mv Works Internally
- Same filesystem: Just renames the file (fast & safe)
- Different filesystems: Actually "copy → delete original"
Real Incident
Moved a 10GB video file to a USB drive:
$ mv large-video.mp4 /mnt/usb/
→ Pulled out the USB during the copy (5 minutes in)
→ Result: Original file already deleted, incomplete file on USB = Data loss
Safe Approach
For cross-filesystem moves, separate cp and rm:
$ cp large-video.mp4 /mnt/usb/ $ ls -la /mnt/usb/large-video.mp4 # Verify copy completed $ rm large-video.mp4 # Delete after confirmation
Why this is safer: You visually confirm the copy completed before deleting, so even if something fails midway, the original file remains.
3) rm: Delete (Critical - Frequent Accidents)
Basic Usage
$ rm b.txt
Safe Pattern (Highly Recommended)
$ rm -i a-renamed.txt
rm: remove regular file 'a-renamed.txt'?
Directory Deletion (Use Caution)
$ rm -r dir1
Critical Warning
- Beginners should NOT use
rm -rf - Always confirm targets with
ls -labeforehand
Common Issues → Recovery
Is a directory→ Need-rflagPermission denied→ Insufficient permissions (don't abuse sudo)- Wildcard accident (
rm -rf *) → Explicit worst-case example
▶ The Real Danger of rm -rf (Actual Incident Examples)
Incident 1: One Space Deletes Everything
Intended command:
$ rm -rf /tmp/test
Actually typed (space typo):
$ rm -rf / tmp/test
→ This tries to delete / (root directory) AND tmp/test
→ The entire system starts disappearing
Incident 2: Variable Expansion Trap
$ rm -rf $DIR/
If $DIR is undefined or empty:
rm -rf /
→ Root directory deletion = System destruction
Safer Alternatives
| Method | Description |
|---|---|
rm -I | Prompts for confirmation when deleting 3+ files |
trash-cli | Moves to trash (recoverable) |
--preserve-root | Refuses to delete / (enabled by default) |
Safe Practice in Scripts
# Verify variable is not empty before deleting
if [ -n "$DIR" ]; then
rm -rf "$DIR"
fi
Basic Checks to Prevent Path Accidents
$ pwd $ ls -la $ ls -la target_path
Make it a habit to always run these 3 steps before any operation.
Common Troubleshooting Quick Reference
| Symptom | First Check |
|---|---|
| No such file | pwd / ls |
| Permission denied | ls -la |
| File disappeared | Check destination |
| Worried about overwrite | -i |
Detailed Troubleshooting
Case 1: No such file or directory
$ cp important.txt backup/
cp: cannot stat 'important.txt': No such file or directory
Diagnosis
$ pwd $ ls -la
Common Causes
- Wrong current directory (you're not where you think you are)
- Typo in filename
- File was already moved or deleted
Fix
Verify the filename with ls and re-run with the correct path.
Case 2: Permission denied
$ cp /etc/shadow ~/backup/
cp: cannot open '/etc/shadow' for reading: Permission denied
Diagnosis
$ ls -la /etc/shadow
-rw-r----- 1 root shadow 1234 Dec 17 10:00 /etc/shadow
Cause
System files are intentionally protected. /etc/shadow contains password hashes, so regular users can't read it.
Important Note
"Fix it with sudo" is not always correct. Ask yourself why you need access to that file and whether it's really necessary.
Case 3: Directory not empty
$ rmdir mydir
rmdir: failed to remove 'mydir': Directory not empty
Diagnosis
$ ls -la mydir
total 8 drwxr-xr-x 2 user user 4096 Dec 17 11:00 . drwxr-xr-x 3 user user 4096 Dec 17 11:00 .. -rw-r--r-- 1 user user 0 Dec 17 11:00 .hidden
Cause
Hidden files (starting with .) remain in the directory. Plain ls won't show them.
Fix
$ rm mydir/.hidden # Delete after verifying contents $ rmdir mydir # Or use rm -r mydir
Why this is safer: By checking with ls -la before deleting, you reduce the risk of accidentally removing unintended files.
Practice Exercise (5 min)
$ cd ~/pgym/file-operations $ mkdir work $ cd work $ touch memo.txt $ echo "hello" > memo.txt $ cp -i memo.txt memo.bak $ mv -i memo.txt memo.old $ ls -la
Use this exercise to experience the confirmation behavior of cp -i and mv -i.
What to Read Next
Verification Environment
Commands in this article were verified on Ubuntu 24.04 LTS / bash 5.2.
Master File Operations Through Practice
Once you've learned the "accident-free pattern" introduced in this article, solidify your skills with hands-on practice at Penguin Gym Linux.