HomeTerminal & Command LineCreating, Moving & Deleting Files
beginner12 min read· Module 3, Lesson 3

📝Creating, Moving & Deleting Files

Master touch, cp, mv, rm, and cat — the essential file commands

Why File Operations Matter

Every task you do on a computer — coding, organizing photos, deploying a server — comes down to creating, copying, moving, and deleting files. In a GUI you drag and drop. In the terminal you type a command and it happens instantly, even across thousands of files.

This lesson covers the essential file commands that every developer must know. By the end, you will be comfortable creating, inspecting, copying, moving, and deleting files entirely from the terminal.


1. Creating Files — `touch`

The `touch` command creates an empty file (or updates the timestamp of an existing one).

```bash

Create a single file

touch notes.txt

Create multiple files at once

touch index.html style.css app.js

Create a file inside a directory

touch src/utils/helpers.ts ```

What if the directory doesn't exist?

`touch` will not create missing directories. You need `mkdir -p` first:

```bash mkdir -p src/utils touch src/utils/helpers.ts ```

Quick file creation with content

If you want a file with content, use redirection instead:

```bash

Write a single line

echo "Hello World" > greeting.txt

Write multiple lines

cat > config.yaml << 'EOF' name: my-app version: 1.0 debug: false EOF ```

Tip: `>` overwrites the file. `>>` appends to it. Mixing them up can destroy data.


2. Copying Files — `cp`

`cp` copies a file from one location to another.

```bash

Copy a file

cp original.txt backup.txt

Copy a file into a directory

cp report.pdf ~/Documents/

Copy and rename

cp config.json config.json.bak ```

Copying Directories — `cp -r`

Directories require the `-r` (recursive) flag:

```bash

Copy an entire directory

cp -r src/ src-backup/

Copy a directory into another directory

cp -r assets/ ~/project/assets/ ```

Useful cp Flags

FlagPurpose
`-r`Copy directories recursively
`-i`Ask before overwriting
`-v`Verbose — show what is being copied
`-n`Do not overwrite existing files
`-u`Copy only when source is newer

```bash

Safe copy — ask before overwriting

cp -i important.doc ~/backup/

Verbose copy — see every file

cp -rv project/ project-backup/ ```


3. Moving & Renaming — `mv`

`mv` does two things: moves files and renames them. There is no separate rename command.

```bash

Rename a file

mv old-name.txt new-name.txt

Move a file to a directory

mv report.pdf ~/Documents/

Move and rename at the same time

mv draft.md ~/final/published-article.md ```

Moving Directories

Unlike `cp`, `mv` does not need `-r` for directories:

```bash

Move a directory

mv old-folder/ new-location/

Rename a directory

mv src/ source/ ```

Useful mv Flags

FlagPurpose
`-i`Ask before overwriting
`-v`Verbose — show what is being moved
`-n`Do not overwrite existing files

```bash

Safe move — confirm before overwriting

mv -i *.log ~/archive/ ```


4. Deleting Files — `rm`

`rm` permanently deletes files. There is no trash can. There is no undo.

```bash

Delete a single file

rm temp.txt

Delete multiple files

rm file1.txt file2.txt file3.txt

Delete with confirmation

rm -i important-file.txt ```

Deleting Directories — `rm -r`

```bash

Delete an empty directory

rmdir empty-folder/

Delete a directory and everything inside

rm -r old-project/ ```

⚠️ DANGER ZONE — `rm -rf`

🚨 WARNING: `rm -rf` is the most dangerous command in Linux/macOS.

It deletes recursively (`-r`) and forces deletion without asking (`-f`). There is no confirmation, no trash, and no undo.

```bash

⚠️ Dangerous — deletes everything inside build/

rm -rf build/

🚨 NEVER DO THIS — deletes your ENTIRE system

rm -rf / ← DO NOT RUN

🚨 NEVER DO THIS — deletes your entire home directory

rm -rf ~ ← DO NOT RUN

🚨 EXTREMELY DANGEROUS — a typo can be fatal

rm -rf / tmp ← the space makes it delete / AND tmp

```

Undo? There Is None.

Unlike your desktop where deleted files go to the Recycle Bin or Trash, `rm` permanently erases the file from disk. No recovery tool will reliably bring it back.

Rules to stay safe:

  1. Always double-check your `rm` command before pressing Enter
  2. Use `rm -i` to get confirmation prompts
  3. Use `ls` first to preview what will be deleted
  4. Never use `rm -rf` with variables — if `$DIR` is empty, `rm -rf $DIR/` becomes `rm -rf /`
  5. Use `trash-cli` if you want a safety net: `trash-put file.txt`

5. Viewing File Contents — `cat`

`cat` prints the entire file to the terminal.

```bash

Display file contents

cat readme.txt

Display with line numbers

cat -n script.py

Combine multiple files

cat header.html body.html footer.html > page.html ```

Warning: Do not `cat` binary files (images, executables). You will see garbage characters and may corrupt your terminal. Use `file` first to check.


6. Partial Viewing — `head` and `tail`

When a file is large, you do not want to print all of it.

```bash

First 10 lines (default)

head server.log

First 20 lines

head -n 20 server.log

Last 10 lines (default)

tail server.log

Last 50 lines

tail -n 50 server.log

Follow a log file in real-time

tail -f server.log ```

