← All lessons
Command line basics · Lesson 3 of 7

Making and moving files

mkdir makes a folder. mkdir -p makes a whole path in one go and doesn't complain if it's already there: mkdir -p projects/duck/notes. touch makes an empty file (or updates the date on one that exists).

cp copies and mv moves. Both take the source first and the destination second. If the destination is a folder, the file goes inside it with the same name; otherwise it gets the new name. That's also how you rename: mv old.txt new.txt.

$ mkdir backup
$ cp notes.txt backup/
$ mv notes.txt ideas.txt
$ ls
backup  ideas.txt
$ ls backup
notes.txt

To copy a folder and everything in it, add -r (recursive): cp -r photos photos-old. mv moves folders without it.

rm deletes files. There's no bin and no undo: the file is gone. rm -r deletes a folder and everything inside, and rmdir deletes a folder only if it's empty, which makes it the safer choice. Add -i to have rm ask before each file.

Be careful with names that have spaces. rm my notes.txt tries to delete two files, my and notes.txt. Put the name in quotes: rm "my notes.txt".

Practice

Make a folder called photos.

More info

Quick reference

mkdir name, mkdir -p a/bMake a folder; make a whole path.
touch fileMake an empty file.
cp from to, cp -rCopy a file; copy a folder.
mv from toMove or rename.
rm file, rm -r folderDelete for good. No undo.
rmdir folderDelete an empty folder.
"a name"Quotes keep a name with spaces together.

The other Linux courses