Create, copy, move and delete
Make files and directories, peek inside them, and use wildcards and braces to handle many at once.
- 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:
| Command | Does | Example |
|---|---|---|
touch | creates an empty file (or updates its timestamp) | touch notes.txt |
mkdir | makes a directory (-p: with parents, no error if it exists) | mkdir -p logs/2026 |
cp | copies (-r for directories) | cp notes.txt backup.txt |
mv | moves or renames | mv draft.txt final.txt |
rm | removes files (-r for directories) | rm old.txt |
rmdir | removes an empty directory | rmdir empty/ |
When the last argument of cp or mv is an existing directory, the files go into it: mv a.txt b.txt archive/.
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.txtbay: archive cargo.txt bay/archive: manifest.bak 6 crates of coffee
Look inside files
cat fileprints a whole file (its name is short for concatenate:cat a bprints both).head -n 5 fileprints the first 5 lines;tail -n 5 filethe last 5.tail -fkeeps watching a log as it grows - great for live debugging.wc -l filecounts lines (-wwords,-cbytes).less fileopens a scrollable viewer: arrows to move,/to search,qto quit.
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.txt1 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:
| Pattern | Matches | Example |
|---|---|---|
* | any characters (including none) | *.log → every file ending in .log |
? | exactly one character | deck?.txt → deck1.txt, deckA.txt |
[...] | one character from the set | deck[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, soreport.{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.
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}.tardeck1.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,mvandrm -rcreate, copy, move, rename and delete.rmis permanent - check withlsorechofirst.cat,head,tail,wcandlessshow 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.
Tidy the cargo bay
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
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.
Build the archive with braces
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
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…