Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Navigation Basics  ›  Lesson

Creating and Deleting Files and Directories

Creating & Deleting Files / Dirs 12 minmkdir and touch create folders and files cheaply, while rm, rmdir and rm -r delete permanently, so deletes should be previewed first.

Creating and Deleting Files and Directories

Asha wants a folder for each season and an empty log file in each one. Later she'll want to clear out a season she no longer needs. Making things on Linux is quick and harmless. Removing them is just as quick, and there is no recycle bin to rescue you.

The rule

Creating is cheap; deleting is forever — look before you rm.

Creating:

$ cd ~/garden
$ mkdir summer                   # make one directory
$ mkdir -p seasons/2026/summer   # make the whole chain, parents included
$ touch summer/log.txt           # make an empty file
  • mkdir makes a directory. It fails if the parent doesn't exist yet, unless you add -p, which creates every missing level. -p also stays quiet if the directory already exists.
  • touch creates an empty file if the name is new. If the file already exists, it leaves the contents alone and only updates its "last modified" time.

Deleting:

$ rm summer/log.txt      # delete a file
$ rmdir summer           # delete an EMPTY directory
$ rm -r seasons          # delete a directory and everything inside it
  • rm removes files. Add -i to be asked about each one.
  • rmdir only removes empty directories. That makes it a safe choice: it refuses if anything is still inside.
  • rm -r (recursive) removes a directory together with its whole contents. rm -rf adds "force": no questions and no complaints about missing files. It is the most dangerous everyday command on the system.

Names with spaces need quotes. mkdir summer plans makes two folders, summer and plans, while mkdir "summer plans" makes one. Many people simply avoid spaces in file names and use hyphens instead, as in summer-plans.

Deleted files do not go to a trash folder. Once rm finishes, you can only get them back from a backup.

The shortcut

Before any rm with a wildcard or -r, run ls with exactly the same arguments first. Whatever ls lists is what rm will delete.

Cleaning out old photo copiesStep 1 of 4
  1. Write the target

    Asha wants to remove every file ending in -copy.jpg in plots.

The trap is a stray space. rm -r plots /old (with a space) means two targets, plots and /old, not the single path plots/old. Previewing with ls shows the extra target before it is too late.

Drills

Answer fast — trust the rule.

quick drill

seasons does not exist yet. Which command creates seasons/2026/summer in one go?

quick drill

notes.txt already has text in it. What does touch notes.txt do?

quick drill

rmdir summer fails with "Directory not empty". Which command removes the folder and its contents?

quick drill

What is the safest habit before running rm plots/*.jpg?

Remember

  • Creating is cheap, deleting is forever. There is no recycle bin.
  • mkdir -p builds a whole chain of folders; touch makes empty files; rmdir only removes empty directories.
  • Preview every wildcard or -r delete with ls and the same arguments, and watch for stray spaces.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainA safe delete routine

Asha wants to delete every file ending in -copy.jpg inside plots, and later remove an old folder called seasons that still has files in it. Describe the commands she should use and the habit that keeps her safe.