100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Bash

Creating and Removing Files and Directories

How to create empty files and directory trees with touch and mkdir, and safely remove them with rm and rmdir, including the dangers of recursive deletion.

Navigating & Managing FilesBeginner8 min readJul 9, 2026
Analogies

Creating and Removing Files and Directories

Four commands cover the vast majority of everyday file and directory lifecycle management on Linux: touch creates empty files (or updates timestamps on existing ones), mkdir creates directories, rmdir removes empty directories, and rm removes files and, with the right flag, entire directory trees. Each has behavior and flags worth understanding precisely, because getting them wrong — especially with rm — can cause irreversible data loss.

🏏

Cricket analogy: Just as a cricket ground needs distinct roles — preparing an empty pitch (touch), marking out new practice nets (mkdir), clearing an unused net (rmdir), and demolishing an entire stand (rm) — Linux's four file commands cover the full lifecycle, and misusing the demolition tool is catastrophic.

Creating Files and Directories

touch filename creates an empty file if it doesn't exist, or updates its modification and access timestamps to the current time if it does — this is genuinely its primary historical purpose (bumping a file's mtime to force a rebuild, for instance). mkdir dirname creates a single directory, and fails with an error if any parent directory in the path doesn't already exist. The -p flag fixes this by creating all necessary parent directories along the way and silently succeeding if the target directory already exists, which makes it the safer default for scripts. mkdir -m 750 dirname creates a directory with specific permissions set at creation time rather than relying on the umask default.

🏏

Cricket analogy: touch updating a file's timestamp without changing content is like re-dating a scorecard to 'today' to force a re-review, while mkdir -p creating all parent folders silently is like a groundskeeper preparing an entire new stadium complex — outfield, pitch, and stands — in one command.

Removing Files and Directories

rmdir dirname removes a directory only if it is completely empty — a deliberate safety constraint. rm filename removes one or more files. To remove a directory and everything inside it, you need rm -r (recursive); combined with -f (force, suppressing confirmation prompts and ignoring nonexistent files) as rm -rf, this becomes one of the most powerful and most dangerous command combinations in all of Linux, capable of silently and irrecoverably deleting an entire directory tree with no undo and no trash bin by default.

🏏

Cricket analogy: rmdir only removing an empty net is a deliberate safety brake, while rm -rf on a stadium's entire archive is like bulldozing the whole ground — stands, pitch, and records — with no undo, the same irreversible danger as accidentally deleting decades of Test match history.

bash
# Creating
touch notes.txt                     # create an empty file, or bump its timestamp if it exists
touch -t 202601010900 report.txt     # set a specific timestamp
mkdir project                        # create one directory
mkdir -p project/src/utils/helpers   # create nested directories in one call, no error if they exist
mkdir -m 700 secrets                 # create with explicit permissions (owner rwx only)

# Removing
rmdir empty_folder                   # fails if empty_folder is not empty
rm notes.txt                         # remove a single file
rm -i important.txt                  # prompt before each removal
rm -r project/src/utils              # recursively remove a directory tree
rm -rf /tmp/build_cache              # force-remove recursively, no prompts, ignore missing paths

# Safer pattern: preview before destroying
ls project/old_build && rm -ri project/old_build

GNU coreutils' rm (the Linux default) has no built-in trash/recycle bin — deleted files are gone once the last filesystem reference and open file handle are released, though specialized data-recovery tools can sometimes reconstruct recently deleted data before it's overwritten. Tools like trash-cli (trash filename) exist precisely to add an undoable safety net for interactive use, moving files to a ~/.local/share/Trash staging area instead of unlinking them immediately.

rm -rf / used to be catastrophic even as root; modern GNU rm requires the extra flag --no-preserve-root to actually operate on / itself, as a built-in guard rail. However, rm -rf on any subdirectory, or on a variable that expands to something unexpected (e.g. rm -rf $DIR/ when $DIR is accidentally empty, resulting in rm -rf /), remains fully destructive with no confirmation. Always double-check expanded variables in destructive commands, and consider rm -i for anything irreplaceable.

  • touch creates empty files or updates timestamps on existing ones.
  • mkdir -p creates nested directory trees in one call and does not error if the directory already exists.
  • rmdir only removes empty directories, acting as a built-in safety check.
  • rm -r recursively removes directories and their contents; adding -f suppresses prompts and skips missing paths.
  • GNU rm has no default trash bin — deletions are immediate and effectively permanent.
  • Unquoted or empty shell variables inside rm -rf $DIR/ can accidentally expand to dangerous, overly broad paths.

Practice what you learned

Was this page helpful?

Topics covered

#Bash#LinuxShellScriptingStudyNotes#DevOps#CreatingAndRemovingFilesAndDirectories#Creating#Removing#Files#Directories#StudyNotes#SkillVeris