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
Last updated: July 2026

2.4 Creating, Moving and Deleting Files

Quick Answer: Build with touch and mkdir (mkdir -p for nested paths). Duplicate with cp / cp -r. Rename or relocate with mv. Remove files with rm, trees with rm -r, and empty folders with rmdir. 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, touch creates an empty file.
  • If it already exists, touch updates timestamps and leaves content unchanged.
  • touch does 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
GoalPattern
Duplicate a filecp source dest
Copy into a directorycp source dir/
Copy several files into a directorycp a b c dir/
Copy a directory treecp -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.

Test Your Knowledge

Which command creates the nested path practice/week1/notes when none of those directories exist yet?

A
B
C
D

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.

GoalCommand
Delete filesrm file…
Delete a directory treerm -r dir
Delete an empty directoryrmdir 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.

PatternMatches
*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]inuxLinux 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

TaskCommand
Empty file / refresh timestamptouch name
Nested pathmkdir -p path/to/dir
Copy file / treecp src dest / cp -r src dest
Rename or movemv src dest
Delete files / tree / empty dirrm / rm -r / rmdir
Match groups of namesglobs *, ?, [ ]

Practice building a tiny tree, copying it, renaming, then removing a backup with rm -r—those patterns map to objective 2.4.

Test Your Knowledge

Which command copies the directory website and all nested contents into website-bak while leaving the original in place?

A
B
C
D
Test Your Knowledge

In a directory containing notes.txt, report.pdf, and .bashrc, which names does the pattern * match for a normal shell glob?

A
B
C
D
Test Your Knowledge

You run rmdir project and receive an error that the directory is not empty. Which command removes project and everything inside it?

A
B
C
D