AI Learning Hub › Learn AI › Self-Paced Courses › Terminal on Mac: Reading Commands Before You Run Them › Making, Copying, and Moving Things
Module 4 ยท Beginner
Making, Copying, and Moving Things
Creating folders and files, and the first commands that will replace something without asking you first.
By the end
- Create folders and empty files
- Copy a file, and copy a folder
- Move and rename, which turn out to be the same operation
- Explain why nothing asked you before replacing something
These are the commands that build things, and they are a gentle introduction with one sharp edge. Everything here is reversible by hand except the moment where a copy lands on top of something that was already there.
Work inside ~/terminal-practice for all of it.
Making things
| Command | What it does |
|---|---|
mkdir | Makes a directory — a folder. Make directory. |
touch | Creates an empty file if that name does not exist. If it does exist, it leaves the contents alone and updates the file’s timestamps. |
That second description is longer than people expect, and it is worth the extra clause. touch is widely described as the command that creates a file, which is true right up until you run it on a file that already exists — at which point it does something entirely different and completely silent. Nothing is lost, but nothing is created either.
Try it
mkdir drafts touch drafts/summary.txt ls drafts
Copying and moving
cp source destination- Copies a file. The original stays where it is.
cp -R source destination- Copies a folder and everything inside it. Without the
-R,cprefuses a folder and says so. mv source destination- Moves a file. The original does not stay.
Renaming is the surprise. There is no rename command, because moving a file to a new name in the same folder is renaming it. mv notes.txt old-notes.txt renames; mv notes.txt archive/ moves. Same command, and the difference is entirely in the destination.
Try it
cp cases/opinion-a.txt archive/ ls archive mv archive/opinion-a.txt archive/first-opinion.txt ls archive
Nothing is going to ask you
Here is the sharp edge, and it is worth meeting deliberately in a folder where it does not matter.
When cp or mv lands on a destination that already exists, the stock commands tested for this course replaced it without a prompt, a warning, or any output at all. The old contents are gone and the terminal says nothing. Compare that with dragging a file onto a folder in Finder, which asks.
Do not expect to be asked. Some people configure their shells to make these commands interactive, so a colleague’s Mac may behave differently from yours. The safe assumption is the stock one: check the destination before you press Return, because the command will not check it for you.
Prove it to yourself. The practice folder is the right place.
Try it — in the practice folder only
cp cases/opinion-a.txt drafts/summary.txt head -1 drafts/summary.txt cp cases/opinion-b.txt drafts/summary.txt head -1 drafts/summary.txt
The file now holds the second opinion. The first copy is gone, and at no point did anything indicate that a replacement had happened. That is the behavior to carry into Module 5, where the stakes are higher, and into Module 9, where somebody else wrote the command.
Exercise: predict the destination
For each line below, write down what you expect before running it. Pay particular attention to whether the destination is a folder that exists, a folder that does not, or a filename.
cp notes/case-list.txt archive/cp notes/case-list.txt archive/list-copy.txtmv drafts/summary.txt drafts/final.txtcp cases output/cp -R cases output/
What you should have found
1 puts a copy inside archive, keeping the name. 2 puts a copy inside archive under a new name. Both work because archive exists; if it did not, cp would have made a file called archive instead, which is a common and confusing mistake.
3 renames. The file does not move anywhere.
4 fails: cp: cases is a directory (not copied). Copying a folder needs -R, and the message says so clearly enough that you will recognize it next time.
5 works, and copies the folder with both opinions inside it.
Start the next module from a clean folder
You have just created, copied and renamed a fair amount inside the practice folder. Reset it before you go on: delete the whole terminal-practice folder in Finder and unzip the download again.
This is not housekeeping for its own sake. Modules 6 and 7 tell you how many results a search should return, and those numbers assume the folder holds what it shipped with. Unzipping again is the intended recovery for every exercise in this course, and it takes a few seconds.