Working with Files and Directories
Create, copy, move, delete and inspect files safely in Bash: mkdir -p, cp -a, mv, rm with guards, tail -F, file and stat, hard and symbolic links, and du.
By The Dark DebuggerUpdated 32 min read
This article covers the commands you'll use every day to create, copy, move, delete, view and
inspect files, along with the mistakes that make some of them dangerous. It assumes you can move
around the filesystem and read ls -l output, both covered in
Navigating the Filesystem. Next in the track is
Wildcards, Globbing, and Brace Expansion, which makes all
of these commands work on many files at once.
Why this matters
We'll keep working on the web shop project from the previous article. Its logs directory has
grown to 44 files, with a daily application log for every day since the start of October:
cd ~/projects/webshop
ls
ls logs | head -5
ls logs | wc -l
du -sh logsREADME.md config deploy.sh docs logs orders.csv src
access.log
access.log.1
access.log.2.gz
app-2026-10-01.log
app-2026-10-02.log
44
936K logsBy the end of this article we'll have archived and compressed October's logs, backed up the
configuration before editing it, and switched a deployment between releases. We'll also look at
how each of these jobs goes wrong: a copy that nests itself one level too deep, a symlink that
points nowhere, and the classic rm -rf "$DIR"/* with an empty variable. That last one has
deleted more production data than any other shell mistake, and it takes one line to guard against.
Creating files: touch
touch creates an empty file if the name doesn't exist:
touch CHANGELOG.md
ls -l CHANGELOG.md-rw-r--r-- 1 sid sid 0 Nov 9 21:57 CHANGELOG.mdThat's actually a side effect. touch exists to update a file's timestamps. On an existing file it
changes nothing but the modification time:
stat -c '%y %n' config/app.conf
touch config/app.conf
stat -c '%y %n' config/app.conf2026-11-04 14:20:00.000000000 -0500 config/app.conf
2026-11-09 21:57:41.649055935 -0500 config/app.confBuild tools such as make decide what to rebuild from these times, so touch is the standard way
to force a rebuild. -d sets any time you like, which is handy for testing a cleanup script that
deletes files older than 30 days without waiting 30 days:
touch -d '30 days ago' old-test.log
ls -l old-test.log-rw-r--r-- 1 sid sid 0 Oct 10 22:57 old-test.logTo create a file with content, redirect output into it (echo "text" > file) or use an editor.
Redirection gets its own article, Redirection and Pipes.
Creating directories: mkdir and mkdir -p
mkdir creates one directory, and its parent must already exist:
mkdir logs/archive/2026-10mkdir: cannot create directory ‘logs/archive/2026-10’: No such file or directoryThe message is misleading. logs exists. What's missing is the intermediate archive. -p
(parents) creates every missing directory along the path, and it doesn't complain if the
directory already exists, which makes it safe to run repeatedly in scripts:
mkdir -p logs/archive/2026-10
echo "exit=$?"
mkdir -pv backups/config/2026-11exit=0
mkdir: created directory 'backups'
mkdir: created directory 'backups/config'
mkdir: created directory 'backups/config/2026-11'Without -p, an existing directory is an error:
mkdir configmkdir: cannot create directory ‘config’: File existsCombined with brace expansion (covered properly in the globbing article), one command lays out a whole project skeleton:
mkdir -p ~/projects/inventory/{src,tests,config,logs/archive}
tree ~/projects/inventory/home/sid/projects/inventory
├── config
├── logs
│ └── archive
├── src
└── tests
6 directories, 0 filesCopying: cp
Files
cp source destination copies one file. If the destination is a directory, the copy goes inside
it with the same name. Give several sources and the last argument must be a directory:
cp config/app.conf config/app.conf.bak
ls config
cp config/app.conf config/nginx.conf backups/config/
ls backups/configapp.conf app.conf.bak nginx.conf
2026-11 app.conf nginx.confForget the destination and cp treats your last file as the target:
cp config/app.conf config/nginx.conf README.mdcp: target 'README.md': Not a directoryThat one failed safely. With only two arguments, though, cp a b overwrites b without asking.
More on that below.
Directories, and the second-copy trap
cp skips directories unless you ask for a recursive copy with -r:
cp config config-backup
cp -r config config-backup
ls config-backupcp: -r not specified; omitting directory 'config'
app.conf app.conf.bak nginx.confNow run the same command again, as you might when re-running a backup step:
cp -r config config-backup
tree config-backupconfig-backup
├── app.conf
├── app.conf.bak
├── config
│ ├── app.conf
│ ├── app.conf.bak
│ └── nginx.conf
└── nginx.conf
2 directories, 6 filesThe first time, config-backup didn't exist, so cp created it as a copy of config. The second
time it existed, so cp put a copy of config inside it. The same command does two different
things depending on whether the destination exists. Scripts that run twice end up with
backup/config/config/config. To copy a directory's contents into a directory that may already
exist, use the dir/. form:
rm -r config-backup
mkdir config-backup
cp -r config/. config-backup/
ls -A config-backup.env app.conf app.conf.bak nginx.confconfig/. means "everything in config", including hidden files like .env. A glob such as
config/* would have skipped it.
Not overwriting things
By default cp silently replaces an existing destination file. Three ways to stop that:
cp -i config/nginx.conf backups/config/nginx.confcp: overwrite 'backups/config/nginx.conf'? n-i (interactive) asks first. Answering anything other than y skips the file. -n (no-clobber)
skips existing files without asking, but coreutils 9.4 warns that its behaviour is changing:
cp -n config/app.conf backups/config/app.conf
echo "exit=$?"cp: warning: behavior of -n is non-portable and may change in future; use --update=none instead
exit=0Use --update=none in new scripts, as the warning suggests. It does the same thing quietly.
-u (update) copies only when the source is newer than the destination. That's a cheap way to
refresh a backup. Combined with -v (verbose), you see exactly what was copied. Nothing, the first
time, because the backups are newer. After touching nginx.conf, just that file:
cp -uv config/*.conf backups/config/
touch config/nginx.conf
cp -uv config/*.conf backups/config/'config/nginx.conf' -> 'backups/config/nginx.conf'Preserving timestamps, permissions and links: -a
A plain cp creates a new file: it gets the current time, and you become its owner. That's
wrong for backups, where you want to know when the original last changed. -a (archive) preserves
timestamps, permissions, ownership (where allowed) and symbolic links, and implies -r:
cp docs/runbook.md runbook-plain.md
cp -a docs/runbook.md runbook-archive.md
ls -l --time-style=long-iso docs/runbook.md runbook-*.md-rw-r--r-- 1 sid sid 46 2026-03-14 11:02 docs/runbook.md
-rw-r--r-- 1 sid sid 46 2026-03-14 11:02 runbook-archive.md
-rw-r--r-- 1 sid sid 46 2026-11-09 21:57 runbook-plain.mdThe plain copy claims the runbook was last edited today, which is false. I use cp -a by default
for anything that's a backup or a copy of a directory tree. For example, preparing a new release
directory from the current one keeps the config and logs symlinks as symlinks:
cp -a ~/srv/webshop/releases/v1.4.2 ~/srv/webshop/releases/v1.4.3
ls -l ~/srv/webshop/releases/v1.4.3total 12
-rw-r--r-- 1 sid sid 6 Nov 6 18:07 VERSION
drwxr-xr-x 2 sid sid 4096 Nov 6 18:07 api
-rw-r--r-- 1 sid sid 150 Nov 6 18:07 app.py
lrwxrwxrwx 1 sid sid 19 Nov 6 18:07 config -> ../../shared/config
lrwxrwxrwx 1 sid sid 17 Nov 6 18:07 logs -> ../../shared/logs| Option | Effect |
|---|---|
-r | Copy directories recursively |
-a | Archive: recursive, and preserve timestamps, permissions, ownership and links |
-i | Ask before overwriting |
--update=none (-n) | Never overwrite existing files |
-u | Copy only if the source is newer than the destination |
-v | Print each file as it's copied |
-p | Preserve mode, ownership and timestamps (without recursion) |
Moving and renaming: mv
Linux has no separate rename command. Renaming is moving a file to a new name. Within one
filesystem, mv doesn't copy any data. It just changes the directory entry, which is why moving
a 50 GB file within a disk is instant. The inode number (the file's identity on disk, shown
by ls -i) stays the same:
ls -i logs/error.log
mv logs/error.log logs/error-2026-11-09.log
ls -i logs/error-2026-11-09.log57591 logs/error.log
57591 logs/error-2026-11-09.logMoving across filesystems (for example from /home to a USB drive) is a copy followed by a
delete, and takes as long as copying.
Moving into a directory works like cp. -v reports what happened:
mv -v CHANGELOG.md docs/renamed 'CHANGELOG.md' -> 'docs/CHANGELOG.md'Like cp, mv replaces an existing destination without asking. That's worse than with cp,
because the source is gone afterwards too. Here a staging config replaces the backup we made
earlier, and the backup's old contents are simply gone:
printf '[server]\nport = 9090\n' > staging.conf
mv staging.conf config/app.conf.bak
cat config/app.conf.bak[server]
port = 9090-i asks first, and -n refuses to overwrite. In coreutils 9.4, mv -n also reports the skip
and exits with status 1, so a script can tell the move didn't happen:
mv -i config/app.conf.bak config/app.conf
mv -n config/app.conf.bak config/app.conf
echo "exit=$?"mv: overwrite 'config/app.conf'? n
mv: not replacing 'config/app.conf'
exit=1Directories are moved and renamed the same way, without -r:
mv config-backup config-old
lsREADME.md backups config config-old deploy.sh docs logs orders.csv srcDeleting: rm and rmdir
rm deletes files. There's no recycle bin and no undo: once the last name for a file is removed,
the space is reused. Treat every rm as permanent.
rm config/app.conf.bak
rm nosuch.txt
rm -f nosuch.txtrm: cannot remove 'nosuch.txt': No such file or directoryThe first rm was silent: success. -f (force) suppresses "no such file" errors and never
prompts, which is why it appears in cleanup scripts. Directories need different handling:
rm docs
rmdir docsrm: cannot remove 'docs': Is a directory
rmdir: failed to remove 'docs': Directory not emptyrmdir removes only empty directories. That makes it a safe choice when you expect a directory
to be empty, because it refuses to delete anything else. To remove a directory and everything in
it, use rm -r. -v lists each deletion:
rm -rv config-oldremoved 'config-old/app.conf'
removed 'config-old/nginx.conf'
removed 'config-old/.env'
removed 'config-old/app.conf.bak'
removed directory 'config-old'Two prompting options are worth knowing. -i asks about every file. -I asks once when you're
about to delete more than three files or recurse, which is a sensible safety net that doesn't get
in the way:
rm -i logs/error-2026-11-09.log
rm -I logs/app-2026-10-0*.logrm: remove regular file 'logs/error-2026-11-09.log'? n
rm: remove 9 arguments? nThe second prompt is especially useful with wildcards. "9 arguments" tells you how many files the pattern matched before anything happens.
The rm -rf "$DIR"/* problem
Scripts often clear a directory like this:
rm -rf "$CACHE_DIR"/*Now suppose CACHE_DIR is empty, because a config file wasn't loaded, a variable was renamed, or
there's a typo. Bash replaces "$CACHE_DIR" with nothing, and /* matches everything in the root
directory. Never test this with rm. Put echo in front instead, which prints the command
Bash would have run:
LOG_DIR=""
echo rm -rf "$LOG_DIR"/*rm -rf /bin /bin.usr-is-merged /boot /dev /etc /home /init /lib /lib.usr-is-merged /lib64 /lost+found /media /mnt /opt /proc /root /run /sbin /sbin.usr-is-merged /snap /srv /sys /tmp /usr /varThat's every top-level directory on the machine. As a normal user, rm would fail on most of them,
but it would still delete everything you own: your whole home directory, /tmp, any mounted
drives you can write to. Run as root, it's the whole system. A typo produces the same result:
CACHE_DIR=~/projects/webshop/cache
echo rm -rf "$CAHCE_DIR"/*rm -rf /bin /bin.usr-is-merged /boot /dev /etc /home /init /lib /lib.usr-is-merged /lib64 /lost+found /media /mnt /opt /proc /root /run /sbin /sbin.usr-is-merged /snap /srv /sys /tmp /usr /varTwo guards fix this, and production scripts should use both. ${VAR:?} makes Bash stop with an
error if the variable is unset or empty, before running the command. set -u makes using any
unset variable an error:
echo rm -rf "${CAHCE_DIR:?}"/*
set -u
echo rm -rf "$CAHCE_DIR"/*
set +u
echo rm -rf "${CACHE_DIR:?}"/*bash: CAHCE_DIR: parameter null or not set
bash: CAHCE_DIR: unbound variable
rm -rf /home/sid/projects/webshop/cache/*In a script, both errors abort the script immediately, so the rm never runs. GNU rm has a
last-resort safety net of its own, from man rm:
--preserve-root[=all]
do not remove '/' (default); with 'all', reject any command line argument on a separate device from its
parentThat protects only the literal path /. It does nothing for /*, which expands to many separate
arguments, as the echo showed. Rely on ${VAR:?}, not on rm. Writing scripts that fail safely
is the subject of Error Handling, Traps, and Debugging.
File names that look like options
A file whose name starts with a dash can turn a harmless command into a dangerous one. Imagine an
export directory where some tool created a file literally called -rf:
mkdir -p ~/scratch/exports && cd ~/scratch/exports
touch -- a.csv b.csv -rf
mkdir keep
touch keep/important.csv
ls
echo rm *-rf a.csv b.csv keep
rm -rf a.csv b.csv keep* expands to the file names, and -rf sorts first, so rm * would have become rm -rf … keep
and recursively deleted the keep directory. With -- (end of options, from the
introduction), every name is treated as a file:
rm -v -- *removed '-rf'
removed 'a.csv'
removed 'b.csv'
rm: cannot remove 'keep': Is a directoryThe file called -rf was deleted like any other file, and keep survived. Writing ./* instead
of * has the same effect, because every name then starts with ./. Make -- a habit with
rm, cp and mv in scripts.
A softer delete for interactive use
If rm's permanence makes you nervous at the prompt, a small function in ~/.bashrc moves files
aside instead (functions were introduced in the setup article):
trash() { mkdir -p ~/.trash && mv -v -- "$@" ~/.trash/; }
touch notes-draft.md
trash notes-draft.md
ls -A ~/.trashrenamed 'notes-draft.md' -> '/home/sid/.trash/notes-draft.md'
notes-draft.md"$@" passes along all the function's arguments. It's not a replacement for backups (a second
file with the same name overwrites the first), but it does catch mistakes you notice straight away.
Viewing files
cat: the whole file
cat config/app.conf
cat -n config/nginx.conf[server]
port = 8080
workers = 4
[database]
host = db.internal
name = webshop
1 server {
2 listen 80;
3 server_name shop.example.com;
4 location / {
5 proxy_pass http://127.0.0.1:8080;
6 }
7 }-n numbers lines, which helps when an error message says "line 5". -A makes invisible
characters visible: tabs as ^I, Windows carriage returns as ^M, and the end of each line as
$. When a config file "looks right" but doesn't work, this is the first thing to try:
printf 'host = db.internal\t# primary\r\n' > config/db.conf
cat config/db.conf
cat -A config/db.confhost = db.internal # primary
host = db.internal^I# primary^M$Use cat for short files only. For anything longer than a screen, use less.
less: paging through large files
less logs/app-2026-11-09.log opens a file one screen at a time, without loading it all into
memory, so it's fine on multi-gigabyte logs. These keys cover nearly everything (they're the same
ones man uses):
| Key | Action |
|---|---|
Space / b | Next / previous page |
g / G | Start / end of file |
/ERROR then Enter | Search forward for ERROR. ?ERROR searches backward |
n / N | Next / previous match |
&ERROR then Enter | Show only lines matching ERROR (& alone shows everything again) |
F | Follow the file as it grows, like tail -f. Ctrl-C stops following |
-S | Toggle wrapping of long lines (useful for wide logs) |
q | Quit |
head and tail: the beginning and the end
head -n 3 logs/app-2026-11-09.log
tail -n 3 logs/app-2026-11-09.log2026-11-09 08:00:00 INFO order 8439 placed total=135.24
2026-11-09 08:07:13 INFO order 2681 placed total=69.25
2026-11-09 08:14:26 INFO order 6887 placed total=53.40
2026-11-09 19:29:11 INFO order 6549 placed total=110.30
2026-11-09 19:36:24 INFO order 2304 placed total=12.82
2026-11-09 19:43:37 INFO order 1563 placed total=52.31Both default to 10 lines. tail -n +N means "starting at line N", which is the standard way to
drop a CSV header:
head -n 1 orders.csv
tail -n +2 orders.csvorder_id,customer,total,status
1042,acme-gmbh,149.90,paid
1043,j.doe,23.50,refunded
1044,northwind,980.00,paid
1045,j.doe,12.00,pendingFollowing a log: tail -f versus tail -F
tail -f file keeps the file open and prints new lines as they're written. It's how you watch an
application while you reproduce a bug. There's a catch on any server with log rotation. When
logrotate (or the app) renames app.log to app.log.1 and starts a fresh app.log, tail -f
keeps following the renamed file, because it follows the open file, not the name. tail -F
follows the name, and notices when it's replaced.
To see the difference I ran both against the same file while a second shell wrote one line per second and renamed the log after the third line:
for i in 1 2 3 4 5 6; do
echo "$(date +%T) INFO order $((1040 + i)) placed" >> app.log
sleep 1
if [ "$i" -eq 3 ]; then mv app.log app.log.1; fi
doneOutput of tail -f app.log:
21:59:33 INFO order 1041 placed
21:59:34 INFO order 1042 placed
21:59:35 INFO order 1043 placedOutput of tail -F app.log:
21:59:33 INFO order 1041 placed
21:59:34 INFO order 1042 placed
21:59:35 INFO order 1043 placed
tail: 'app.log' has become inaccessible: No such file or directory
tail: 'app.log' has appeared; following new file
21:59:36 INFO order 1044 placed
21:59:37 INFO order 1045 placed
21:59:38 INFO order 1046 placedtail -f sat silently while three more orders were logged. If you were watching for an error, you
would have missed it. Use tail -F for log files, always. Ctrl-C stops either one.
wc: counting lines, words and bytes
wc logs/app-2026-11-09.log
wc -l logs/app-2026-11-0*.log
wc -l < logs/app-2026-11-09.log 110 770 6131 logs/app-2026-11-09.log
111 logs/app-2026-11-01.log
133 logs/app-2026-11-02.log
133 logs/app-2026-11-03.log
140 logs/app-2026-11-04.log
74 logs/app-2026-11-05.log
78 logs/app-2026-11-06.log
137 logs/app-2026-11-07.log
80 logs/app-2026-11-08.log
110 logs/app-2026-11-09.log
996 total
110The three numbers are lines, words and bytes. -l counts lines only and adds a total for several
files. With < (input redirection), wc reads the file's contents without knowing its name, so it
prints only the number. That's the form you want when capturing a count in a script.
Inspecting files: file and stat
file identifies content by looking inside it, ignoring the name:
file config/app.conf deploy.sh orders.csv logs/access.log.2.gz ~/srv/webshop/currentconfig/app.conf: ASCII text
deploy.sh: Bourne-Again shell script, ASCII text executable
orders.csv: CSV ASCII text
logs/access.log.2.gz: gzip compressed data, was "access.log.1", last modified: Tue Nov 10 02:57:36 2026, from Unix, original size modulo 2^32 373019
/home/sid/srv/webshop/current: symbolic link to releases/v1.4.2(The gzip file stores the original file's modification time, and file prints it in UTC. That's
why the date there is "Nov 10" when the file was compressed on the evening of Nov 9 local time.)
stat shows everything the filesystem knows about a file:
stat config/nginx.conf File: config/nginx.conf
Size: 125 Blocks: 8 IO Block: 4096 regular file
Device: 8,48 Inode: 57147 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/ sid) Gid: ( 1000/ sid)
Access: 2026-11-09 21:57:55.262882046 -0500
Modify: 2026-11-09 21:57:54.855548700 -0500
Change: 2026-11-09 21:57:54.855548700 -0500
Birth: 2026-11-09 21:57:36.559884473 -0500The four timestamps are a common source of confusion:
| Timestamp | Changes when | Shown by |
|---|---|---|
| Modify (mtime) | The file's content changes | ls -l |
| Change (ctime) | The content or the metadata (permissions, owner, name, link count) changes | ls -lc |
| Access (atime) | The file is read (on most systems only roughly, for performance) | ls -lu |
| Birth | The file was created (if the filesystem records it; ext4 does) | stat |
ctime is not "creation time", despite what many tutorials say. -c with a format string picks
out exactly the fields you need, which is useful in scripts:
stat -c '%n: %s bytes, modified %y' config/nginx.confconfig/nginx.conf: 125 bytes, modified 2026-11-09 21:57:54.855548700 -0500Hard links and symbolic links
Inodes and hard links
A file on disk is an inode: a numbered record holding the data's location, size, owner, permissions and timestamps. A file name is just a directory entry pointing at an inode. A hard link is a second name for the same inode:
ln config/app.conf app-hardlink.conf
ls -li config/app.conf app-hardlink.conf57067 -rw-r--r-- 2 sid sid 79 Nov 9 21:57 app-hardlink.conf
57067 -rw-r--r-- 2 sid sid 79 Nov 9 21:57 config/app.confSame inode number (57067), and the link count in the second column is now 2. Neither name is the "original". They're equal. A change through one name is visible through the other, because there's only one file:
echo "timeout = 30" >> app-hardlink.conf
tail -n 2 config/app.confname = webshop
timeout = 30Deleting a name removes one link. The data is freed only when the count reaches zero:
rm config/app.conf
ls -li app-hardlink.conf
tail -n 2 app-hardlink.conf57067 -rw-r--r-- 1 sid sid 92 Nov 9 21:58 app-hardlink.conf
name = webshop
timeout = 30That's what rm really does: it unlinks a name. Backup tools such as rsync --link-dest use
hard links to keep many daily snapshots where unchanged files take up space only once. (We put the
file back with mv app-hardlink.conf config/app.conf before continuing.)
Hard links have two limits, and both produce clear errors:
ln config hl-config
ln config/app.conf /dev/shm/app.confln: config: hard link not allowed for directory
ln: failed to create hard link '/dev/shm/app.conf' => 'config/app.conf': Invalid cross-device linkThey can't point to directories, and they can't cross filesystems, because inode numbers only mean something within one filesystem.
Symbolic links
A symbolic link (ln -s target name) is a separate small file whose content is a path. It
can point to directories, across filesystems, or to things that don't exist yet:
ln -s config/app.conf app.conf
ls -li app.conf config/app.conf
head -n 3 app.conf65396 lrwxrwxrwx 1 sid sid 15 Nov 9 21:58 app.conf -> config/app.conf
57067 -rw-r--r-- 1 sid sid 92 Nov 9 21:58 config/app.conf
[server]
port = 8080
workers = 4Different inode, type l, and its size (15) is the length of the path config/app.conf. Reading
through it reads the target. But if the target moves, the link breaks. It still exists, pointing
at a path that's no longer there:
mv config/app.conf config/app.conf.moved
cat app.conf
ls -l app.confcat: app.conf: No such file or directory
lrwxrwxrwx 1 sid sid 15 Nov 9 21:58 app.conf -> config/app.confThis is a dangling symlink. ls happily lists it (in red, with Ubuntu's colours), while every
attempt to read it says "No such file or directory", which is confusing until you know why. Move
the target back and the link works again, because it only ever stored a path.
| Hard link | Symbolic link | |
|---|---|---|
| What it is | Another name for the same inode | A small file containing a path |
| Survives deleting the "original" | Yes, the data stays until the last name is gone | No, it dangles |
| Can point to a directory | No | Yes |
| Can cross filesystems | No | Yes |
ls -l shows | - and a link count above 1 | l and name -> target |
| Typical use | Snapshot backups, deduplication | current release links, config pointing into a shared directory, versioned binaries |
Relative targets are relative to the link
This one catches almost everyone. A relative target is resolved from the directory the link lives in, not from where you were when you created it:
ln -s config/nginx.conf ~/srv/webshop/shared/config/nginx.conf
ls -l ~/srv/webshop/shared/config/
cat ~/srv/webshop/shared/config/nginx.conftotal 4
-rw-r--r-- 1 sid sid 79 Nov 4 14:20 app.conf
lrwxrwxrwx 1 sid sid 17 Nov 9 21:58 nginx.conf -> config/nginx.conf
cat: /home/sid/srv/webshop/shared/config/nginx.conf: No such file or directoryThe link points to shared/config/config/nginx.conf, which doesn't exist. Either give an absolute
target, or let ln -r work out the correct relative path for you (-f replaces the broken link):
ln -sfr config/nginx.conf ~/srv/webshop/shared/config/nginx.conf
ls -l ~/srv/webshop/shared/config/nginx.conf
head -n 2 ~/srv/webshop/shared/config/nginx.conflrwxrwxrwx 1 sid sid 46 Nov 9 21:58 /home/sid/srv/webshop/shared/config/nginx.conf -> ../../../../projects/webshop/config/nginx.conf
server {
listen 80;Switching a release: ln -sfn
Deployments switch the current link from one release to another. The obvious command has a trap:
cd ~/srv/webshop
ln -sf releases/v1.4.1 current
readlink current
ls -l releases/v1.4.2/releases/v1.4.2
total 12
-rw-r--r-- 1 sid sid 6 Nov 6 18:07 VERSION
drwxr-xr-x 2 sid sid 4096 Nov 6 18:07 api
-rw-r--r-- 1 sid sid 150 Nov 6 18:07 app.py
lrwxrwxrwx 1 sid sid 19 Nov 6 18:07 config -> ../../shared/config
lrwxrwxrwx 1 sid sid 17 Nov 6 18:07 logs -> ../../shared/logs
lrwxrwxrwx 1 sid sid 15 Nov 9 21:58 v1.4.1 -> releases/v1.4.1current still points at v1.4.2. Because current is a link to a directory, ln treated it as
"put the new link inside this directory" and created a stray v1.4.1 link inside the live release.
The rollback silently did nothing. -n tells ln to treat an existing link as a plain file and
replace it:
rm releases/v1.4.2/v1.4.1
ln -sfn releases/v1.4.1 current
readlink current
ln -sfn releases/v1.4.3 current
readlink currentreleases/v1.4.1
releases/v1.4.3For deploy scripts, ln -sfn is the line to remember. The
deployment capstone builds a full release switch
around it.
Disk usage: du and df
ls -l shows file sizes, and as the previous article warned, a directory's "size" says nothing
about its contents. du (disk usage) adds up what's actually stored:
cd ~/projects/webshop
du -sh logs
du -sh * | sort -h944K logs
4.0K README.md
4.0K deploy.sh
4.0K orders.csv
8.0K docs
16K config
20K backups
20K src
944K logs-s gives one summary line per argument, -h makes it human-readable, and sort -h sorts
human-readable sizes correctly (so 944K comes after 20K). du -sh * | sort -h is the command
to reach for when a disk fills up. Run it at the top of the suspect directory, then cd into the
biggest entry and repeat. --max-depth=1 (or -d 1) shows one level of subdirectories plus the
total:
du -h --max-depth=1 ~/projects/webshop16K /home/sid/projects/webshop/config
944K /home/sid/projects/webshop/logs
8.0K /home/sid/projects/webshop/docs
20K /home/sid/projects/webshop/src
20K /home/sid/projects/webshop/backups
1.1M /home/sid/projects/webshopdf (disk free) works the other way round. It reports how full each filesystem is. Give it a
path to see only the filesystem that path is on:
df -h ~Filesystem Size Used Avail Use% Mounted on
/dev/sdd 1007G 4.6G 952G 1% /home/sidls and du can disagree, and the difference matters. ls reports a file's apparent size, while
du reports the disk blocks actually allocated. A sparse file, where the system records "500 MB
of zeros" without storing them, makes this obvious:
truncate -s 500M disk-test.img
ls -lh disk-test.img
du -h disk-test.img
rm disk-test.img-rw-r--r-- 1 sid sid 500M Nov 9 21:58 disk-test.img
0 disk-test.imgVirtual machine disk images and database files are often sparse. When you're working out what's
filling a disk, trust du.
Where you'll use this
Back to the overgrown log directory. The task: archive October's application logs in their own directory, compress them, and back up the config before an edit. First, look before touching anything:
cd ~/projects/webshop/logs
ls
du -sh .access.log app-2026-10-10.log app-2026-10-22.log app-2026-11-03.log
access.log.1 app-2026-10-11.log app-2026-10-23.log app-2026-11-04.log
access.log.2.gz app-2026-10-12.log app-2026-10-24.log app-2026-11-05.log
app-2026-10-01.log app-2026-10-13.log app-2026-10-25.log app-2026-11-06.log
app-2026-10-02.log app-2026-10-14.log app-2026-10-26.log app-2026-11-07.log
app-2026-10-03.log app-2026-10-15.log app-2026-10-27.log app-2026-11-08.log
app-2026-10-04.log app-2026-10-16.log app-2026-10-28.log app-2026-11-09.log
app-2026-10-05.log app-2026-10-17.log app-2026-10-29.log archive
app-2026-10-06.log app-2026-10-18.log app-2026-10-30.log error-2026-11-09.log
app-2026-10-07.log app-2026-10-19.log app-2026-10-31.log
app-2026-10-08.log app-2026-10-20.log app-2026-11-01.log
app-2026-10-09.log app-2026-10-21.log app-2026-11-02.log944K .Create the archive directory (-p, so re-running the job is harmless) and move one month's files
in. The pattern app-2026-10-*.log matches exactly the October logs. Checking the count
afterwards confirms that all 31 days arrived:
mkdir -p archive/2026-10
mv app-2026-10-*.log archive/2026-10/
ls archive/2026-10 | wc -l
du -sh archive/2026-1031
236K archive/2026-10Old logs are read rarely, so compress them. gzip replaces each file with a .gz version (read
them later with zcat or zless, no unpacking needed):
gzip archive/2026-10/*.log
ls archive/2026-10 | head -3
du -sh archive/2026-10app-2026-10-01.log.gz
app-2026-10-02.log.gz
app-2026-10-03.log.gz
128K archive/2026-10October now takes about half the space, and the working directory only shows what matters this month:
ls
du -sh .access.log app-2026-11-01.log app-2026-11-04.log app-2026-11-07.log archive
access.log.1 app-2026-11-02.log app-2026-11-05.log app-2026-11-08.log error-2026-11-09.log
access.log.2.gz app-2026-11-03.log app-2026-11-06.log app-2026-11-09.log836K .Finally, before editing the application config, keep a dated copy with the original timestamp:
cd ~/projects/webshop
cp -a config/app.conf "backups/config/2026-11/app.conf.$(date +%F)"
ls -l backups/config/2026-11/total 4
-rw-r--r-- 1 sid sid 92 Nov 9 21:58 app.conf.2026-11-09$(date +%F) inserts today's date, so repeated backups don't overwrite each other. These are the
same steps a log-cleanup cron job performs. Turning them into a script with error handling is the
subject of Real-World Scripts, and on most servers
logrotate does the rotation and compression part for system logs.
Common mistakes
Copying a directory without -r
cp: -r not specified; omitting directory 'config'cp skipped the directory and copied nothing. Add -r, or better -a for backups. In a script,
the trap is that the other arguments were still copied and cp exits with status 1, so check the
exit status.
Running the same cp -r twice
The first run creates config-backup, and the second creates config-backup/config, as shown in
the copying section. Use cp -r src/. dest/ (contents into an existing directory) or
rsync -a src/ dest/ when a copy step may run more than once.
Creating a symlink with a relative target from the wrong place
lrwxrwxrwx 1 sid sid 17 Nov 9 21:58 nginx.conf -> config/nginx.conf
cat: /home/sid/srv/webshop/shared/config/nginx.conf: No such file or directoryRelative targets are resolved from the link's own directory. Use ln -sr to have the path
computed correctly, or use an absolute target.
ln -sf on a link to a directory
ln -sf releases/v1.4.1 current created releases/v1.4.2/v1.4.1 and left current unchanged. Always
use ln -sfn to replace a link that points to a directory, then confirm with readlink.
An empty variable in rm -rf "$VAR"/*
Shown above: an empty or misspelled variable turns the command into rm -rf /*. Write
"${VAR:?}", use set -u in scripts, and preview destructive commands with echo first.
Summary
mkdir -pcreates missing parents and is safe to repeat.touchcreates empty files and resets modification times (-dsets any time).cpandmvoverwrite silently. Use-iinteractively,--update=noneormv -nin scripts, and-vto see what happened. Copy directories with-r, and use-awhenever timestamps, permissions or symlinks matter.cp -r src destbehaves differently depending on whetherdestexists. Usesrc/.to copy contents.rmis permanent.rmdirremoves only empty directories, andrm -Iasks once before big deletions. Guard variables in destructive commands with"${VAR:?}"andset -u, and use--so names starting with-can't become options.lessfor reading,head/tail -nfor the ends of a file,tail -n +2to skip a header, andtail -F(not-f) to follow logs that get rotated.fileidentifies content regardless of the name.statshows size, inode, link count and the timestamps. ctime is change time, not creation time.- A hard link is another name for the same inode, and data lives until the last name is removed.
A symlink stores a path, which is resolved relative to the link's directory and can dangle.
Switch directory links with
ln -sfn. du -sh * | sort -hfinds what's using space, anddf -h pathshows how full the filesystem is. Trustduoverlsfor disk usage.
Exercises
Before editing config/nginx.conf, make a copy called config/nginx.conf.orig that keeps the
original modification time. Show that the two timestamps are identical.
Show Solution
cp -a config/nginx.conf config/nginx.conf.orig
stat -c '%y %n' config/nginx.conf config/nginx.conf.orig2026-11-09 21:57:54.855548700 -0500 config/nginx.conf
2026-11-09 21:57:54.855548700 -0500 config/nginx.conf.orig-a preserves the timestamp, so you can still see when the configuration was last changed. With a
plain cp, the .orig file would show the current time.
Print the header line of orders.csv, then the number of data rows, not counting the header.
Show Solution
head -n 1 orders.csv
tail -n +2 orders.csv | wc -lorder_id,customer,total,status
4tail -n +2 starts output at line 2, which skips the header, and wc -l counts what's left.
wc -l orders.csv alone would say 5.
In an empty directory, create app.log containing current line and app.log.1 containing
old line. Rotate them so that the old app.log.1 becomes app.log.2, the current log becomes
app.log.1, and a new empty app.log exists. Show the result.
Hint
Work from the oldest file to the newest, or you'll overwrite something.
Show Solution
mkdir -p ~/rotate-demo && cd ~/rotate-demo
printf 'old line\n' > app.log.1
printf 'current line\n' > app.log
mv app.log.1 app.log.2
mv app.log app.log.1
touch app.log
ls -l
cat app.log.1total 8
-rw-r--r-- 1 sid sid 0 Nov 9 21:58 app.log
-rw-r--r-- 1 sid sid 13 Nov 9 21:58 app.log.1
-rw-r--r-- 1 sid sid 9 Nov 9 21:58 app.log.2
current lineIf you moved app.log to app.log.1 first, mv would silently overwrite the old app.log.1 and
that log would be lost. This "shift from the oldest end" order is exactly what logrotate does.
Create a hard link orders-link.csv to orders.csv. Show that both names have the same inode
number, delete orders.csv, and show that the data is still there. Then restore the original name.
Show Solution
ln orders.csv orders-link.csv
ls -li orders.csv orders-link.csv
rm orders.csv
wc -l orders-link.csv
mv orders-link.csv orders.csv65068 -rw-r--r-- 2 sid sid 136 Nov 9 09:40 orders-link.csv
65068 -rw-r--r-- 2 sid sid 136 Nov 9 09:40 orders.csv
5 orders-link.csvSame inode (65068), link count 2. Deleting one name dropped the count to 1 but didn't touch the
data. With ln -s instead, wc would have failed with "No such file or directory" after the rm.
Write a function clean_cache DIR that deletes everything inside DIR but refuses to run if the
argument is missing or empty, or isn't an existing directory. Test it on a real cache directory,
with no argument, and with a typo.
Hint
${1:?message} stops with message when $1 is empty. [ -d "$dir" ] tests for a directory.
Show Solution
clean_cache() {
local dir=${1:?usage: clean_cache DIR}
[ -d "$dir" ] || { echo "clean_cache: not a directory: $dir" >&2; return 1; }
rm -rf -- "$dir"/*
}
mkdir -p cache/img && touch cache/a.tmp cache/img/b.tmp
clean_cache cache
ls -A cache
clean_cache
clean_cache cahcebash: 1: usage: clean_cache DIR
clean_cache: not a directory: cahcels -A cache printed nothing: the cache was emptied, including the img subdirectory. Without an
argument, ${1:?…} stops before rm runs. With a typo, the -d test catches it. Without that test,
rm -rf -- "cahce"/* would have done nothing here, but the habit matters for paths that do
exist. One limitation to know: "$dir"/* doesn't match hidden files, so .something entries
survive. Wildcards, Globbing, and Brace Expansion shows how
to include them.
Tested with: GNU Bash 5.2.21 and GNU coreutils 9.4 on Ubuntu 24.04 (WSL2), November 2026.
Everything was run in one continuous interactive session in the order shown, starting from a
sample project with synthetic logs, so later outputs reflect earlier steps (inode numbers and times will
differ on your machine). The tail -f/-F comparison ran both commands side by side against the
same rotating file. The less keys in the table were each tried in a terminal on the sample log.
The rm -rf "$VAR"/* examples were run only with echo in front, never as real deletions. Two things surprised me while testing: coreutils 9.4 now warns about cp -n, and
mv -n exits with status 1 when it skips a file.
Written by The Dark Debugger
Engineer documenting the things that took me too long to learn, so they take you less time. Every Bash tutorial here is tested end to end before publishing.