Learn › Linux Foundations › Manipulation

Chapter 6, Part 1. Manipulation: touch, mkdir, rmdir, rm, cp and mv

Part 1 of 2: touch, mkdir, mkdir -p, rmdir, rm, rm -r, cp, cp -r and mv (Sections 6.1 to 6.9). No machine test here; it completes on the final step. The chapter lab waits at the end of Part 2.

Chapter 6, Part 1. Manipulation

This chapter covers the following topics:

This is Part 1 of 2. It covers Sections 6.1 to 6.9, the tools that create, copy, move and delete files and directories. Part 2 covers Sections 6.10 to 6.15, links and the tools that take a path apart or resolve it, and it ends in the chapter lab, which covers all fifteen sections.

What you already know: Chapter 2 taught ls, cd, absolute and relative paths and tree, and Chapter 5 read files with cat and the tools beside it without changing a byte. This part is the first that changes the filesystem: it creates, copies, moves and deletes, and it teaches the message each tool prints when it refuses.

Objectives

After this part you can:

Before you start a machine, each exercise shows a rehearsal terminal. It checks the requested command against an example and does not change Linux files. Start one real practice machine and use its admin@server1 shell across these steps. Commands there create, move or delete real files; a wrong command can still change them even if the lesson does not mark the exercise complete. The restock helper rebuilds a named exercise bay when you need to try again.

This part finishes at its final step. The separate final Test Room comes at the end of Part 2, uses a Debian 12 server prepared for this chapter, and checks its objectives automatically as you work.

Pre-check 1 of 5: an empty file

Five questions before the material. A wrong answer costs nothing: the explanation states the fact and names the section that teaches it.

Pre-check 2 of 5: where a deleted file goes

Pre-check 3 of 5: missing parents

Pre-check 4 of 5: copy and move

Pre-check 5 of 5: a refusal

6.1 Creating an empty file and updating a time: touch

Every chapter so far read the filesystem. This one changes it. A filesystem operation is an action that creates, copies, moves or removes a name in a directory. The standard file tools in this part usually print nothing when they succeed and a reason when they refuse. The course's restock helper is different: it reports the bay it rebuilt.

This chapter adds one directory to the server, staging. It holds 25 small directories named bay-01 to bay-25, plus scratch. This chapter calls each of them a bay. A bay holds only the files one exercise needs, so what you create or delete in one never touches another, and the chapter lab uses the same layout. You are in your home directory. List staging with ls from Chapter 2:

prompt: admin@server1:~$ answer: ls staging ||| ls staging/ ||| ls ~/staging ||| ls ~/staging/ output: README.txt bay-03 bay-06 bay-09 bay-12 bay-15 bay-18 bay-21 bay-24 bay-01 bay-04 bay-07 bay-10 bay-13 bay-16 bay-19 bay-22 bay-25 bay-02 bay-05 bay-08 bay-11 bay-14 bay-17 bay-20 bay-23 scratch hint: Section 2.2. The listing command from Chapter 2, then the directory staging (or ~/staging from anywhere).

Twenty-five numbered directories, a README and scratch. Your terminal may show the same names across a different number of columns; ls fits them to the width of the window. The names differ only in the number. bay-01 belongs to the first exercise, bay-25 to the last objective of the lab.

6.1 The staging README

You are in your home directory. Print staging/README.txt, which states what the directories are for and how to rebuild one:

prompt: admin@server1:~$ answer: cat staging/README.txt ||| cat ~/staging/README.txt output: staging: working directories for Chapter 6 (Manipulation) ========================================================= The directories bay-01 to bay-25 each hold the files one objective of the chapter lab needs. What you create, copy, move or delete in one bay never affects another. scratch is a spare directory for experiments.

To rebuild a directory to the state it had when the machine booted:

restock bay-07 rebuild one directory restock scratch rebuild the spare directory restock all rebuild every directory under staging

restock bay-NN rebuilds only that named directory. A state-graded objective may reopen; a command already recorded in history stays recorded. hint: Section 5.1. The printing command from Chapter 5, then the path staging/README.txt (or ~/staging/README.txt from anywhere).

Three facts. Each bay holds what one objective needs. The intended exercises stay inside their named bays. restock, a helper installed on this server rather than a Linux command, can rebuild an owned bay to its boot state. Rebuilding a state-graded bay can reopen its objective; a command-history check remains recorded. Inspect the target before using it. Section 6.5 runs it.

The README calls the bays working directories, meaning directories to work in. Chapter 2 used those two words for the directory you are standing in, the one pwd prints, and that meaning is unchanged.

6.1 An empty directory

The first bay, staging/bay-01, is empty. You are in your home directory. A long listing of an empty directory shows only its total line:

prompt: admin@server1:~$ answer: ls -l staging/bay-01 ||| ls -l staging/bay-01/ ||| ls -l ~/staging/bay-01 output: total 0 hint: Section 2.4. The listing command with its long option -l, then the path staging/bay-01 (or ~/staging/bay-01 from anywhere).

total 0 and nothing under it: no entries, no blocks in use. The next exercise puts a file here.

