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.
# 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_buildGNU 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.
touchcreates empty files or updates timestamps on existing ones.mkdir -pcreates nested directory trees in one call and does not error if the directory already exists.rmdironly removes empty directories, acting as a built-in safety check.rm -rrecursively removes directories and their contents; adding-fsuppresses 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
1. What happens when you run `touch existingfile.txt`?
2. Why is `mkdir -p` generally preferred over plain `mkdir` in scripts?
3. What built-in safety check does `rmdir` provide that `rm -r` does not?
4. By default, where do files deleted with `rm` go?
5. What is the risk of running `rm -rf $DIR/` in a script where $DIR might be unset or empty?
Was this page helpful?
You May Also Like
Navigating the Filesystem (cd, pwd, ls)
Practical mastery of moving around the Linux directory tree using cd, pwd, and ls, including absolute vs relative paths and useful ls flags for everyday work.
Copying, Moving, and Renaming
How cp and mv handle copying, moving, and renaming files and directories, including recursive copies, preserving attributes, and safe overwrite behavior.
File Permissions Explained (rwx)
Understand the Linux permission model — read, write, and execute bits for owner, group, and others — and how ls -l output maps to those permissions.
Common Shell Scripting Pitfalls
A field guide to the mistakes that silently break Bash scripts — unquoted variables, wrong test operators, subshell scoping, and error-handling gaps.
Related Reading
Related Study Notes in DevOps
Browse all study notesNginx Study Notes
DevOps · 30 topics
DevOpsAnsible Study Notes
DevOps · 30 topics
DevOpsAdvanced Kubernetes Study Notes
Kubernetes · 30 topics
DevOpsAdvanced Bash Scripting Study Notes
Bash · 30 topics
DevOpsApache Kafka Study Notes
Kafka · 30 topics
DevOpsDocker & Kubernetes Study Notes
YAML · 40 topics