Making, moving and deleting files
mkdir, touch, cp, mv and rm, plus the one fact that matters most: the terminal has no trash can. Part 2 of the Terminal Basics series.
By the end of this tutorial you will know how to:
- make directories (
mkdir) and empty files (touch) - copy files and whole directories (
cp) - move and rename things (
mv) - delete files and directories (
rm,rmdir) - use wildcards to match many files at once
Prerequisites: you can use pwd, ls and cd from part 1. If you skipped it, the commands are small and each one is explained as it appears.
How this was checked: every command was run on Ubuntu 24.04 with bash 5.2 and GNU coreutils 9.4, in a real terminal session as an ordinary (non-admin) user, and the output shown is the real output. Your username will differ from ours (gerald).
To follow along: type each command and press Enter. Every destructive command in this post runs inside the practice mansion, never against your real files.
1. The terminal has no trash can
On a normal desktop, deleting a file moves it to a trash can. You can change your mind. You can open the lid and fish it back out.
The terminal has no trash can. When you tell it to delete something, it deletes it, immediately, without asking whether you are sure. There is no confirmation, no undo, and no lid. What you delete is gone.
This is not a flaw. It is why the terminal is fast, and why it is trusted for serious work. It is also why this series practises in a mansion built to be demolished. Nobody ever needs a funeral for a haunted house.
So the deal is simple: in the mansion, be reckless, because that is how you learn. Anywhere else, read the command twice before you press Enter.
2. Rebuild the mansion
Every post starts from a fresh mansion. Run the setup script you saved in part 1 (if you do not have it, the whole script is in that post):
bash ~/mansion-setup.shThe mansion is built. Try not to wake anyone.Then step inside:
cd ~/mansion
lsattic cellar kitchen library readme.txt3. mkdir: build a room
mkdir means make directory. Give it a name and it creates one in the working directory:
mkdir ballroom
lsattic ballroom cellar kitchen library readme.txtLike every command that works, mkdir printed nothing. You can see the new room in the listing.
To build a room inside a room inside a room, you would normally have to create each one in turn. The -p option (for "parents") builds the whole chain in one go:
mkdir -p ballroom/stage/curtains
ls ballroom
ls ballroom/stagestage
curtainsWithout -p, mkdir refuses to build a room inside one that does not exist yet:
mkdir ballroom/chandelier/crystalsmkdir: cannot create directory ‘ballroom/chandelier/crystals’: No such file or directory4. touch: create an empty file
touch creates an empty file, ready to be filled later:
touch ballroom/guestbook.txt
ls ballroomguestbook.txt stage(touch has a second job, updating the time a file was last changed, but creating empty files is what you will use it for most.)
ls ballroom should list guestbook.txt and stage. If a name is missing, check you ran the mkdir and touch commands from the right directory. pwd tells you where you are.
5. cp: copy things
cp means copy. Give it the file to copy, then where the copy should go. If the destination is a directory, the copy lands inside it with the same name:
cp readme.txt ballroom
ls ballroomguestbook.txt readme.txt stageIf the destination is a new name, the copy gets that name instead:
cp readme.txt ballroom/welcome.txt
ls ballroomguestbook.txt readme.txt stage welcome.txtcp will not copy a directory alone
Try to copy a whole directory and cp refuses:
cp attic attic-copycp: -r not specified; omitting directory 'attic'It wants the -r option (for recursive, meaning "everything inside, and everything inside that"):
cp -r attic attic-copy
ls attic-copycobwebs.txt trunkcp and mv overwrite silently
Here is the first lesson about the missing trash can. We just copied readme.txt to ballroom/welcome.txt. Now copy something completely different to the same name, then look inside the file with cat, which prints a file's contents (the next post covers it properly):
cp kitchen/recipe.txt ballroom/welcome.txt
cat ballroom/welcome.txtSoup: water, onion, bad decisions.The welcome message was replaced by the soup recipe. cp did not ask, did not warn, and did not keep the old one. If the destination exists, it is quietly overwritten.
6. mv: move and rename
mv means move. It takes the same two arguments as cp, but the original does not stay behind. Move the guestbook from the ballroom to the library:
mv ballroom/guestbook.txt library
ls librarydiary.txt guestbook.txt guests.txtThe same command renames things, because renaming is just moving something to a new name in the same place:
mv library/guestbook.txt library/ledger.txt
ls librarydiary.txt guests.txt ledger.txtUnlike cp, mv moves a directory without any extra option:
mv attic-copy cellar
ls cellarattic-copy vault.sh wine-list.txt7. rm: delete
rm means remove. Delete the soup recipe we copied into the ballroom:
rm ballroom/welcome.txt
ls ballroomreadme.txt stageIt is gone. Not in a trash can, not hidden: gone. You can confirm it:
ls ballroom/welcome.txtls: cannot access 'ballroom/welcome.txt': No such file or directoryDeleting directories
rm alone will not delete a directory:
rm ballroomrm: cannot remove 'ballroom': Is a directoryThere is a gentler command, rmdir, which only removes directories that are empty, so it is hard to do damage with:
mkdir empty-room
rmdir empty-room
rmdir ballroomrmdir: failed to remove 'ballroom': Directory not emptyThe empty room vanished without a word, but the ballroom refused, because it still has things in it.
rm -r deletes a directory and everything inside it, no questions asked. There is no trash can, no undo, and no confirmation. In the mansion, this is a demolition permit. Anywhere else on your computer, it is a career change.
Before you ever use it outside the mansion, type the path with ls first, and read it twice.
Demolish the ballroom:
rm -r ballroom
lsattic cellar kitchen library readme.txt8. Wildcards: match many files at once
A wildcard is a character that stands in for other characters. The most useful is *, which matches any run of characters, including none:
ls *.txtreadme.txtThat finds every file in the mansion's front hall whose name ends in .txt. Wildcards also work inside paths. This finds every .txt file exactly one directory down:
ls */*.txtattic/cobwebs.txt kitchen/recipe.txt library/guests.txt
cellar/wine-list.txt library/diary.txt library/ledger.txtHere is the important part. You do not expand the wildcard, the shell does, before the command ever runs. echo just prints what it receives, so it shows you exactly what rm or cp would get:
echo library/led*library/ledger.txtThat is why rm *.log is powerful and dangerous in equal measure: it deletes every match. Let it loose on the kitchen's complaints:
rm kitchen/*.log
ls kitchenrecipe.txt9. Common errors
Here are the errors from this post, in one place:
| Error | What it usually means | Fix |
|---|---|---|
mkdir: cannot create directory ... No such file or directory | A parent directory does not exist | Use mkdir -p |
cp: -r not specified; omitting directory | You tried to copy a directory | Add -r |
rm: cannot remove ... Is a directory | You tried to rm a directory | rmdir if empty, rm -r if not (carefully) |
rmdir: failed to remove ... Directory not empty | There is still something inside | Empty it first, or use rm -r |
cannot stat ... No such file or directory | The file you named does not exist | Check the spelling and pwd |
Here is the last one, which is the one you will see most:
cp ghost.txt librarycp: cannot stat 'ghost.txt': No such file or directoryPractice
Predict the result before you run each one. Answers follow. First, a fresh mansion, so your listings match ours:
bash ~/mansion-setup.shThe mansion is built. Try not to wake anyone.Exercise 1. Create the chain of directories dungeon/cell-1/shackles with one command.
Exercise 2. Copy the whole library directory, with everything in it, to a new directory called library-backup.
Exercise 3. Rename kitchen/recipe.txt to kitchen/soup.txt.
Answers
Exercise 1. -p builds the whole chain at once:
cd ~/mansion
mkdir -p dungeon/cell-1/shackles
ls dungeon/cell-1shacklesExercise 2. A directory needs -r:
cp -r library library-backup
ls library-backupdiary.txt guests.txtNotice the hidden file .boo is not in that list. ls hides it, but it was copied too:
ls -a library-backup. .. .boo diary.txt guests.txtExercise 3. Renaming is moving to a new name in the same place:
mv kitchen/recipe.txt kitchen/soup.txt
ls kitchencomplaints.log soup.txtWhat you learned
- The terminal has no trash can. Deletes are immediate and permanent, so practise in the mansion.
mkdirmakes directories (-pfor a whole chain) andtouchmakes empty files.cpcopies. Directories need-r. It silently overwrites anything already at the destination.mvmoves and renames, and it also overwrites silently.rmdeletes files,rmdirdeletes empty directories, andrm -rdeletes a directory and everything in it.*is a wildcard. The shell expands it before the command runs, soechoshows you what a command is about to receive.
Next in the series: Reading and searching files. Now that you can make things, you will learn to read them, and to hunt for the one line you need among thousands.