Creating, copying, moving and removing

Beginner · 9 min

Use mkdir, touch, cp, mv, rm and rmdir safely. See which of them silently overwrite, why rm has no undo, and how a trailing slash changes cp -R on macOS.

Before thisListing and inspecting files

Microsoft Developer · 9 min

Creating, copying, moving and removing

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Microsoft DeveloperReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • mkdir creates directories (-p makes the missing parents and tolerates existing ones). touch creates an empty file or updates a file's modification time.
  • cp and mv overwrite an existing target without asking. Add -n to refuse or -i to be asked.
  • mv is both rename and move. Within one filesystem it only changes a name, so it is fast even for huge files.
  • rm deletes immediately with no trash and no undo. Directories need -r, and rmdir removes only empty ones.
  • cp -R src/ dest on macOS copies the contents of src into dest, while cp -R src dest makes dest/src. GNU cp behaves differently, so check your platform.

Six small commands

Everything you do to files from a shell is built from six commands: mkdir and touch create, cp copies, mv moves or renames, and rm and rmdir delete. They are simple. The danger is that none of them double-checks you, and most overwrite or delete without a word. I ran each one in a scratch directory so you can see what actually happens.

Making things

mkdir creates a directory. It fails if the directory exists, or if a parent is missing:

$ mkdir notes; mkdir notes
mkdir: notes: File exists
$ mkdir a/b/c
mkdir: a/b: No such file or directory
$ mkdir -p a/b/c && ls -R a
b

a/b:
c

a/b/c:

-p makes the whole chain and is quiet if everything already exists, which makes it safe in scripts (mkdir -p x/y/z a second time returned status 0). -v prints each directory created.

touch creates an empty file if the name is new:

$ touch todo.txt; ls -l todo.txt | awk '{print $5, $9}'
0 todo.txt

Zero bytes. If the file exists, touch leaves its contents alone and only updates its modification time, which is why it is also used to nudge a build tool.

Copying

cp source target copies a file. If target is a directory, the copy goes inside it:

$ cp todo.txt copy.txt
$ cp todo.txt notes; ls notes
todo.txt

Now the first hazard. I put new text in todo.txt and copied it over an existing copy.txt:

$ printf 'NEW\n' > todo.txt; cp todo.txt copy.txt; cat copy.txt
NEW

No prompt, no warning. The old content is gone. Two flags add a seatbelt: -n refuses to overwrite, and -i asks first.

$ printf 'other\n' > todo.txt; cp -n todo.txt copy.txt; cat copy.txt
NEW
$ echo y | cp -i todo.txt copy.txt; cat copy.txt
overwrite copy.txt? (y/n [n]) other

With -n nothing happened and the old text stayed. With -i it asked and I answered y.

Copying a directory needs -R (or -r), otherwise:

$ cp notes notes2
cp: notes is a directory (not copied).

By default cp also gives the copy a new modification time. -p keeps the original times. I set a file to 1 January 2020: the plain copy got today’s time and cp -p kept Jan 1 2020.

The trailing slash trap

Here the BSD cp on macOS and the GNU cp on Linux can disagree, and it silently changes where your files land. On macOS:

$ cp -R src2/ dest2; ls dest2
one
two
$ cp -R src2 dest3; ls dest3
src2

A trailing slash on the source copied its contents into dest2. Without it, a directory src2 was created inside dest3. I only ran the BSD version. I believe GNU cp -r src2/ dest2 onto an existing dest2 makes dest2/src2, but I haven’t seen that here, so don’t rely on my word. Test with a throwaway directory before a copy you can’t redo.

Moving and renaming

mv has one job and two faces. Given a file and a new name it renames. Given a file and a directory it moves:

$ mv copy.txt renamed.txt
$ mv renamed.txt notes/
$ ls notes
renamed.txt
todo.txt

On the same disk this only rewrites a directory entry, so moving a 50 GB file takes no longer than moving a note. Across disks it has to copy and delete. Like cp, mv overwrites without asking:

$ printf 'A\n' > fa; printf 'B\n' > fb; mv fa fb; cat fb
A

B is gone and fa is gone. mv -n declines to overwrite (fb stayed A and fa stayed put), and mv -i asks.

Removing

$ rm c1.txt; ls c1.txt
ls: c1.txt: No such file or directory

There is no trash. The shell asks the kernel to unlink the name, and the data blocks are freed. Directories are protected by a second rule:

$ rm notes3
rm: notes3: is a directory
$ rmdir notes3
rmdir: notes3: Directory not empty
$ rm -r notes

rmdir removes only empty directories, which makes it a safe way to tidy. rm -r deletes a directory and everything beneath it. rm -i asks (I answered n and the file stayed), -v lists what it deletes, and -f silences complaints about missing files and prompts, returning status 0 even for a name that doesn’t exist.

One more caution, caught in testing: I ran rm a b after creating a file called a b. It tried to remove a, a directory I had made earlier, and b, which didn’t exist. An unquoted space deletes two names. Quote names that contain spaces, as the anatomy lesson showed.

About the video

Microsoft Developer’s “How to Work with Directories in the Terminal with Bash [8 of 20]” was published 15 February 2023 and runs 9 minutes 5 seconds. Its description gives chapters for mkdir (0:00), touch (2:38), mv (3:33), cp (4:30), rm (5:55) and rmdir (7:10), which is the same set of commands as this lesson. It is in English per its title and description. I couldn’t read a transcript, so I haven’t checked whether it warns about overwriting, shows -p, or mentions -r. The trailing-slash behaviour and the BSD/GNU split are not in its description and come from my own runs.

What I did not run

GNU cp, GNU mv, and rm on Linux. cp -a, cp -l and cp -c (the macOS clone flag), rm -d, and mv across filesystems. I didn’t test deleting anything outside a scratch directory.

Next

Those commands touch names and contents. The next lesson covers who is allowed to do any of this: permissions and chmod.

Check yourself

  1. 01mkdir a/b/c fails with 'No such file or directory'. What fixes it?

    mkdir -p a/b/c. The -p flag creates any missing parent directories and does not complain if the directory already exists.

  2. 02You run cp new.txt old.txt and old.txt already exists. What happens?

    It is overwritten with no warning. Use cp -n to keep the old file or cp -i to be prompted first.

  3. 03What is the difference between rm notes and rmdir notes for a non-empty directory?

    rm refuses because it is a directory, and rmdir refuses because it is not empty. To remove a directory with contents you need rm -r.

  4. 04You moved a file with mv and now want it back at the old name. Is there a trash?

    No trash for mv or rm. For a move, run mv again with the arguments reversed. For a delete there is nothing to restore.

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark