The Academy

Loading the search index…

Searches the academy on this device only.

Making, copying and moving: mkdir, touch, cp and mv

Create folders and empty files, copy them, move them and rename them, always inside ~/practice. You also learn how a copy or move can silently overwrite a file, and meet rm as a clearly flagged danger.

30 minbeginnerUbuntu/LinuxPractical task needs a laptop or desktop

Marking a lesson as read does not prove you can do it. Skills count as demonstrated when you pass the quiz (not yet) and complete the practical tasks (not yet).

By the end of this lesson you will be able to

  • Create folders with mkdir and empty files with touch inside ~/practice.
  • Copy a file with cp and a folder with cp -r, and move or rename with mv.
  • Explain how cp and mv can overwrite a file silently, and use -i to be asked first.
  • Explain why rm cannot be undone, and use rm -i only on practice files.

Until now every command only looked. This lesson is different: these commands change things on your disk. That is why the whole lesson happens inside the practice folder you made, and why the risky parts are marked clearly. The skills are the same ones you use in a file manager when you make a folder, copy a document or rename a photo. Doing them by typing is faster once you know them, and it is how scripts and servers work.

Making folders and files

mkdir (make directory) creates a new folder. touch creates a new, empty file if the name does not exist yet. If the file already exists, touch only updates its 'last changed' time and leaves the contents alone, so it is safe on existing files too. Give each command one or more names as arguments.

Terminal (Linux) · you can run this on your own computer
cd ~/practice
mkdir projects
touch notes.txt
touch a.txt b.txt
ls -l

Nothing is printed by the first four commands, which means they worked. ls -l then shows the result. The -rw-rw-r-- at the start of a file line is its permissions, explained in a later lesson. The 0 is the size in bytes: a new file is empty.

Simulated interface: not a real screenshot

Creating a folder and three empty files, then listing them (simulated).

amina@laptop: ~/practice (simulated)
  1. Marker 1: A new empty folder; no output
  2. Marker 2: Three new empty files in one command
  3. Marker 3: Sizes are 0 for files, and d marks the folder
Creating a folder and three empty files, then listing them (simulated).
Read this figure as text

A simulated terminal in the practice folder showing mkdir, touch and a long listing with one folder and three empty files.

A simulated bash terminal in ~/practice. Marker 1: mkdir projects prints nothing. Marker 2: touch notes.txt a.txt b.txt prints nothing. Marker 3: ls -l prints total 4 and four lines: a.txt with size 0, b.txt with size 0, notes.txt with size 0, each starting with a dash and the permissions -rw-rw-r--, and projects starting with d for directory and size 4096. Listing order is alphabetical.

To put some text in a file, use echo with >. The > sign sends the output of a command into a file instead of the screen. It is useful, but it comes with a trap, which is explained in the overwrite section below.

Terminal (Linux) · you can run this on your own computer
echo "Buy bread" > notes.txt

Copying: cp

cp (copy) needs two arguments: the source (what to copy) and the destination (where the copy goes). The original stays exactly where it is. If the destination is a folder, the copy goes inside it with the same name. If it is a new file name, the copy gets that name.

Terminal (Linux) · you can run this on your own computer
cp notes.txt notes-backup.txt
cp notes.txt projects
ls projects
Text
notes.txt

To copy a whole folder you must add the recursive option -r, which means 'and everything inside it'. Without it cp refuses, and the message tells you why.

Simulated interface: not a real screenshot

Copying a folder needs -r (simulated).

amina@laptop: ~/practice (simulated)
  1. Marker 1: A folder needs the -r option
  2. Marker 2: -r copies the folder and its contents
  3. Marker 3: Both folders now exist
Copying a folder needs -r (simulated).
Read this figure as text

A simulated terminal where cp on a folder fails with an omitting directory message and then succeeds with -r.

A simulated bash terminal in ~/practice. Marker 1: cp projects projects-copy fails with cp: -r not specified; omitting directory 'projects'. This means cp does not copy folders unless told to. Marker 2: cp -r projects projects-copy prints nothing and makes the copy. Marker 3: ls shows a.txt, b.txt, notes-backup.txt, notes.txt, projects and projects-copy.

Moving and renaming: mv

mv (move) takes the same two arguments as cp, but the original is gone afterwards. Linux has no separate rename command for everyday use: renaming is simply moving a file to a new name in the same folder.

Terminal (Linux) · you can run this on your own computer
mv a.txt first.txt
mv b.txt projects
ls
ls projects

The first line renames a.txt to first.txt. The second moves b.txt into the projects folder. After this, ls no longer shows a.txt or b.txt in the practice folder, and ls projects shows b.txt and notes.txt.

Diagram

cp leaves the original in place; mv takes it away.

cp notes.txt backup.txtCopy
  • Before: notes.txt
  • After: notes.txt and backup.txt
  • The original stays

Two files

mv notes.txt backup.txtMove or rename
  • Before: notes.txt
  • After: backup.txt only
  • The original is gone

One file, new name

cp leaves the original in place; mv takes it away.
Read this figure as text

Two columns comparing cp, which keeps the original and makes a second file, with mv, which leaves only one file in the new place.

Two columns. Column one, cp notes.txt backup.txt: before, one file notes.txt; after, two files notes.txt and backup.txt. Use it when you want to keep both. Column two, mv notes.txt backup.txt: before, one file notes.txt; after, one file backup.txt and notes.txt is gone. Use it to rename or relocate. Both commands silently replace a destination file that already has the same name.

