๐Ÿ“ Lesson 3: Files and Folders: Making, Copying, Moving and Deleting

Create, copy, rename and delete things, and the habits that keep you from deleting the wrong ones

Video: Files and Folders (Linux 101 video)
Prefer to watch? This short video runs the lesson's main commands on a real Ubuntu 24.04 machine, with on-screen captions and no voiceover. Click to play, or watch it on YouTube.

A promise before we start

The command line has no recycle bin. When you delete something with rm, it is gone straight away, with no "are you sure?" and no undo. That sounds scary, and on your own computer it deserves respect. On your practice machine from lesson 1 it is a gift: you can learn the dangerous commands where nothing important lives. If you break something, type exit, start a fresh machine, and carry on.

Open your practice machine and follow along. Each example here was run in order, in one session, on a real Ubuntu 24.04, so if you type the same commands you will get the same results (apart from the dates).

Make folders with mkdir

mkdir means "make directory", and a directory is just a folder.

ubuntu@practice:~$ mkdir projects
ubuntu@practice:~$ ls
projects
ubuntu@practice:~$ cd projects
ubuntu@practice:~/projects$ pwd
/home/ubuntu/projects
ubuntu@practice:~/projects$ mkdir x/y/z
mkdir: cannot create directory 'x/y/z': No such file or directory
ubuntu@practice:~/projects$ mkdir -p a/b/c
ubuntu@practice:~/projects$ ls
a
ubuntu@practice:~/projects$ ls a
b

Make files with touch and echo

ubuntu@practice:~/projects$ touch notes.txt
ubuntu@practice:~/projects$ echo "Buy milk" > shopping.txt
ubuntu@practice:~/projects$ ls -l
total 8
drwxr-xr-x 3 ubuntu ubuntu 4096 Oct  5 13:59 a
-rw-r--r-- 1 ubuntu ubuntu    0 Oct  5 13:59 notes.txt
-rw-r--r-- 1 ubuntu ubuntu    9 Oct  5 13:59 shopping.txt
ubuntu@practice:~/projects$ cat shopping.txt
Buy milk

Copy with cp

ubuntu@practice:~/projects$ cp shopping.txt shopping-backup.txt
ubuntu@practice:~/projects$ ls
a  notes.txt  shopping-backup.txt  shopping.txt
ubuntu@practice:~/projects$ cp -r a a-copy
ubuntu@practice:~/projects$ ls
a  a-copy  notes.txt  shopping-backup.txt  shopping.txt
ubuntu@practice:~/projects$ cp shopping.txt
cp: missing destination file operand after 'shopping.txt'
Try 'cp --help' for more information.
ubuntu@practice:~/projects$ cp shopping.txt notes.txt
ubuntu@practice:~/projects$ cat notes.txt
Buy milk
ubuntu@practice:~/projects$ cp -i shopping.txt notes.txt
cp: overwrite 'notes.txt'? n

Move and rename with mv

ubuntu@practice:~/projects$ mv notes.txt ideas.txt
ubuntu@practice:~/projects$ ls
a  a-copy  ideas.txt  shopping-backup.txt  shopping.txt
ubuntu@practice:~/projects$ mv ideas.txt a
ubuntu@practice:~/projects$ ls a
b  ideas.txt
ubuntu@practice:~/projects$ mv a/ideas.txt .
ubuntu@practice:~/projects$ ls
a  a-copy  ideas.txt  shopping-backup.txt  shopping.txt

Linux uses one command for both moving and renaming, because they are the same thing: giving a file a new name and place.

mv overwrites existing files without asking, just like cp does. It has the same safety option: mv -i.

Delete with rm and rmdir

ubuntu@practice:~/projects$ rm shopping-backup.txt
ubuntu@practice:~/projects$ ls
a  a-copy  ideas.txt  shopping.txt
ubuntu@practice:~/projects$ rm a
rm: cannot remove 'a': Is a directory
ubuntu@practice:~/projects$ rmdir a
rmdir: failed to remove 'a': Directory not empty
ubuntu@practice:~/projects$ rm -r a-copy
ubuntu@practice:~/projects$ ls
a  ideas.txt  shopping.txt
ubuntu@practice:~/projects$ rm -i ideas.txt
rm: remove regular file 'ideas.txt'? y
ubuntu@practice:~/projects$ ls
a  shopping.txt

โš ๏ธ You will see rm -rf in tutorials and forum posts. The -r means "folders and everything inside", and the -f means "force: do not ask and do not complain". Together they delete without a single question. Never run rm -rf on a path you have not just looked at with ls, and never paste a command you do not understand into a terminal, from any website, including this one. On a practice machine you can experiment freely. On a real machine, slow down.

Wildcards: matching many files at once

A wildcard is a character that stands for other characters, so one command can handle many files. The shell expands it into the list of matching names before the command runs.

ubuntu@practice:~/projects$ touch report1.txt report2.txt report10.txt
ubuntu@practice:~/projects$ touch photo.jpg
ubuntu@practice:~/projects$ ls *.txt
report1.txt  report10.txt  report2.txt  shopping.txt
ubuntu@practice:~/projects$ ls report?.txt
report1.txt  report2.txt
ubuntu@practice:~/projects$ ls report*
report1.txt  report10.txt  report2.txt
ubuntu@practice:~/projects$ rm *.jpg
ubuntu@practice:~/projects$ ls
a  report1.txt  report10.txt  report2.txt  shopping.txt

The golden rule of wildcards: look before you delete. Run ls with your pattern first. If ls *.jpg lists exactly the files you mean, rm *.jpg will delete exactly those. If the list surprises you, you have just saved yourself a bad day.

Spaces in file names

Spaces separate things on the command line, so a file name with a space needs quotes:

ubuntu@practice:~/projects$ touch "my notes.txt"
ubuntu@practice:~/projects$ ls
 a  'my notes.txt'   report1.txt   report10.txt   report2.txt   shopping.txt
ubuntu@practice:~/projects$ rm my notes.txt
rm: cannot remove 'my': No such file or directory
rm: cannot remove 'notes.txt': No such file or directory
ubuntu@practice:~/projects$ rm "my notes.txt"
ubuntu@practice:~/projects$ ls
a  report1.txt  report10.txt  report2.txt  shopping.txt

rm my notes.txt was read as two names, my and notes.txt, and neither exists. With quotes, rm "my notes.txt" is one name, and it worked. Notice that ls showed the name wrapped in single quotes to warn you that it contains a space. The easiest habit: avoid spaces in your own file names. Use dashes or underscores, like my-notes.txt.

Habits that keep you safe

ubuntu@practice:~/projects$ ls report*.txt
report1.txt  report10.txt  report2.txt
ubuntu@practice:~/projects$ rm -i report*.txt
rm: remove regular empty file 'report1.txt'? y
rm: remove regular empty file 'report10.txt'? y
rm: remove regular empty file 'report2.txt'? y
ubuntu@practice:~/projects$ ls
a  shopping.txt
  1. Look first, then delete. Run ls report*.txt and read the list before rm report*.txt.
  2. **Use -i when a wildcard is involved.** Above, rm -i report*.txt asked about every file, and y agreed.
  3. Check where you are. pwd before a delete. The prompt shows it too.
  4. Let Tab finish the names. If Tab completes it, it exists, and you did not mistype it.
  5. Practise here. That is what the practice machine is for.
Why did the file go to therapy?<br>
Too many unresolved links.

๐Ÿงช Try it yourself

1. In your home folder, make a folder called garden with two folders inside it, seeds and tools, using one command. (Hint: mkdir -p accepts several names.) 2. Put a file called todo.txt in garden that says water plants. Look at it with cat. 3. Copy todo.txt to todo-old.txt, then rename the copy to archive.txt. 4. Make three files called a1.log, a2.log and a10.log. Use a wildcard so that ls shows only the first two. 5. Delete the whole garden folder, and confirm with ls that it is gone.

The cheat sheet

← Lesson 2 · ↑ All lessons · Lesson 4: Reading and Searching Text →