← Linux from ScratchCheat sheet
M5Linux from Scratch

M5 — Creating, copying, moving, deleting

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:

  1. mkdir -p project/{src,docs,data}
  2. touch or create starter files
  3. cp -r project project-backup
  4. Rename with mv as names stabilise
  5. 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) cp only 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 -rf from /
  • B) Use rm -ri inside a known lab directory after checking pwd
  • C) Delete as root to save time
  • D) Prefer cat to 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) project is 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#

  1. Create parents as needed: mkdir ___ path/to/dir
  2. Copy a directory tree: cp ___ src_tree dest_tree
  3. Interactive copy (ask before overwrite): cp ___ file1 file2
  4. Rename old.txt to new.txt: ___ old.txt new.txt
  5. Remove an empty directory: ________ emptydir
Answers
  1. -p
  2. -r (or -R)
  3. -i
  4. mv
  5. rmdir

Practice: Order the steps#

Order a safe delete of a practice directory named sandbox under your lab folder.

  1. Run rm -ri sandbox and read each prompt
  2. Run pwd and confirm you are inside the lab directory
  3. Run ls -la sandbox and decide everything inside is disposable
  4. Create sandbox earlier with throwaway files (already done in the lab)
  5. 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:

  1. mkdir -p mini/{src,tests,docs}
  2. touch mini/src/main.sh mini/tests/test_main.sh mini/docs/notes.txt
  3. cp -r mini mini-backup
  4. mv mini/docs/notes.txt mini/docs/NOTES.txt
  5. rm -i mini/tests/test_main.sh (confirm)
  6. rmdir mini/tests after 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:

  1. Under your lab directory, build workdir/{bin,etc,var/log} with mkdir -p.
  2. Create three small files; copy the whole workdir to workdir.bak with cp -r.
  3. Rename one file with mv; move another into a different subdirectory.
  4. Delete only the backup with rm -ri workdir.bak after checking pwd.

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 -rf muscle memory before understanding paths
  • Forgetting -r on cp for 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.

Continue

← Linux from Scratch hub · Cheat sheet · All tutorials