The overwrite risk and the -i option

The -i option (interactive) makes cp and mv ask before replacing anything. Type y and Enter to allow it, or n and Enter (or just Enter) to cancel. Using -i while you are learning costs you nothing.

Simulated interface: not a real screenshot

mv -i asks before replacing an existing file (simulated).

amina@laptop: ~/practice (simulated)
  1. Marker 1: -i makes mv ask first
  2. Marker 2: Answering n cancels: nothing is replaced
  3. Marker 3: Both files are still there
mv -i asks before replacing an existing file (simulated).
Read this figure as text

A simulated terminal where mv -i asks whether to overwrite first.txt and the learner answers n to cancel.

A simulated bash terminal in ~/practice. Marker 1: mv -i notes.txt first.txt prints the question mv: overwrite 'first.txt'? and waits. Marker 2: the learner types n and presses Enter, so nothing is replaced. Marker 3: ls shows both notes.txt and first.txt still exist.

A word about rm

rm -i asks for confirmation for each file, which gives you one chance to stop. To remove an empty practice folder, rmdir is a safer choice, because it refuses to remove a folder that still has something inside.

Terminal (Linux) · you can run this on your own computer
touch scrap.txt
rm -i scrap.txt

rm -i prints rm: remove regular empty file 'scrap.txt'? Type y and press Enter to delete the empty file you just made. Answer n to keep it. For a file with content the message says 'regular file' instead of 'regular empty file'.

Practise in the simulated terminal

The simulated terminal from the previous lesson is also a safe place to try mkdir, touch, cp and mv. Nothing you do there reaches your real computer, so you can make mistakes on purpose.

Common mistakes

  • ✖ Instead of: Practising mv or rm in the Documents folder because 'it is only a test'.

    ✔ Do this: Only practise in ~/practice. Check with pwd before every change.

  • ✖ Instead of: Assuming cp or mv will warn you before replacing a file.

    ✔ Do this: They replace silently. Use -i so they ask first.

  • ✖ Instead of: Using rm -r or rm -rf to tidy a folder.

    ✔ Do this: Do not use them while learning. They delete everything inside with no way back. Use rmdir for empty folders and rm -i for single practice files.

  • ✖ Instead of: Putting a space in a new file name, such as touch my notes.txt.

    ✔ Do this: That makes two files, my and notes.txt. Use a hyphen, my-notes.txt, or quote the name.

If something goes wrong

cp: cannot stat 'notes.txt': No such file or directory
The source name is wrong or you are in the wrong folder. Run pwd and ls, then retype the name exactly.
mv: cannot move 'x' to 'y': Not a directory
You gave a path through something that is a file, not a folder. Check with ls -l that the destination is a d (directory) or a new name.
rmdir: failed to remove 'projects': Directory not empty
That is a safety feature: the folder still has files in it. Look with ls projects. Leave it alone, or empty it deliberately, file by file with rm -i, only if it is practice data.

Key terms

Overwrite
To replace an existing file with new contents so the old contents are lost.
Recursive
Applied to a folder and everything inside it, including folders within folders.

Practise it for real

Reading is not the same as being able to do it. Complete these on your own computer, then tick only what you have actually done. These ticks are your own honest record: nothing checks them for you.

Guided exerciseNeeds a laptop or desktop

Build and reorganise inside ~/practice

On your Ubuntu laptop run cd ~/practice and pwd. Make a folder projects. Create notes.txt with touch. Copy it to notes-backup.txt, then make a second folder archive and move the backup into it with mv. Rename notes.txt to todo.txt. Use ls, ls archive and ls -l to check each result.

You have succeeded when:
Independent challengeNeeds a laptop or desktop

Challenge: see an overwrite and stop it

In ~/practice make two files, one.txt and two.txt, with different text using echo "..." > file. Without using -i, predict what cp one.txt two.txt will do to the contents of two.txt, then check your prediction with cat two.txt (you will meet cat properly next lesson). Repeat the same idea with mv -i and answer n.

You have succeeded when:

Knowledge check

7 questions. You need 80% to pass. Mistakes here are part of learning, and you can retry as often as you like.

  1. Question 1.After running mv report.txt old-report.txt, what is true?
  2. Question 2.If b.txt already exists, cp a.txt b.txt will warn you before replacing it.
  3. Question 3.In ~/practice you type cp projects projects-copy and see: cp: -r not specified; omitting directory 'projects'.What should you do?
  4. Question 4.Match each command to what it does.
  5. Question 5.Why is rm more dangerous than deleting a file in the file manager?
  6. Question 6.Put the safe habit in order before changing files.
    1. Run pwd to check you are in ~/practice
    2. Read the line once more, then press Enter
    3. Run ls to check the exact names
    4. Type the command with -i if it has that option
  7. Question 7.What does echo "Hello" > greeting.txt do if greeting.txt already holds some text?

Summary

  • mkdir makes folders and touch makes empty files; both print nothing when they succeed.
  • cp copies and keeps the original; folders need cp -r.
  • mv moves or renames; the original name disappears.
  • cp, mv and > overwrite silently, so use -i and check names with ls.
  • rm has no Trash and no Undo. Use it only as rm -i on files in ~/practice.

Checklist: can you do these?

Marking a lesson as read does not prove you can do it. Skills count as demonstrated when you pass the quiz (not yet) and complete the practical tasks (not yet).

Related lessons

References and further reading

Official sources for details that can change between versions.

On this page