6.1 touch creates the file

touch takes a path. If no file exists there, touch creates an empty one: a name in the directory with zero bytes behind it. If a file does exist, touch changes nothing about its contents and updates its modification time, the timestamp ls -l shows, to now. Both uses are common: an empty file as a marker or a placeholder to fill later, and a fresh timestamp to make a file look newly changed.

You are in your home directory. Create an empty file at staging/bay-01/notes.txt:

prompt: admin@server1:~$ answer: touch staging/bay-01/notes.txt ||| touch ~/staging/bay-01/notes.txt output: hint: Section 6.1. The command that creates an empty file, then the path staging/bay-01/notes.txt (or ~/staging/bay-01/notes.txt from anywhere).

Nothing printed, and the prompt is back. touch is normally silent on success, so check the listing to see what happened. The course's restock helper later reports the bay it rebuilt.

6.1 The new file

You are in your home directory. List staging/bay-01 in long form again:

prompt: admin@server1:~$ answer: ls -l staging/bay-01 ||| ls -l staging/bay-01/ ||| ls -l ~/staging/bay-01 output: total 0 -rw-r--r-- 1 admin admin 0 Sep 6 14:44 notes.txt hint: Section 2.4. The same long listing as Exercise 6-3: the option -l, then staging/bay-01 (or ~/staging/bay-01 from anywhere).

One entry. The size column reads 0: the file exists and holds nothing. The date and time are the moment you ran touch, so yours differ from the ones shown here. The total is still 0 because an empty file occupies no disk block; Section 5.10 explained blocks.

6.1 A file that exists

You are in your home directory. The second bay, staging/bay-02, holds a web page written when the machine was built. Its timestamp is the fixed date every fixture on server1 carries. List it in long form:

prompt: admin@server1:~$ answer: ls -l staging/bay-02 ||| ls -l staging/bay-02/ ||| ls -l ~/staging/bay-02 output: total 4 -rw-r--r-- 1 admin admin 225 Jan 15 2026 index.html hint: Section 2.4. The listing command with -l, then the path staging/bay-02 (or ~/staging/bay-02 from anywhere).

One file of 225 bytes dated January 15, 2026. When a file is older than six months, ls prints the year instead of the time of day, which is why this line shows 2026 where the last one showed a clock time.

6.1 touch updates the time

You are in your home directory. Run touch on the file that exists, staging/bay-02/index.html:

prompt: admin@server1:~$ answer: touch staging/bay-02/index.html ||| touch ~/staging/bay-02/index.html output: hint: Section 6.1. The same command as Exercise 6-4, then the path staging/bay-02/index.html (or ~/staging/bay-02/index.html from anywhere).

Silent again. The file existed, so touch created nothing and changed no byte of it. What it changed is in the listing.

6.1 The moved time

You are in your home directory. List staging/bay-02 in long form again:

prompt: admin@server1:~$ answer: ls -l staging/bay-02 ||| ls -l staging/bay-02/ ||| ls -l ~/staging/bay-02 output: total 4 -rw-r--r-- 1 admin admin 225 Sep 6 14:44 index.html hint: Section 2.4. The long listing of staging/bay-02 again (or ~/staging/bay-02 from anywhere).

Same name, same 225 bytes, and a new timestamp: the moment you ran touch, so yours differs from the one shown. The contents did not change; only the modification time did. Build tools and backup scripts read that time to decide what is new, which is the reason touch exists.

6.2 Creating a directory: mkdir

mkdir, make directory, creates one empty directory at the path you give. The directory that will contain it must already exist. You are in your home directory. The third bay, staging/bay-03, holds a plan; add staging/bay-03/reports beside it:

prompt: admin@server1:~$ answer: mkdir staging/bay-03/reports ||| mkdir ~/staging/bay-03/reports ||| mkdir staging/bay-03/reports/ output: hint: Section 6.2. The command that makes a directory, then the path of the new directory, staging/bay-03/reports (or ~/staging/bay-03/reports from anywhere).

Silent: the directory exists. Run the same command a second time and mkdir refuses, because the name is taken; the step Errors you will meet shows the message.

6.2 The new directory

You are in your home directory. List staging/bay-03:

prompt: admin@server1:~$ answer: ls staging/bay-03 ||| ls staging/bay-03/ ||| ls ~/staging/bay-03 output: plan.txt reports hint: Section 2.2. The plain listing command, then staging/bay-03 (or ~/staging/bay-03 from anywhere).

The fixture file and the new directory, side by side. A plain listing does not say which is which; ls -F from Section 2.4 marks a directory with a slash, and tree draws it as a branch.

6.3 Creating a chain of directories: mkdir -p

A path of several new directories fails with plain mkdir, because each new directory needs its parent to exist first. The fourth bay is empty, and mkdir staging/bay-04/assets/css/fonts stops at the first missing parent:

mkdir: cannot create directory ‘staging/bay-04/assets/css/fonts’: No such file or directory

The -p option, parents, creates every missing directory in the path in order, and does not complain about any that already exist. You are in your home directory. Create the whole chain staging/bay-04/assets/css/fonts in one command:

