File Operations and Manipulation
Summary
This chapter teaches you how to create, copy, move, and delete files and directories. You'll learn essential commands like touch, mkdir, cp, mv, and rm, as well as how to view file contents using cat, head, tail, and less. These are the everyday operations you'll perform constantly when working with Linux.
Concepts Covered
This chapter covers the following 18 concepts from the learning graph:
- Touch Command
- Mkdir Command
- Rmdir Command
- Cp Command
- Mv Command
- Rm Command
- Rm Recursive
- Cat Command
- Head Command
- Tail Command
- Less Command
- More Command
- Wc Command
- Sort Command
- Uniq Command
- Cut Command
- Paste Command
- Diff Command
Prerequisites
This chapter builds on concepts from:
From Tourist to Resident: Let's Make Some Files!
In the last chapter, you learned to navigate the Linux file system like a tourist exploring a new city. You could look around, read the street signs, and find your way home. But tourists just look at things—now it's time to become a resident and actually DO stuff!
This chapter is where things get real. You're going to learn to:
- Create files and directories out of thin air
- Copy files faster than you can say "Control-C Control-V"
- Move and rename files like a shell ninja
- Delete files (carefully... we'll talk about that)
- Peek inside files to see what's cooking
These are the bread-and-butter commands you'll use every single day in Linux. Master these, and you'll be amazed at how much faster you can work compared to clicking around in a graphical file manager.
With Great Power Comes Great Responsibility
Some of these commands can delete files permanently—no Recycle Bin, no "Are you sure?" confirmation, no undo. We'll teach you safe practices, but always double-check before hitting Enter on any destructive command!
Creating Files: The Touch Command
Let's start with creating files. The touch command is your go-to tool for creating empty files:
1 | |
That's it! You've just created an empty file called myfile.txt. No wizard required, no dialog boxes, no "Save As"—just instant file creation.
"But wait," you might ask, "why is it called touch?" Great question! The command actually has two purposes:
- Create a new empty file if it doesn't exist
- Update the timestamp of an existing file (like "touching" it to show activity)
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
Creating Multiple Files at Once
Why create files one at a time when you can batch them?
1 2 3 4 5 6 7 8 9 10 | |
Brace Expansion: Your Secret Weapon
The curly brace syntax {a,b,c} and {1..10} is called brace expansion. It's not just for touch—you can use it with almost any command. It's like a superpower for your terminal!
Touch Options
1 2 3 4 5 6 7 8 | |
Creating Directories: The Mkdir Command
Files need homes, and that's where the mkdir command (Make Directory) comes in:
1 | |
Boom! You've created a directory called Documents. But here's where it gets fun...
Creating Nested Directories
What if you want to create /home/dan/projects/website/css/themes? You could do this:
1 2 3 4 5 6 7 8 | |
Or you could use the -p flag (for "parent"):
1 | |
The -p flag creates all the parent directories as needed. It's like building the whole staircase at once instead of one step at a time. This is one of the most useful flags in all of Linux.
1 2 3 4 5 6 7 8 9 | |
Combine with Brace Expansion
Mix mkdir -p with brace expansion for maximum efficiency:
1 | |
Removing Directories: The Rmdir Command
The rmdir command (Remove Directory) deletes directories... but there's a catch. It only removes empty directories:
1 2 3 4 5 6 7 | |
This is actually a safety feature. rmdir won't accidentally delete a directory full of important files. If you want to remove a directory that has stuff in it, you'll need rm -r (which we'll cover soon, with lots of warnings).
1 2 3 4 5 6 | |
Diagram: File and Directory Creation Commands
Creating Files and Directories Workflow
Type: diagram
Use the microsim-py skill to create a side-by-side two panel simulation of directories and files being created in a projects directory.
Bloom Taxonomy: Understand, Apply
Learning Objective: Visualize the relationship between mkdir, touch, echo and cat, and their options for creating file system structures.
Layout: two panels side by side. The left side shows commands. The right side is a upside down tree drawing (root at the top) of a file system. The /usr/home/dan node is at the top.
Components to show: - file system - just home directory labeled HOME - Sequential steps showing touch and mkdir creating structure - Visual representation of brace expansion creating multiple items - Tree view showing final result - dark circle around the node for current directory
Layout: Three-panel sequential diagram
Step 1 - Initial State: - Empty HOME on the right - Shell Command 1: pwd returns /usr/home/dan
Step 2 - Commands Executed: - mkdir projects - cd projects The projects node appears under the HOME directory
Step 2 - Touch Command - touch creates empty files (shown as small document icons) - touch today.txt - Right tree shows 'today.txt' in projects
- mkdir creates directories (shown as folder icons)
- mkdir -p creates nested paths (shown with dotted parent folders materializing)
- Brace expansion shows one command creating multiple items
Panel 3 - Final Result: - Complete tree structure showing all created items - Files vs directories visually distinguished
Color coding: - touch commands: Blue - mkdir commands: Green - New files: Light blue - New directories: Light green
Animation: - Show commands executing one by one - Items appear with subtle pop animation - Tree view updates in real-time
Implementation: p5.js with step-by-step animation
Copying Files: The Cp Command
Now we're getting to the action! The cp command (Copy) duplicates files and directories:
1 2 | |
Think of cp as a photocopier for your files. The original stays put, and a duplicate appears wherever you specify.
Copy Scenarios
1 2 3 4 5 6 7 8 9 10 11 | |
Essential Cp Options
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | |
Trailing Slashes Matter!
Be careful with directory copies:
1 2 3 | |
-v to see exactly what's happening!
Preserving File Attributes
When you copy a file, some metadata (like ownership and timestamps) may change. To preserve everything:
1 2 3 4 5 6 | |
| Option | Meaning | When to Use |
|---|---|---|
-i |
Interactive | When you might overwrite files |
-v |
Verbose | When you want to see what's happening |
-r |
Recursive | When copying directories |
-p |
Preserve | When timestamps/permissions matter |
-a |
Archive | For backups (preserves everything) |
-u |
Update | For incremental backups |
Moving and Renaming: The Mv Command
The mv command (Move) is a two-for-one deal. It both moves files AND renames them:
1 2 3 4 5 6 7 8 9 10 11 | |
Here's the thing about mv that's different from cp: the original is gone. It's not a copy; it's a true move. If you mv a file to a new location, it no longer exists in the old location.
The Rename Trick
Since mv oldname newname renames files, this is how you rename things in Linux. There's no separate "rename" command for individual files:
1 2 3 4 5 | |
Mv Options
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |
Mv vs Cp: When to Use Which
- Use cp when you want the original to stay where it is
- Use mv when you want to relocate or rename
- Rule of thumb: If you're "organizing," you probably want
mv. If you're "backing up," you wantcp.
Diagram: Cp vs Mv Comparison
Copy vs Move Operations
Type: diagram
Bloom Taxonomy: Understand Learning Objective: Clearly illustrate the difference between copy (duplicates) and move (relocates) operations.
Layout: Side-by-side comparison with before/after states
Left Panel - CP (Copy): Before: - Source directory with file.txt - Empty destination directory
After: - Source directory STILL HAS file.txt - Destination directory has file.txt copy - Arrow showing duplication action - File appears in TWO places
Right Panel - MV (Move): Before: - Source directory with file.txt - Empty destination directory
After: - Source directory is EMPTY (file gone!) - Destination directory has file.txt - Arrow showing relocation action - File appears in ONE place only
Visual elements: - File icons with clear labels - Directory boxes with names - Arrows showing data flow - Checkmarks/X marks showing presence/absence
Color coding: - CP operations: Blue theme - MV operations: Orange theme - Original file: Solid color - Copy: Dashed border or faded color
Interactive features: - Click "Execute" to animate the operation - Toggle between cp and mv to see difference - Reset button to try again
Implementation: p5.js
Deleting Files: The Rm Command
And now, the moment of truth. The rm command (Remove) deletes files:
1 | |
Gone. Just like that. No confirmation, no Recycle Bin, no undo.
Let me say that again because it's really important:
There is No Undo
When you rm a file in Linux, it is GONE. It doesn't go to a Trash folder. It doesn't ask "Are you sure?" by default. The file is deleted immediately and permanently. Always double-check before hitting Enter!
Rm Options (Your Safety Net)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
Rm Recursive: The Big One
The rm recursive flag (-r or -R) allows you to delete directories and everything inside them:
1 2 | |
This is powerful and dangerous. One wrong command, and you could delete thousands of files. Here are some safety practices:
1 2 3 4 5 6 7 8 9 10 11 12 | |
The Most Dangerous Command in Linux
You may have heard horror stories about:
1 2 3 | |
rm -rf / but the others will work. Always know what directory you're in (pwd) before using rm -r!
Safe Deletion Practices
- Always know where you are: Run
pwdbeforerm -r - Use
-ifor important stuff:rm -ri directory/ - Preview first:
ls directory/before deleting - Consider trash-cli: Install
trash-clifor a command-line trash can - Make backups: Before major deletions, copy important files
1 2 3 4 5 6 7 8 9 | |
One Character Makes a Big Difference
Early in my career I was working as the "Superuser" on a UNIX system. I wanted to delete a big directory of files generated by an overnight logic simulation that tools many megabytes (really big back in 1984). I wanted to type:
1 | |
Unfortunately, what I actually types was the following:
1 | |
Did you notice that extra space? A dot is the current directory.
In this case the "." was the root file systems. UNIX deleted EVERYTHING! Luckily, I was very careful about running backups on magnetic tape every night. Although it took me two hours to restore the file system impacting all my users, I will say I have never repeated that mistake again. Sometimes painful public embarrassment is the best teacher!
Viewing File Contents: Cat, Head, Tail, and Friends
Now that you can create, copy, move, and delete files, let's learn to look inside them!
The Cat Command
The cat command (concatenate) displays file contents. It's one of the most-used commands in Linux:
1 | |
The whole file spills out onto your screen. Simple!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | |
Cat's Origin Story
cat is short for "concatenate" because its original purpose was to join (concatenate) files together:
1 | |
The Head Command
What if you just want to peek at the beginning of a file? The head command shows you the first lines:
1 2 3 4 5 6 7 8 9 | |
head is perfect for:
- Quickly checking what a file contains
- Looking at CSV headers
- Previewing log files
- Checking if a file is what you think it is
The Tail Command
The opposite of head! The tail command shows the last lines of a file:
1 2 3 4 5 6 7 8 9 | |
But here's where tail gets REALLY cool:
1 2 | |
The -f flag makes tail "follow" the file. As new lines are added, they appear on your screen. This is incredibly useful for watching log files while debugging. Press Ctrl+C to stop following.
1 2 3 4 5 | |
Tail -f is Your Debugging Best Friend
When something's not working right, open a terminal and run:
1 | |
Head + Tail Combo: Getting Lines from the Middle
What if you want lines 50-60 from a file? Combine head and tail with pipes!
1 2 3 4 5 | |
Diagram: Head and Tail Visualization
Head and Tail Command Visualization
Type: microsim
Bloom Taxonomy: Understand, Apply Learning Objective: Visualize how head and tail extract portions of files, and understand the -f follow mode.
Canvas layout (responsive, ~700px max): - File visualization area (400px): Shows a "file" with numbered lines - Control panel (remaining): Command input and options
Visual elements: - A tall "document" showing 30+ lines numbered - Highlighted region showing selected lines - Visual representation of command being run - Output preview showing selected lines
Simulated file: - Line 1: "First line of the file" - Line 2: "Second line here" - ... (numbered lines with sample text) - Line 30: "This is the last line"
Interactive controls: - Radio buttons: head / tail / both - Slider: Number of lines (1-30) - Input: Custom number with -n flag - Checkbox: -f mode demonstration (for tail) - Button: "Run Command"
Behavior: - Select head: highlight from top - Select tail: highlight from bottom - Adjust slider: change highlighted range - Show the actual command being generated - Display "output" showing extracted lines - For -f mode: animate new lines appearing at bottom
Example scenarios: - head -5: Highlights lines 1-5 - tail -10: Highlights lines 21-30 - head -20 | tail -5: Highlights lines 16-20
Color scheme: - File background: Light gray - Selected lines: Yellow highlight - Line numbers: Blue - Unselected lines: Faded
Implementation: p5.js
The Less Command (More is Less!)
For large files, cat dumps everything at once—not helpful for a 10,000-line file! The less command is an interactive file viewer:
1 | |
Now you can scroll through the file like a civilized person!
Less navigation:
| Key | Action |
|---|---|
Space / f |
Forward one page |
b |
Back one page |
Enter / j |
Forward one line |
k |
Back one line |
g |
Go to beginning |
G |
Go to end |
/pattern |
Search forward |
?pattern |
Search backward |
n |
Next search match |
N |
Previous search match |
q |
Quit |
1 2 3 4 5 6 7 8 | |
Less Has More Features
less can do so much more than more! You can:
- Search forward AND backward
- Mark positions and return to them
- Edit the file (press v)
- See line numbers
- Handle binary files safely
The More Command
The more command is the older, simpler cousin of less:
1 | |
It only scrolls forward (not backward), which is why the saying goes: "less is more" (because less does more than more!).
more still exists for compatibility, but most people use less because... well... it's more useful. Get it? 😄
Text Processing Tools: Wc, Sort, Uniq, Cut, Paste
Now let's level up with some powerful text processing commands. These tools are like data superpowers!
The Wc Command (Word Count)
The wc command counts lines, words, and characters:
1 2 3 4 5 6 | |
Get specific counts:
1 2 3 4 5 6 7 8 9 10 11 | |
wc is incredibly useful in pipelines:
1 2 3 4 5 6 7 8 | |
The Sort Command
The sort command sorts lines alphabetically (or numerically):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | |
Real-world examples:
1 2 3 4 5 6 7 8 | |
The Uniq Command
The uniq command filters out ADJACENT duplicate lines:
1 2 3 4 5 6 7 8 9 10 11 | |
Important: uniq only removes ADJACENT duplicates. For non-adjacent duplicates, sort first:
1 2 3 4 5 6 7 8 9 10 | |
Classic combo for finding duplicate counts:
1 2 | |
The Cut Command
The cut command extracts columns or fields from text:
1 2 3 4 5 6 7 8 9 | |
Real examples:
1 2 3 4 5 6 7 8 | |
The Paste Command
The paste command is the opposite of cut—it joins files line by line:
1 2 3 4 5 6 7 8 9 10 11 | |
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
Diagram: Text Processing Pipeline
Text Processing Tools Pipeline
Type: diagram
Bloom Taxonomy: Apply, Analyze Learning Objective: Show how text processing tools (sort, uniq, cut, wc) can be combined in pipelines to transform data.
Layout: Horizontal data flow diagram showing transformation stages
Scenario: Processing a log file to find top error types
Input data (leftmost):
1 2 3 4 5 6 7 | |
Pipeline stages (left to right): 1. grep "ERROR" → filters to only ERROR lines 2. cut -d ':' -f 2 → extracts error message 3. sort → alphabetizes messages 4. uniq -c → counts occurrences 5. sort -rn → sorts by count descending 6. head -3 → top 3 errors
Visual elements: - Data boxes showing content at each stage - Arrows with command labels - Color coding for each command type
Output (rightmost):
1 2 | |
Interactive features: - Click each stage to see intermediate output - Highlight matching data at each step - Toggle different example datasets
Color coding: - grep: Red - cut: Orange - sort: Blue - uniq: Green - head: Purple
Implementation: HTML/CSS/JavaScript or p5.js
Comparing Files: The Diff Command
The diff command compares two files and shows their differences:
1 | |
Understanding diff output:
1 2 3 4 5 6 7 8 9 10 | |
The codes mean:
| Code | Meaning |
|---|---|
c |
Changed |
d |
Deleted |
a |
Added |
< |
Line from first file |
> |
Line from second file |
Diff Options
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | |
The unified format (-u) is especially readable:
1 2 3 4 5 6 7 8 | |
Diff + Git
When you use Git for version control, git diff uses a similar format. Learning to read diff output is essential for any developer!
Diagram: Diff Command Visualization
Interactive Diff Comparison Tool
Type: microsim
Bloom Taxonomy: Understand, Analyze Learning Objective: Help students understand diff output by showing side-by-side files with changes highlighted and corresponding diff codes.
Canvas layout (responsive, ~800px max): - Left panel (300px): File 1 content - Right panel (300px): File 2 content - Bottom panel (200px): Diff output with explanations
Visual elements: - Two "text editor" style panels with line numbers - Color-coded highlighting for changes - Diff output with hover explanations - Legend explaining diff codes
Sample files to compare: File 1:
1 2 3 4 5 | |
File 2:
1 2 3 4 5 6 | |
Highlighting: - Changed lines: Yellow background (Banana→Blueberry, Date→Fig) - Deleted lines: Red strikethrough - Added lines: Green background (Grape) - Unchanged: White background
Interactive features: - Edit either file and see diff update live - Click on diff output to highlight corresponding lines - Toggle between normal and unified (-u) format - Switch between different example file pairs
Color coding: - Unchanged: White - Changed: Yellow - Deleted: Light red with strikethrough - Added: Light green
Implementation: p5.js or HTML/CSS/JavaScript
Putting It All Together: File Operations Workflow
Let's practice combining everything you've learned. Here's a real-world scenario:
Scenario: Organizing Downloads
Your Downloads folder is a mess. Let's clean it up!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | |
Scenario: Quick Backup
Before making changes, create a quick backup:
1 2 3 4 5 6 7 8 | |
Scenario: Log Analysis
Find the most common errors in a log file:
1 2 | |
File Operations Cheat Sheet
Here's your quick reference card:
| Command | Purpose | Example |
|---|---|---|
touch |
Create empty file | touch newfile.txt |
mkdir |
Create directory | mkdir -p path/to/dir |
rmdir |
Remove empty directory | rmdir emptydir |
cp |
Copy files/dirs | cp -r src/ dest/ |
mv |
Move/rename | mv old.txt new.txt |
rm |
Delete files | rm -ri directory/ |
cat |
Display file | cat file.txt |
head |
Show beginning | head -20 file.txt |
tail |
Show end | tail -f log.txt |
less |
Page through file | less bigfile.txt |
more |
Page (forward only) | more bigfile.txt |
wc |
Count lines/words | wc -l file.txt |
sort |
Sort lines | sort -n numbers.txt |
uniq |
Remove duplicates | sort \| uniq -c |
cut |
Extract columns | cut -d ',' -f 2 |
paste |
Merge files | paste a.txt b.txt |
diff |
Compare files | diff -u old new |
Key Takeaways
You've mastered the essential file operations! Here's what you now know:
- touch creates empty files (or updates timestamps)
- mkdir -p creates directories (including parents)
- cp -r copies files and directories
- mv moves OR renames files
- rm -r deletes directories (BE CAREFUL!)
- cat displays files, head shows beginnings, tail shows ends
- less is your interactive file viewer (better than more!)
- tail -f follows log files in real-time
- wc counts, sort sorts, uniq de-duplicates
- cut extracts columns, paste combines files
- diff compares files
You're a File Operations Pro!
You can now create, organize, view, and manage files entirely from the command line. These skills will serve you every single day as you work with Linux. Practice these commands until they become second nature!
What's Next?
Now that you can manipulate files, it's time to learn about permissions and ownership. Who can read your files? Who can modify them? Who can run programs? That's coming up next!
But first, let's make sure all this knowledge sticks with some practice...
Quick Quiz: File Operations
- What command creates an empty file?
- How do you create nested directories in one command?
- What's the difference between
cpandmv? - What does
rm -rdo, and why is it dangerous? - How do you watch a log file update in real-time?
- What does
sort | uniqdo thatuniqalone doesn't?
Quiz Answers
touch filenamecreates an empty filemkdir -p path/to/nested/dircreates all parent directoriescpduplicates (original stays),mvrelocates (original gone)rm -rdeletes directories recursively—dangerous because it permanently deletes everything with no undotail -f logfile.txtfollows the file and shows new lines as they're addedsort | uniqremoves ALL duplicates;uniqalone only removes ADJACENT duplicates
References
- GNU Coreutils Manual - Official documentation for touch, mkdir, cp, mv, rm, and other core file utilities.
- Linux cp Command Tutorial - Linuxize's comprehensive guide to copying files and directories with practical examples.
- Linux mv Command Examples - TecMint's tutorial on moving and renaming files safely.
- Understanding rm and Safe Deletion - DigitalOcean guide to using rm command safely and avoiding disasters.
- Cat Command in Linux - GeeksforGeeks tutorial on viewing and concatenating files.
- Head and Tail Commands Explained - How-To Geek's guide including head and tail for file preview.
- Less is More: Pager Commands - Linuxize tutorial on using less and more for viewing large files.
- Linux wc Command Tutorial - GeeksforGeeks guide to counting lines, words, and characters in files.
- Sort Command with Examples - TecMint's comprehensive guide to sorting text data.
- Uniq Command Tutorial - GeeksforGeeks explanation of removing duplicate lines from sorted data.
- Cut Command in Linux - Linuxize guide to extracting columns and fields from text files.
- Paste Command Examples - GeeksforGeeks tutorial on merging files line-by-line.
- Using diff to Compare Files - Computer Hope's guide to finding differences between files.
- Touch Command Tutorial - HowtoForge explanation of creating files and updating timestamps.
- Mkdir Command with Examples - PhoenixNAP guide to creating directories including nested structures.
- Ryan's Tutorials: Linux File Manipulation - Interactive tutorial on basic file operations for beginners.
- Trash-CLI: Safer File Deletion - GitHub repository for command-line trash/recycle bin alternative to rm.
- Brace Expansion in Bash - Linux Journal article on using brace expansion for efficient file creation.
- Understanding Tail -f for Log Monitoring - How-To Geek guide to watching log files in real-time.
- File Operations Best Practices - Opensource.com article on safely managing files in Linux.