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 filemkdirmakes a directory. It fails if the parent doesn't exist yet, unless you add-p, which creates every missing level.-palso stays quiet if the directory already exists.touchcreates 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 itrmremoves files. Add-ito be asked about each one.rmdironly 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 -rfadds "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.
Write the target
Asha wants to remove every file ending in
-copy.jpginplots.
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.
seasons does not exist yet. Which command creates seasons/2026/summer in one go?
notes.txt already has text in it. What does touch notes.txt do?
rmdir summer fails with "Directory not empty". Which command removes the folder and its contents?
What is the safest habit before running rm plots/*.jpg?
Remember
- Creating is cheap, deleting is forever. There is no recycle bin.
mkdir -pbuilds a whole chain of folders;touchmakes empty files;rmdironly removes empty directories.- Preview every wildcard or
-rdelete withlsand the same arguments, and watch for stray spaces.
