terminal.cab

Linux Basics Lesson 5 of 38

guestLevel 1Badges and progress0/100 XP, XP to the next levelStreak0

Create, copy, move

10 minutes

Learning goals

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:

  • -p create missing parents, no error if it exists

Example:

mkdir -p staging/src/tests
ls -R staging
staging:
src

staging/src:
tests

staging/src/tests:

in the glossary: mkdir

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.md

in the glossary: touch

Copy: 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:

  • -r copy folders with everything in them
  • -p keep time, owner and permissions
  • -i ask before overwriting

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.txt

in the glossary: cp

Move 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:

  • -i ask before overwriting

Example:

mv notes.txt todo.txt
mkdir old
mv todo.txt old/
ls old
todo.txt

in the glossary: mv

Practice

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.

Loading terminal …

lerner@web-01: ~bash
lerner@web-01:~$

Esc, then Tab leaves the terminal

Not done yet

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.

Loading terminal …

lerner@web-01: ~bash
lerner@web-01:~$

Esc, then Tab leaves the terminal

Not done yet

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.

Loading terminal …

lerner@web-01: ~bash
lerner@web-01:~$

Esc, then Tab leaves the terminal

Not done yet

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.

Loading terminal …

lerner@web-01: ~bash
lerner@web-01:~$

Esc, then Tab leaves the terminal

Not done yet

Predict the output

Command

cp projects copy

Before 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
What does cp projects copy print?

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

  1. Question 1 of 3What is the safe way to prepare for changing a config?

    Check works once you have chosen an answer.

  2. Question 2 of 3What does touch do to a file that already exists?

    Check works once you have chosen an answer.

  3. Question 3 of 3You run mv draft.txt report.txt, and report.txt already exists. What happens?

    Check works once you have chosen an answer.

Cards

  1. Card 1 of 3

    Tap the card or press Space to turn it over.

  2. Card 2 of 3

    Tap the card or press Space to turn it over.

  3. 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

Next: lesson 6, Delete, link, measure (10 min)

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

  1. Lesson 1: open
  2. Lesson 2: open
  3. Lesson 3: open
  4. Lesson 4: open
  5. Lesson 5: done
  6. Lesson 6: open
  7. Lesson 7: open
  8. Lesson 8: open
  9. Lesson 9: open
  10. Lesson 10: open
  11. Lesson 11: open
  12. Lesson 12: open
  13. Lesson 13: open
  14. Lesson 14: open
  15. Lesson 15: open
  16. Lesson 16: open
  17. Lesson 17: open
  18. Lesson 18: open
  19. Lesson 19: open
  20. Lesson 20: open
  21. Lesson 21: open
  22. Lesson 22: open
  23. Lesson 23: open
  24. Lesson 24: open
  25. Lesson 25: open
  26. Lesson 26: open
  27. Lesson 27: open
  28. Lesson 28: open
  29. Lesson 29: open
  30. Lesson 30: open
  31. Lesson 31: open
  32. Lesson 32: open
  33. Lesson 33: open
  34. Lesson 34: open
  35. Lesson 35: open
  36. Lesson 36: open
  37. Lesson 37: open
  38. Lesson 38: open
1 of 38

Next: lesson 6, Delete, link, measure (10 min) That's enough for today

Path complete

You finished all 38 lessons of Linux Basics.

Back to the path

Cheat sheet

The most important command lines of this module on one A4 page, black and white, to print or keep on your phone.