prompt: admin@server1:~$ answer: mkdir -p staging/bay-04/assets/css/fonts ||| mkdir -p ~/staging/bay-04/assets/css/fonts ||| mkdir --parents staging/bay-04/assets/css/fonts ||| mkdir -p staging/bay-04/assets/css/fonts/ output: hint: Section 6.3. The directory command with its parents option -p, then the whole path staging/bay-04/assets/css/fonts (or ~/staging/bay-04/assets/css/fonts from anywhere).

Silent. Three directories exist now: assets, css inside it, fonts inside that. mkdir -p is also the safe form in a script, because it succeeds whether or not the directories were already there.

6.3 The chain

You are in your home directory. Draw staging/bay-04 with tree from Section 2.7:

prompt: admin@server1:~$ answer: tree staging/bay-04 ||| tree staging/bay-04/ ||| tree ~/staging/bay-04 output: staging/bay-04 └── assets └── css └── fonts

4 directories, 0 files hint: Section 2.7. The tree drawing command, then the path staging/bay-04 (or ~/staging/bay-04 from anywhere).

One branch three levels deep, each level a directory the one command created, and no files. This is the shape a website keeps its assets in, and one mkdir -p builds it.

6.4 Removing an empty directory: rmdir

rmdir, remove directory, deletes a directory that is empty and refuses one that holds anything. The refusal is a safety: rmdir can never take a file with it. You are in your home directory. The fifth bay, staging/bay-05, holds two directories, one with a draft in it and one empty. List it:

prompt: admin@server1:~$ answer: ls staging/bay-05 ||| ls staging/bay-05/ ||| ls ~/staging/bay-05 output: drafts old-drafts hint: Section 2.2. The plain listing command, then staging/bay-05 (or ~/staging/bay-05 from anywhere).

Two names. drafts holds one file; old-drafts holds nothing. Only one of them can go with rmdir.

6.4 rmdir removes the empty one

You are in your home directory. Remove the empty one, staging/bay-05/old-drafts:

prompt: admin@server1:~$ answer: rmdir staging/bay-05/old-drafts ||| rmdir ~/staging/bay-05/old-drafts ||| rmdir staging/bay-05/old-drafts/ output: hint: Section 6.4. The command that removes an empty directory, then the path staging/bay-05/old-drafts (or ~/staging/bay-05/old-drafts from anywhere). Not drafts, which holds a file.

Silent: the empty directory is gone. Aim rmdir at drafts instead and it prints Directory not empty and changes nothing; the step Errors you will meet quotes the line.

6.4 What remains

You are in your home directory. List staging/bay-05 again:

prompt: admin@server1:~$ answer: ls staging/bay-05 ||| ls staging/bay-05/ ||| ls ~/staging/bay-05 output: drafts hint: Section 2.2. The plain listing of staging/bay-05 again (or ~/staging/bay-05 from anywhere).

Only drafts remains, with its file untouched. rmdir is the tool to reach for first when a directory should be empty: if it refuses, something is inside that you did not know about.

6.5 Removing a file: rm

rm, remove, deletes the file at the path you give. There is no trash and no undo: when rm returns, the name is gone. The space the file used is free again once no name is left for it, which Section 6.10 explains. The habit that protects you is to list first and remove second. You are in your home directory. The sixth bay, staging/bay-06, holds two logs, one current and one stale. List it:

prompt: admin@server1:~$ answer: ls staging/bay-06 ||| ls staging/bay-06/ ||| ls ~/staging/bay-06 output: current.log stale.log hint: Section 2.2. The plain listing command, then staging/bay-06 (or ~/staging/bay-06 from anywhere).

Two files. The stale one is the target; the current one must stay. rm takes exactly the paths you name and nothing beside them.

6.5 rm removes the file

You are in your home directory. Remove the stale log, staging/bay-06/stale.log. The current one stays:

prompt: admin@server1:~$ answer: rm staging/bay-06/stale.log ||| rm ~/staging/bay-06/stale.log output: hint: Section 6.5. The removal command, then the path staging/bay-06/stale.log (or ~/staging/bay-06/stale.log from anywhere). Leave current.log alone.

Silent and permanent. On this server, you have permission to remove the name from its parent directory, and no interactive alias asks first. A file's own write bit is not what grants permission to remove its name. Some systems add an alias that makes rm behave as rm -i, which asks before each file; server1 has no such alias.

6.5 Gone

You are in your home directory. List staging/bay-06 again:

prompt: admin@server1:~$ answer: ls staging/bay-06 ||| ls staging/bay-06/ ||| ls ~/staging/bay-06 output: current.log hint: Section 2.2. The plain listing of staging/bay-06 again (or ~/staging/bay-06 from anywhere).

One file left. Run the rm again and it refuses, because there is nothing at t

Practice Chapter 6, Part 1. Manipulation: touch, mkdir, rmdir, rm, cp and mv in a real Linux terminal at The Linux Camp. Progress is verified automatically as you type commands on the machine.

More lessons in Manipulation