Linux Basics Lesson 5 of 38
Create, copy, move
10 minutes
Learning goals
- Create whole folder paths with mkdir -p and empty files with touch.
- Copy files and folders with cp, cp -r and cp -p, and make a backup before you change a config.
- Move and rename with mv, and check first what cp and mv would overwrite.
You know these commands from Terminal-Start, lesson 4 and lesson 6. Here come the options you need on a server.
Create: mkdir and touch
mkdir -p creates a whole path in one go and does not complain about parts that exist. Without -p, mkdir a/b/c stops
as soon as a is missing. That also makes -p safe in scripts, because it can run twice.
mkdir -p staging/src/tests
Purpose: Creates folders. -p creates the whole path and accepts parts that exist.
Syntax: mkdir [-p] FOLDER...
Options:
Example:
mkdir -p staging/src/tests
ls -R staging
staging:
src
staging/src:
tests
staging/src/tests:touch creates an empty file. If the file exists, only its time is set to now, which is where the name comes from.
touch TODO.md
Purpose: Creates an empty file, or sets the time of an existing one to now.
Syntax: touch FILE...
Example:
touch TODO.md
ls -l TODO.md
-rw-r--r-- 1 lerner lerner 0 Oct 17 09:42 TODO.mdCopy: cp
cp SOURCE TARGET copies a file. For a folder it needs -r, otherwise it refuses. And -p keeps time, owner and
permissions, which you want for a backup, because otherwise the copy pretends to be new.
cp notes.txt notes.txt.bak
Purpose: Copies files. Folders need -r. An existing target is overwritten.
Syntax: cp [OPTION]... SOURCE TARGET
Options:
Example:
cp notes.txt notes.txt.bak
cp -r projects/shop shop.bak
ls shop.bak
README.md app.conf build.log releases test.log
cp -p notes.txt keep.txt
ls -l notes.txt keep.txt
-rw-r--r-- 1 lerner lerner 76 Oct 17 08:15 keep.txt
-rw-r--r-- 1 lerner lerner 76 Oct 17 08:15 notes.txtMove and rename: mv
On Linux, renaming is just moving to a new name, so there is only one command for both. mv does not need -r, it takes
folders along as they are.
mv notes.txt todo.txt
Purpose: Moves or renames, also whole folders. An existing target is overwritten.
Syntax: mv [OPTION]... SOURCE TARGET
Options:
Example:
mv notes.txt todo.txt
mkdir old
mv todo.txt old/
ls old
todo.txtPractice
Terminal task 1/4
Mission
Create a folder called backup in your home folder and copy your notes (notes.txt) into it. The original stays where it is.
Task 1/4: Create a folder called backup in your home folder and copy your notes (notes.txt) into it. The original stays where it is.
Mission
Create a folder called backup in your home folder and copy your notes (notes.txt) into it. The original stays where it is.
Task complete
Solution
mkdir backup
cp notes.txt backup/ Why: The slash at the end says that backup is meant as a folder. If the folder were missing, cp would refuse instead of quietly creating a file called backup.
See the solution? You get fewer XP for this task.
Start over? Files, folders and output go back to the start. Your hints stay.
Loading terminal …
Esc, then Tab leaves the terminal
Not done yet
Repeat: you have solved this task before.
Terminal task 2/4
You are about to change projects/shop/app.conf. First make a copy next to it, called app.conf.bak, that keeps the original time.
Task 2/4: You are about to change projects/shop/app.conf. First make a copy next to it, called app.conf.bak, that keeps the original time.
You are about to change projects/shop/app.conf. First make a copy next to it, called app.conf.bak, that keeps the original time.
Task complete
Solution
cp -p projects/shop/app.conf projects/shop/app.conf.bak Why: If the change goes wrong, one cp the other way round brings back the old state in seconds.
Another way
cp projects/shop/app.conf projects/shop/app.conf.bak Without -p the content is the same, but the copy gets the current time. Later you can no longer tell from ls -l when the config last changed.
See the solution? You get fewer XP for this task.
Start over? Files, folders and output go back to the start. Your hints stay.
Loading terminal …
Esc, then Tab leaves the terminal
Not done yet
Repeat: you have solved this task before.
Terminal task 3/4
Copy the whole folder projects/shop to shop.bak in your home folder.
Task 3/4: Copy the whole folder projects/shop to shop.bak in your home folder.
Copy the whole folder projects/shop to shop.bak in your home folder.
Task complete
Solution
cp -r projects/shop shop.bak Why: -r takes everything below the folder along. mv would have worked too, but then the original would be gone, and that is no backup.
See the solution? You get fewer XP for this task.
Start over? Files, folders and output go back to the start. Your hints stay.
Loading terminal …
Esc, then Tab leaves the terminal
Not done yet
Repeat: you have solved this task before.
Terminal task 4/4
Rename projects/shop/README.md to projects/shop/ABOUT.md.
Task 4/4: Rename projects/shop/README.md to projects/shop/ABOUT.md.
Rename projects/shop/README.md to projects/shop/ABOUT.md.
Task complete
Solution
mv projects/shop/README.md projects/shop/ABOUT.md Why: mv changes only the entry in the folder. The content, its time and its owner stay as they are.
See the solution? You get fewer XP for this task.
Start over? Files, folders and output go back to the start. Your hints stay.
Loading terminal …
Esc, then Tab leaves the terminal
Not done yet
Repeat: you have solved this task before.
Predict the output
Command
cp projects copyBefore the command, in your home folder ~
- ~/projects/
- ~/projects/shop/
- ~/projects/shop/README.md
- ~/projects/shop/app.conf
- ~/projects/shop/build.log
- ~/projects/shop/test.log
- ~/projects/shop/releases/
- ~/projects/shop/releases/v2.4.0/
- ~/projects/shop/releases/v2.4.0/VERSION
- ~/projects/shop/releases/v2.4.1/
- ~/projects/shop/releases/v2.4.1/VERSION
Check works once you have chosen an answer.
Fill the gap
mkdir needs the option to create a/b/c if a does not exist yet.
mkdir needs the option … to create a/b/c if a does not exist yet.
Check works once you have typed an answer.
Next up: deleting for good, and two kinds of links.
Quiz
-
Check works once you have chosen an answer.
-
Check works once you have chosen an answer.
-
Check works once you have chosen an answer.
Cards
-
Card 1 of 3
Tap the card or press Space to turn it over.
-
Card 2 of 3
Tap the card or press Space to turn it over.
-
Card 3 of 3
Tap the card or press Space to turn it over.
End of the lesson
Still open: 6 exercises, 3 quiz questions
What you can do now: mkdir -p staging/src/tests, touch TODO.md, cp notes.txt notes.txt.bak, mv notes.txt todo.txt
+0 XP in this lesson
- Lesson 1: open
- Lesson 2: open
- Lesson 3: open
- Lesson 4: open
- Lesson 5: done
- Lesson 6: open
- Lesson 7: open
- Lesson 8: open
- Lesson 9: open
- Lesson 10: open
- Lesson 11: open
- Lesson 12: open
- Lesson 13: open
- Lesson 14: open
- Lesson 15: open
- Lesson 16: open
- Lesson 17: open
- Lesson 18: open
- Lesson 19: open
- Lesson 20: open
- Lesson 21: open
- Lesson 22: open
- Lesson 23: open
- Lesson 24: open
- Lesson 25: open
- Lesson 26: open
- Lesson 27: open
- Lesson 28: open
- Lesson 29: open
- Lesson 30: open
- Lesson 31: open
- Lesson 32: open
- Lesson 33: open
- Lesson 34: open
- Lesson 35: open
- Lesson 36: open
- Lesson 37: open
- Lesson 38: open
Cheat sheet
-
mkdir -p a/b/ccreate a whole path -
cp -r <folder> <copy>copy a folder with everything in it -
cp -p <file> <file>.bakbackup with time and owner -
mv <old> <new>rename or move -
cp -i, mv -iask before overwriting
The most important command lines of this module on one A4 page, black and white, to print or keep on your phone.
- Download PDF (PDF, 19 KB)
- View as image (PNG, 63 KB)