2.4 Creating, Moving and Deleting Files
Key Takeaways
- touch creates an empty file or updates timestamps; mkdir creates directories and mkdir -p builds missing parents
- cp copies files and cp -r copies directory trees; mv renames or relocates; neither is a substitute for the other
- rm deletes files; rm -r deletes directory trees; rmdir removes only empty directories
- Linux filenames are case-sensitive: Report.txt and report.txt are different names
- Simple globbing with *, ?, and [ ] expands to matching names before the command runs; * does not match a leading dot
2.4 Creating, Moving and Deleting Files
Quick Answer: Build with
touchandmkdir(mkdir -pfor nested paths). Duplicate withcp/cp -r. Rename or relocate withmv. Remove files withrm, trees withrm -r, and empty folders withrmdir. Linux names are case-sensitive, and shell globs (*,?,[ ]) expand before the command runs.
Objective 2.4 turns navigation into file operations: create, rearrange, delete, and select names with globs—remembering File and file are different.
Case Sensitivity
On a typical Linux filesystem, names are case-sensitive:
touch Report.txt
touch report.txt
ls
You now have two files. Commands looking for REPORT.txt fail unless that exact spelling exists—a frequent Essentials trap.
Creating Files With touch
touch notes.txt
touch a.txt b.txt c.txt
- If the name does not exist,
touchcreates an empty file. - If it already exists,
touchupdates timestamps and leaves content unchanged. touchdoes not open an editor; it only creates or refreshes metadata.
Creating Directories With mkdir
mkdir reports
mkdir drafts archive # multiple directories at once
mkdir -p labs/linux/010 # create missing parents
Plain mkdir labs/linux/010 fails if labs or linux is missing. mkdir -p creates each missing component and does not fail merely because the final directory already exists. Use -p whenever you build nested lab trees in one step.
Copying With cp
cp notes.txt notes.bak
cp notes.txt ~/backup/
cp file1.txt file2.txt dest_dir/
cp -r project project-copy
| Goal | Pattern |
|---|---|
| Duplicate a file | cp source dest |
| Copy into a directory | cp source dir/ |
| Copy several files into a directory | cp a b c dir/ |
| Copy a directory tree | cp -r source_dir dest |
Without -r (or -R), cp refuses to copy a directory. Recursive copy duplicates the tree; the original remains. Useful companion flags: -i (prompt before overwrite), -v (verbose). If the destination file name already exists, cp typically overwrites it when permissions allow unless you add -i or -n.
Moving and Renaming With mv
mv both renames and moves:
mv draft.txt final.txt # rename in place
mv report.pdf ~/Documents/ # move into a directory
mv a.txt b.txt archive/ # move several items
If the destination is an existing directory, sources go into it. If it is an existing file name, mv overwrites by default (-i prompts). A successful mv does not leave a duplicate at the old path—use cp when you need to keep the original.
Which command creates the nested path practice/week1/notes when none of those directories exist yet?
Deleting With rm and rmdir
Remove files: rm
rm notes.txt
rm a.txt b.txt
rm *.tmp
rm deletes files. If you pass a directory without a recursive flag, it errors instead of removing the folder.
Remove trees: rm -r
rm -r old-lab
rm -ri old-lab # recursive but interactive prompts
rm -rf old-lab # recursive + force: powerful and dangerous
-r / -R deletes a directory and everything inside. Adding -f skips most prompts. Preview with ls (and ls -a if hidden names matter) before recursive deletes—there is no trash can in the classic CLI model.
Empty directories only: rmdir
rmdir empty-folder
rmdir succeeds only when the directory contains nothing (hidden files also block it). If it fails as not empty, clear contents first or deliberately use rm -r after confirming.
| Goal | Command |
|---|---|
| Delete files | rm file… |
| Delete a directory tree | rm -r dir |
| Delete an empty directory | rmdir dir |
Simple Globbing: *, ?, and [ ]
Globbing turns patterns into matching filenames before the command runs. This is not the same as regular expressions used later with grep.
| Pattern | Matches |
|---|---|
* | Any string of characters (including empty), within one path component |
? | Exactly one character |
[abc] | One character that is a, b, or c |
[0-9] | One digit |
[Ll]inux | Linux or linux as that character choice |
Examples in a directory with notes.txt, notes.bak, a.png, b.png, and .secret:
ls *.txt # notes.txt
ls notes.* # notes.txt notes.bak
ls ?.png # a.png b.png
ls chap[12].md # chap1.md chap2.md if those exist
ls * # visible names only — .secret omitted
ls .* # patterns that start with . can match hidden names
Critical rule: A bare * does not match a leading dot. Hidden files need a pattern that begins with ., or ls -a—not a bare star. Preview with ls PATTERN before rm PATTERN. Quote to pass a literal metacharacter: ls '*.txt'.
Worked Scenario: Build, Copy, Move, Clean
cd ~
mkdir -p lab/docs lab/data
touch lab/docs/readme.txt lab/data/sample.csv
cp lab/docs/readme.txt lab/docs/readme.bak
cp -r lab lab-backup
mv lab/data/sample.csv lab/docs/
rm lab/docs/readme.bak
rmdir lab/data # succeeds only if data is empty
rm -r lab-backup
Case sensitivity: touch Todo.txt then rm todo.txt fails—the real name is still Todo.txt. Globbing: rm f?.log removes f1.log and f2.log but not f10.log, because ? matches exactly one character.
Command Summary for Objective 2.4
| Task | Command |
|---|---|
| Empty file / refresh timestamp | touch name |
| Nested path | mkdir -p path/to/dir |
| Copy file / tree | cp src dest / cp -r src dest |
| Rename or move | mv src dest |
| Delete files / tree / empty dir | rm / rm -r / rmdir |
| Match groups of names | globs *, ?, [ ] |
Practice building a tiny tree, copying it, renaming, then removing a backup with rm -r—those patterns map to objective 2.4.
Which command copies the directory website and all nested contents into website-bak while leaving the original in place?
In a directory containing notes.txt, report.pdf, and .bashrc, which names does the pattern * match for a normal shell glob?
You run rmdir project and receive an error that the directory is not empty. Which command removes project and everything inside it?