Module brief#
Build and reshape directory trees safely: touch, mkdir, cp, mv, rm, and rmdir — with interactive flags that protect you while you learn.
Learning objectives#
- Create empty files and directory trees (
touch,mkdir,mkdir -p) - Copy files and directories (
cp,cp -i,cp -r) - Move and rename with
mv - Remove files and directories carefully (
rm,rm -i,rm -r,rmdir) - Prefer confirmation flags while building muscle memory
1. Create files with touch#
touch updates timestamps; if the file does not exist, it creates an empty file.
cd /tmp/linux-lab-pushpjeet
mkdir -p labs/m5/src labs/m5/docs
touch labs/m5/src/app.py labs/m5/docs/readme.md
ls -la labs/m5/src labs/m5/docs
labs/m5/docs:
total 8
drwxr-xr-x 2 box box 4096 Sep 26 11:05 .
drwxr-xr-x 4 box box 4096 Sep 26 11:05 ..
-rw-r--r-- 1 box box 0 Sep 26 11:05 readme.md
labs/m5/src:
total 8
drwxr-xr-x 2 box box 4096 Sep 26 11:05 .
drwxr-xr-x 4 box box 4096 Sep 26 11:05 ..
-rw-r--r-- 1 box box 0 Sep 26 11:05 app.py
For short text content, printf or a here-document also works:
printf 'print("hello")\n' > labs/m5/src/app.py
2. Create directories with mkdir#
mkdir labs/m5/data
mkdir -p labs/m5/nested/deep/path
-p creates missing parents and does not error if the path already exists — ideal for scripts and lab setup.
mkdir -p demo/nested/deep
touch demo/nested/deep/x.txt
ls -R demo
demo:
nested
demo/nested:
deep
demo/nested/deep:
x.txt
3. Copy with cp#
cp notes.txt notes-copy.txt
ls notes*.txt
notes-copy.txt
notes.txt
Important flags:
| Flag | Purpose |
|---|---|
cp -i |
Interactive — ask before overwriting |
cp -r or cp -R |
Recursive — required to copy directories |
cp -a |
Archive mode (preserves attributes; common on GNU) |
cp -r labs/m5 labs/m5-backup
ls labs/
m5 m5-backup
Without -r, copying a directory fails. Always prefer cp -i while learning so an accidental overwrite asks first.
4. Move and rename with mv#
mv relocates a name. If the destination is in the same directory, you have renamed the file.
mv labs/m5/docs/readme.md labs/m5/docs/README.md
ls labs/m5/docs
README.md
mv notes-copy.txt labs/m5/docs/
mv -i asks before overwriting an existing destination — use it.
Moving a directory does not need -r (unlike cp).
5. Delete carefully: rm and rmdir#
| Command | Use when |
|---|---|
rm file |
Remove a file |
rm -i file |
Confirm each file |
rmdir dir |
Remove an empty directory |
rm -r dir |
Remove a directory and its contents |
rm -ri dir |
Recursive + interactive confirmations |
# Safe pattern while learning:
rm -i empty.dat
rmdir labs/m5/data # works only if empty
Danger: rm -rf / or wild paths as root can destroy a system. Never practise recursive force-delete outside a disposable lab folder. There is no recycle bin in classic CLI rm.
Empty a tree you created for practice:
rm -ri demo # answer y/n as you learn; later you may use rm -r on known lab trees
6. A small project tree workflow#
Typical flow:
mkdir -p project/{src,docs,data}touchor create starter filescp -r project project-backup- Rename with
mvas names stabilise - Remove leftovers with
rm -i/rmdir
After the demo copy/rename steps, a lab listing looked like:
ls -laR labs/m5
labs/m5:
total 16
drwxr-xr-x 4 box box 4096 Sep 26 11:05 .
drwxr-xr-x 4 box box 4096 Sep 26 11:05 ..
drwxr-xr-x 2 box box 4096 Sep 26 11:05 docs
drwxr-xr-x 2 box box 4096 Sep 26 11:05 src
labs/m5/docs:
…
-rw-r--r-- 1 box box 0 Sep 26 11:05 README.md
labs/m5/src:
…
-rw-r--r-- 1 box box 0 Sep 26 11:05 app.py
Practice: MCQs#
Q1. Which command creates /tmp/a/b/c even if parents are missing?
- A)
mkdir /tmp/a/b/c - B)
mkdir -p /tmp/a/b/c - C)
touch /tmp/a/b/c - D)
cp -r /tmp/a/b/c
Answer
B. -p creates parent directories as needed.
Q2. Why does cp mydir otherdir often fail when mydir is a directory?
- A) Copying directories is impossible on Linux
- B) You normally need recursive mode:
cp -r - C)
cponly works as root - D) Names cannot contain letters
Answer
B. Directory copy requires -r / -R.
Q3. mv report.txt reports/report.txt primarily:
- A) Encrypts the file
- B) Moves (or renames via path change) the file
- C) Creates a hard link only
- D) Prints the file
Answer
B. mv relocates the directory entry.
Q4. Safest beginner habit before deleting a tree:
- A) Always
rm -rffrom/ - B) Use
rm -riinside a known lab directory after checkingpwd - C) Delete as root to save time
- D) Prefer
catto delete
Answer
B. Confirm location, then interactive recursive delete on disposable data.
Q5. rmdir project fails when:
- A) The directory is not empty
- B) The name contains letters
- C) You are not using Ubuntu
- D)
projectis a regular file named with a trailing slash only
Answer
A. rmdir only removes empty directories. Use rm -r for trees (carefully).
Practice: Fill in the blank#
- Create parents as needed:
mkdir ___ path/to/dir - Copy a directory tree:
cp ___ src_tree dest_tree - Interactive copy (ask before overwrite):
cp ___ file1 file2 - Rename
old.txttonew.txt:___ old.txt new.txt - Remove an empty directory:
________ emptydir
Answers
-p-r(or-R)-imvrmdir
Practice: Order the steps#
Order a safe delete of a practice directory named sandbox under your lab folder.
- Run
rm -ri sandboxand read each prompt - Run
pwdand confirm you are inside the lab directory - Run
ls -la sandboxand decide everything inside is disposable - Create
sandboxearlier with throwaway files (already done in the lab) - Optionally take a VM snapshot before large experiments
Answer key
5 → 4 → 2 → 3 → 1 (snapshot optional but first if you use VMs; then ensure the tree exists; confirm location; inspect; then interactive delete)
If snapshot is skipped: 2 → 3 → 1 after the tree exists.
Practice: Mini terminal challenge#
Challenge: Build a mini project tree#
Goal: Create, copy, rename, and clean up safely.
Setup: cd /tmp/linux-lab-pushpjeet
Tasks:
mkdir -p mini/{src,tests,docs}touch mini/src/main.sh mini/tests/test_main.sh mini/docs/notes.txtcp -r mini mini-backupmv mini/docs/notes.txt mini/docs/NOTES.txtrm -i mini/tests/test_main.sh(confirm)rmdir mini/testsafter it is empty
Verify: ls -R mini shows src and docs with NOTES.txt; mini-backup still has the original layout.
Stretch: Use cp -i to copy a file onto an existing name and practise answering n once.
Hands-on lab#
Goal: Confident CRUD on a disposable tree.
Steps:
- Under your lab directory, build
workdir/{bin,etc,var/log}withmkdir -p. - Create three small files; copy the whole
workdirtoworkdir.bakwithcp -r. - Rename one file with
mv; move another into a different subdirectory. - Delete only the backup with
rm -ri workdir.bakafter checkingpwd.
Verify: workdir remains; workdir.bak is gone; find workdir -type f lists your remaining files.
Stretch: Intentionally try rmdir on a non-empty directory and read the error — then remove contents the safe way.
Common mistakes#
rm -rfmuscle memory before understanding paths- Forgetting
-roncpfor directories - Running destructive commands without
pwd - Assuming deleted files are recoverable like a desktop trash folder
Summary#
You can create files and trees, copy and rename them, and delete with intention. Interactive flags are training wheels worth keeping until the commands feel natural.