Pro tip: `tail -f` is invaluable for watching logs as your app runs. Press `Ctrl+C` to stop.


7. Scrollable Viewing — `less` and `more`

For browsing large files interactively:

```bash

Open file in scrollable viewer

less large-file.log

Older, simpler viewer

more large-file.log ```

`less` Navigation

KeyAction
`Space` / `f`Page down
`b`Page up
`g`Go to beginning
`G`Go to end
`/pattern`Search forward
`?pattern`Search backward
`n`Next search result
`q`Quit

Remember: "less is more" — `less` is the improved version of `more`.


8. Counting — `wc`

`wc` (word count) counts lines, words, and characters.

```bash

Full count: lines, words, characters

wc essay.txt

42 318 1847 essay.txt

Count only lines

wc -l server.log

15230 server.log

Count words

wc -w essay.txt

Count characters

wc -c data.csv

Count lines in multiple files

wc -l src/*.ts ```


9. Comparing Files — `diff`

`diff` shows the differences between two files.

```bash

Basic diff

diff original.txt modified.txt

Side-by-side comparison

diff -y file1.txt file2.txt

Unified format (like git diff)

diff -u old-version.js new-version.js

Brief — just say if files differ

diff -q file1.txt file2.txt ```

Reading diff Output

``` < This line was removed

This line was added


2c2 ← line 2 was changed 5a6 ← line 6 was added after line 5 8d7 ← line 8 was deleted ```


10. Checking File Type — `file`

Not sure what a file is? `file` inspects it:

```bash file mystery-file

mystery-file: PNG image data, 800 x 600

file script.sh

script.sh: Bourne-Again shell script, ASCII text executable

file data.bin

data.bin: gzip compressed data

file notes.txt

notes.txt: UTF-8 Unicode text

```

Use `file` before `cat` to avoid dumping binary data into your terminal.


Windows Equivalents

Terminal CommandWindows CMDWindows PowerShellWhat It Does
`touch file.txt``type nul > file.txt``New-Item file.txt`Create empty file
`cp src dst``copy src dst``Copy-Item src dst`Copy file
`cp -r src dst``xcopy /s src dst``Copy-Item -Recurse src dst`Copy directory
`mv src dst``move src dst``Move-Item src dst`Move / rename
`rm file``del file``Remove-Item file`Delete file
`rm -r dir``rmdir /s dir``Remove-Item -Recurse dir`Delete directory
`cat file``type file``Get-Content file`Display contents
`head -n 5 file`N/A`Get-Content file -Head 5`First lines
`tail -n 5 file`N/A`Get-Content file -Tail 5`Last lines
`wc -l file``find /c /v "" file``(Get-Content file).Count`Count lines
`diff a b``fc a b``Compare-Object (gc a) (gc b)`Compare files
`file mystery`N/AN/ACheck file type

Practice Exercises

Exercise 1: File Creation Workflow

```bash

Create a project structure

mkdir -p my-project/{src,tests,docs} touch my-project/src/index.js touch my-project/tests/index.test.js touch my-project/docs/README.md echo "console.log('hello');" > my-project/src/index.js ```

Exercise 2: Backup and Restore

```bash

Create a backup

cp -r my-project/ my-project-backup/

Make changes

echo "// new code" >> my-project/src/index.js

Compare with backup

diff my-project/src/index.js my-project-backup/src/index.js

Restore from backup if needed

cp my-project-backup/src/index.js my-project/src/index.js ```

Exercise 3: Log Investigation

```bash

Create a fake log

for i in {1..100}; do echo "Line $i: log entry"; done > test.log

Count the lines

wc -l test.log

View the first 5 lines

head -n 5 test.log

View the last 5 lines

tail -n 5 test.log

Search through it

less test.log ```

Exercise 4: Safe Deletion Practice

```bash

Always preview first

ls my-project-backup/

Use interactive mode

rm -ri my-project-backup/

Verify it is gone

ls my-project-backup/

ls: cannot access 'my-project-backup/': No such file or directory

```


Command Cheat Sheet

CommandExamplePurpose
`touch``touch file.txt`Create empty file
`cp``cp a.txt b.txt`Copy file
`cp -r``cp -r dir/ backup/`Copy directory
`mv``mv old.txt new.txt`Move or rename
`rm``rm file.txt`Delete file
`rm -r``rm -r dir/`Delete directory
`cat``cat file.txt`Show file contents
`head``head -n 20 file`Show first lines
`tail``tail -n 20 file`Show last lines
`tail -f``tail -f log.txt`Follow live
`less``less big-file.txt`Scrollable viewer
`wc``wc -l file.txt`Count lines
`diff``diff a.txt b.txt`Compare files
`file``file mystery`Check file type

Key Takeaways

  1. `touch` creates empty files; use `echo` or `cat` for files with content
  2. `cp -r` is required for directories; `cp` alone only copies files
  3. `mv` handles both moving and renaming — no separate rename command
  4. `rm` is permanent — there is no trash, no undo, no recovery
  5. `rm -rf` is dangerous — always double-check before running it
  6. Use `cat` for small files, `less` for large files, `head`/`tail` for quick peeks
  7. `diff` compares files — essential for reviewing changes before committing
  8. `file` tells you what something is — use it before opening unknown files