Essential File Operations
Create, manage, search, link, and archive files and directories safely.
File manipulation is the daily bread of Linux work. This chapter covers creation, copying, moving, removal, search, links, redirection, and archiving — with an emphasis on doing each safely and efficiently.
By the end of this chapter you will be able to
- Perform safe create/copy/move/remove operations on files and directories.
- Apply globbing patterns to operate on file sets.
- Locate files by name (find/locate) and content (grep).
- Create hard and symbolic links and explain the difference.
- Create and extract tar/gzip archives and use redirection and pipes.
3.1 Core File Operations
| Command | What it does |
|---|---|
| touch file | Create an empty file (or update its timestamp). |
| cp a b | Copy a to b. Add -r to copy a whole directory. |
| mv a b | Move or rename a to b. |
| rm file | Delete a file. No recycle bin — it’s gone. |
| mkdir -p x/y/z | Create a nested directory tree in one step. |
| rmdir / rm -r | Remove an empty directory / a directory and its contents. |
3.2 Globbing (Wildcards)
Wildcards let one command target many files. The shell expands them before the command runs.
| Pattern | Matches |
|---|---|
| * | Any number of characters: *.txt = every .txt file. |
| ? | Exactly one character: file?.log. |
| [abc] | Any one of a, b, or c. |
| {jpg,png} | Brace expansion: *.{jpg,png} = both types. |
# Move every .jpg into a photos folder
mkdir -p photos && mv *.jpg photos/
# Copy two file types at once
cp report.{md,pdf} /tmp/backup/
3.3 Finding Files
Two tools: find searches live by name or attributes; grep searches inside files for text.
find (by name/size) and grep (by content)
# Every .log file under /var/log
find /var/log -name '*.log'
# Files larger than 100 MB under your home
find ~ -type f -size +100M
# Which files mention the word 'timeout'?
grep -ril timeout /etc
3.4 Hard and Symbolic Links
A symbolic link (symlink) is a pointer to another file — like a shortcut. A hard link is a second real name for the same data. Symlinks are what you’ll use most.
# Create a symbolic link
ln -s /opt/app/current/config.yml ~/config.yml
# It points at the target; follow it with ls -l
ls -l ~/config.yml
3.5 Archiving and Compression
tar bundles many files into one archive; gzip compresses it. The two are usually combined.
tar: create (c), extract (x), list (t); z = gzip, f = file
# Create a compressed archive of a project
tar -czf project.tar.gz project/
# List what's inside without extracting
tar -tzf project.tar.gz
# Extract it
tar -xzf project.tar.gz
3.6 Redirection and Pipes
| Symbol | Meaning |
|---|---|
| command > file | Send output INTO a file (overwrites it). |
| command >> file | Append output to the end of a file. |
| command < file | Feed a file in as input. |
| a | b | Send a’s output straight into b. |
| command 2> errs.log | Redirect error messages separately. |
# Save a list of running processes
ps aux > processes.txt
# Append a timestamped note to a log
echo "$(date): backup done" >> backup.log
3.7 Guided Lab: File Workflow
Estimated time: 25 minutes. Build a small project tree, manipulate it, search it, and archive it — the everyday moves in one flow.
- Create the tree:
mkdir -p dojo/{src,docs,logs}thencd dojo. - Make some files:
touch src/app.py docs/readme.md logs/run.log. - Copy readme.md into the project root, then rename it to README.md.
- Write text into a log:
echo 'started' > logs/run.logthen appendecho 'finished' >> logs/run.log. - Find every file under dojo larger than zero bytes:
find dojo -type f -size +0c. - Search for the word ‘finished’ inside the logs:
grep -r finished logs/. - Archive the whole project:
tar -czf dojo.tar.gz dojo/, then list it withtar -tzf dojo.tar.gz.
Troubleshooting
| Symptom | Likely cause and fix |
|---|---|
| rm deleted the wrong files | There is no undo. Prevent it: preview globs with ls first, use rm -i to confirm, and keep backups. Recovery tools rarely help on a busy filesystem. |
| ‘cannot remove: Is a directory’ | Use rm -r for directories, or rmdir for empty ones. Plain rm only removes files. |
| tar: extracted files in the wrong place | tar extracts relative to your current directory. cd to the intended target first, or use -C /path to extract there. |
| grep returns nothing on a binary file | grep is for text. Use the right tool for binaries, or grep -a to force text mode (with care). |
Practice & Prove It
Write-the-command drills
- Create the nested directories reports/2026/q1 in one command.
- Move every .csv file in the current folder into a data/ subfolder.
- Find all files under /etc whose name ends in .conf.
- Create a gzip-compressed archive of the folder mysite/ named mysite.tar.gz.
- Append the current date to a file called journal.txt without erasing it.
Quick quiz
- What is the difference between > and >> ?
- Which wildcard matches exactly one character?
- Which command searches inside files for text?
- What does the -r option add to cp and rm?
- How do you list the contents of a .tar.gz without extracting it?
Key Takeaways
- cp, mv, rm, mkdir, and touch are the core file operations — rm is permanent, so verify first.
- Wildcards (* ? [] {}) let one command act on many files; preview with ls before destructive use.
- find locates files by name/attributes; grep finds text inside files; locate is fast but cached.
- Symbolic links are shortcuts that power real deployment workflows.
- tar + gzip bundle and compress; redirection (>, >>, |) routes data between files and commands.
Next — Chapter 4: the user environment and shell configuration that make you faster and your sessions reproducible.