Files and Directories Without Regret
Four commands cover almost everything you'll do to files on a server:
mkdir dirname # make a directory
touch filename # create an empty file (or bump its timestamp)
cp src dst # copy
mv src dst # move or rename - same command, same idea
rm filename # delete
rm has no undo button, no trash can, and by default it never asks "are you
sure?" The flags -r (recursive - go into every subfolder too) and -f
(force - never ask for confirmation) combine into rm -rf, which deletes an
entire folder and everything inside it, instantly, with no warning. This is
one of the most common ways people accidentally delete something important on
Linux. It's not dangerous because it's complicated - it's dangerous because
it's fast and gives you no chance to change your mind.
Reading a directory listing properly
A colleague deployed to ~/deploy/ and left it a wreck:
cd ~/deploy
ls -la
total 16
drwxrwxr-x 3 laborant laborant 4096 Sep 22 03:43 .
drwxr-x--- 9 laborant laborant 4096 Sep 22 03:43 ..
-rw-rw-r-- 1 laborant laborant 25 Sep 22 03:43 app.conf
drwxrwxr-x 3 laborant laborant 4096 Sep 22 03:43 archive
-rw-rw-r-- 1 laborant laborant 0 Sep 22 03:43 old-2026-01-01.tmp
-rw-rw-r-- 1 laborant laborant 0 Sep 22 03:43 old-2026-01-02.tmp
-rw-rw-r-- 1 laborant laborant 0 Sep 22 03:43 old-2026-01-03.tmp
The first character of each line tells you what kind of entry it is: - for
a regular file, d for a directory. So app.conf is a real file, archive
is a directory, and the three old-*.tmp files are all real files too - each
one 0 bytes, meaning genuinely empty, left over from some earlier abandoned
attempt. One file matters (app.conf), one directory needs a look inside
(archive), and three files are pure junk.
Wildcards
The character * stands in for "any number of any characters." So
old-*.tmp matches every file whose name starts with old- and ends with
.tmp, no matter what's in between - it matches all three junk files above
in one shot. This lets you act on many files with one command, which is
powerful, but also means it's easy to delete more than you meant to. So check
first, delete second:
ls old-*.tmp # look at exactly which files the wildcard matches
rm old-*.tmp # only delete them once you've confirmed the list looks right
Links
A hard link, made with ln target linkname, is a second name pointing at
the exact same data on disk. If you delete the original name, the data is
still there, safe, under the other name. A symbolic link (or "symlink"),
made with ln -s target linkname, is different - it's a small separate file
that just contains a path, like a signpost pointing somewhere. If you delete
the thing it points at, the symlink is left pointing at nothing. Symlinks are
what you'll actually use day to day, most often to create a "current version"
name that always points at whichever file is the active one right now.
Clean it up
Remove the junk, remove the now-empty archive/empty-leftover directory, and
set up a current pointer to the real config:
rm old-*.tmp
rmdir archive/empty-leftover
ln -s app.conf current
ls -la
total 16
drwxrwxr-x 3 laborant laborant 4096 Sep 22 03:43 .
drwxr-x--- 9 laborant laborant 4096 Sep 22 03:43 ..
-rw-rw-r-- 1 laborant laborant 25 Sep 22 03:43 app.conf
drwxrwxr-x 2 laborant laborant 4096 Sep 22 03:43 archive
lrwxrwxrwx 1 laborant laborant 8 Sep 22 03:43 current -> app.conf
Two changes prove the cleanup worked: the three old-*.tmp lines are gone,
and there's a new line starting with l (for "link"), current -> app.conf,
showing the symlink and exactly what it points at. Confirm what it actually
resolves to:
readlink -f current
/home/laborant/deploy/app.conf
readlink -f follows the symlink and prints the real, absolute path it leads
to - proof current genuinely points at app.conf, not at nothing.
rm complains the directory isn't empty
rmdir only removes directories that are already empty - which is exactly why
it's safer than rm -rf for cleanup. If it refuses, look inside first instead
of reaching for the more destructive command.
- Previous lesson
- Where Things Live: The Filesystem Layout