Um momento
0x20Lesson 3 of 18

Create, copy, move and delete

Make files and directories, peek inside them, and use wildcards and braces to handle many at once.

25 min 7-question quiz 2 code exercises
By the end of this lesson you can
  • Create, copy, move, rename and delete files and directories
  • Read files with cat, head, tail and wc
  • Match many files with globs (*, ?, [...]) and generate names with brace expansion

The cargo bay is a mess and you’ve been asked to sort it out. Here’s your toolkit:

CommandDoesExample
touchcreates an empty file (or updates its timestamp)touch notes.txt
mkdirmakes a directory (-p: with parents, no error if it exists)mkdir -p logs/2026
cpcopies (-r for directories)cp notes.txt backup.txt
mvmoves or renamesmv draft.txt final.txt
rmremoves files (-r for directories)rm old.txt
rmdirremoves an empty directoryrmdir empty/

When the last argument of cp or mv is an existing directory, the files go into it: mv a.txt b.txt archive/.

cargo-bay.sh
1cd "$(mktemp -d)"
2mkdir -p bay/archive
3touch bay/manifest.txt
4echo "6 crates of coffee" > bay/manifest.txt   # > writes text into a file
5
6cp bay/manifest.txt bay/manifest.bak           # copy
7mv bay/manifest.bak bay/archive/               # move into a directory
8mv bay/manifest.txt bay/cargo.txt              # rename
9ls bay bay/archive
10cat bay/cargo.txt
Output
bay:
archive
cargo.txt

bay/archive:
manifest.bak
6 crates of coffee

Look inside files

  • cat file prints a whole file (its name is short for concatenate: cat a b prints both).
  • head -n 5 file prints the first 5 lines; tail -n 5 file the last 5. tail -f keeps watching a log as it grows - great for live debugging.
  • wc -l file counts lines (-w words, -c bytes).
  • less file opens a scrollable viewer: arrows to move, / to search, q to quit.
peek.sh
1cd "$(mktemp -d)"
2seq 1 10 > countdown.txt     # seq prints the numbers 1 to 10, one per line
3head -n 3 countdown.txt
4echo "..."
5tail -n 2 countdown.txt
6wc -l countdown.txt
Output
1
2
3
...
9
10
10 countdown.txt

Wildcards: one pattern, many files

Typing every filename gets old fast. Globs are patterns that the shell expands into matching filenames before the command runs:

PatternMatchesExample
*any characters (including none)*.log → every file ending in .log
?exactly one characterdeck?.txt → deck1.txt, deckA.txt
[...]one character from the setdeck[12].txt → deck1.txt, deck2.txt
[!...]one character not in the set[!a]* → names not starting with a

Globs don’t match hidden files unless the pattern starts with a dot, and they come out sorted. If nothing matches, Bash leaves the pattern as-is - so ls *.xyz complains about a file literally called *.xyz.

Brace expansion is different: it generates words whether or not files exist. It’s perfect for creating things.

  • {a,b,c} → a b c, so report.{txt,csv} → report.txt report.csv
  • {1..5} → 1 2 3 4 5, and {01..03} keeps the zeros: 01 02 03
  • They combine: mkdir -p logs/{2025,2026}/{jan,feb} creates four directories in one go.
globs.sh
1cd "$(mktemp -d)"
2touch deck{1..3}.log deck{1..2}.txt crew.csv
3echo deck*.log
4echo deck?.txt
5echo *.[ct]sv
6echo deck[!1]*
7echo backup-{mon,tue}.tar
Output
deck1.log deck2.log deck3.log
deck1.txt deck2.txt
crew.csv
deck2.log deck2.txt deck3.log
backup-mon.tar backup-tue.tar

Key takeaways

  • touch, mkdir -p, cp -r, mv and rm -r create, copy, move, rename and delete.

  • rm is permanent - check with ls or echo first.

  • cat, head, tail, wc and less show what’s inside files.

  • Globs (*, ?, [...]) match existing files; braces ({a,b}, {1..5}) generate words.

Lesson quiz

7 questions · pass with 5 correct · up to 50 XP

Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.

Practice: write Bash scripts

Write a script in the editor and run it for real against sample input. Each run gets a fresh Linux sandbox with Bash 5.2 and the GNU tools on Wandbox, a free public service - so experiment freely, even with rm. Your script and test input are sent there.

Exercise 1

Tidy the cargo bay

+25 XP

The starter code dumps files into bay. Make directories bay/logs and bay/manifests, then use globs to move every .log file into bay/logs and every .csv file into bay/manifests. Delete bay/junk.tmp. Finally, the provided ls commands print the result.

  • Files are sorted into place
script.sh
Loading editor…

Your script runs with Bash 5.2 and GNU tools on Wandbox, a free public service, in a fresh sandbox each time. Your script and test input are sent to that service.

Exercise 2

Build the archive with braces

+25 XP

Using one mkdir -p command with brace expansion, create archive/2026/q1, archive/2026/q2, archive/2026/q3 and archive/2026/q4. Then, with one touch command, create report-01.txt through report-05.txt inside archive/2026/q4. The ls commands at the end show what you built.

  • Builds the archive
script.sh
Loading editor…

Your script runs with Bash 5.2 and GNU tools on Wandbox, a free public service, in a fresh sandbox each time. Your script and test input are sent to that service.

Questions about this lesson

Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.

Loading posts…

Gostou da aula? 😆👍
Apoie nosso trabalho com uma doação: