Module 1 · Lesson 11 of 103

Globbing and the lifetime of an open file

Expand glob patterns, tell them apart from brace expansion and regex, and diagnose and recover deleted-but-open files and a df-versus-du mismatch.

  • Beginner to advanced
  • 7 min read
  • 14 min lab
What you will need

Ubuntu 24.04 (WSL2, VM or container)

Your place in the module
By the end you can 3 objectives
  • Select files with glob patterns and tell globbing apart from brace expansion and regular expressions.
  • Explain why a deleted file can still occupy disk space and diagnose the df-versus-du mismatch.
  • Diagnose a full disk with lsof +L1 and df -i, and recover a deleted-but-open file through /proc/pid/fd
Video coming soonGlobbing and the lifetime of an open file
The video for this lesson is being recorded. Read the lesson below in the meantime.
Recall first from Links and the directory entry
  • A hard link is a second name for the same inode; a symlink stores a path and dangles when its target is deleted.
  • Hard links cannot cross filesystems or point at directories; symlinks can do both.
  • The inode holds type, permissions, ownership, size, atime, mtime, ctime, link count and block pointers; the directory entry holds only a name and an inode number
01

Globbing and the shell

3 min read

You now know that names point at inodes and that links add or remove names. This lesson answers how the shell expands patterns and why a deleted file can still be written.

The shell can expand patterns before it runs a command, which is how you select many files at once. A is expanded by the shell itself, not by the program you run. ls *.txt never sees the asterisk: the shell replaces the pattern with every matching filename first. * matches any number of characters, ? matches exactly one, and [...] matches one of the listed characters or a range such as [a-c].

The classic mistake is thinking ? means one or more. It means exactly one. So f?.txt matches f1.txt but not f10.txt (two characters) or f.txt (zero).

bash
ls f?.txt
mkdir txt log png bak
mv *.txt txt/
mv *.log log/

{a,b} is different. It is brace expansion, which happens even when no files match, and it is not really a wildcard: {txt,log} expands to the two words txt log regardless of what is on disk. Two more subtleties matter. * does not match a leading dot, so hidden files such as .bashrc need .* or .[!.]* explicitly. And if a pattern matches nothing, the shell by default passes the literal pattern to the program, which is why a typo can produce a confusing "No such file or directory" naming the pattern itself.

Going deeperGlobbing is not a regular expression

People who know regex often reach for * and . the way regex uses them, and it breaks. In a glob, * means any characters, not "zero or more of the previous thing", and . is a literal dot, not "any character". So *.txt means "anything ending in .txt", while the equivalent regex would be .*\.txt. A glob also matches whole names, while a regex can match part of a line. grep takes a regex; the shell's filename expansion is a glob. Keep the two syntaxes separate.

Bash gives you control over the edge cases. With shopt -s nullglob, a pattern that matches nothing expands to nothing at all instead of the literal pattern, which removes a whole class of surprising errors in loops. With shopt -s failglob, the shell reports an error instead. With shopt -s extglob, you get richer patterns such as @(a|b) for one of several and +(x) for one or more, though these stray beyond the portable basics.

Glob patternsWhat itmatchesExample*anycharacters*.txt matchesa.txt?exactly onecharacterf?.txtmatchesf1.txt[12]one listedcharacter[12].txtmatches 1.txt{a,b}eachalternativebraceexpansion,not awildcard
The glob characters * ? [12] and the brace expansion {a,b}, each matched against example filenames to show which names each selects.
The shell expands globs before the command runs
f2.txt
learner@embeddex:~/lab$ # * matches any run, including zero: f.txt is back too
learner@embeddex:~/lab$ ls f*.txt
f.txt
f1.txt
f10.txt
f2.txt
learner@embeddex:~/lab$ mkdir txt log
learner@embeddex:~/lab$ # The shell expands *.txt before mv ever runs
learner@embeddex:~/lab$ mv *.txt txt/
learner@embeddex:~/lab$ mv *.log log/
learner@embeddex:~/lab$ ls txt log
log:
app.log

txt:
f.txt
f1.txt
f10.txt
f2.txt
learner@embeddex:~/lab$ # Brace expansion works even when no file matches
learner@embeddex:~/lab$ echo {txt,log}
txt log
02

Deleted but still open

3 min read

rm does not destroy a file; it calls the system call, which removes one name and decrements the link count. The data is freed only when two things are both true: the link count has reached zero, and no process has the file open. If a process still holds a , the kernel keeps the inode and its blocks alive until the last descriptor closes. This is why df can report a disk full while du finds nothing large: a deleted file is still held open, invisible to directory listings but still using blocks.

Two levels are involved, and it is worth keeping them straight. The file descriptor is the small number a program holds; it points to an open file description, which records the file offset and mode; that in turn points to the inode. When a process is forked, the child shares the same open file description, and that is exactly why a deleted log file does not free its space until every process that inherited the descriptor has closed it. Log rotation depends on this: a daemon keeps writing to a renamed and deleted file until it is told to reopen, which is why logrotate sends a signal after rotating.

1hold a file open and delete its name

bash
echo secret > data.bin
exec 3<data.bin
rm data.bin

exec 3<data.bin opens the file on descriptor 3 in the current shell. Deleting the name removes the only directory entry, but the descriptor still refers to the inode.

2see the proof in /proc

bash
ls -l /proc/$$/fd/3

The entry shows descriptor 3 still pointing at data.bin, with the path marked (deleted). The inode is alive even though no directory lists the name.

3read the data back

bash
cat <&3

The data is still there, read through the open descriptor. Close it and the blocks are finally freed.

Step by stepLife of a deleted-but-open file
  1. Named and open: the file has one name and one open descriptor; the link count is 1.

  2. Unlinked: rm removes the name and the link count drops to 0, but the descriptor keeps the inode referenced.

  3. Still allocated: blocks stay allocated, so df still counts them while du no longer sees them.

  4. Visible as (deleted): /proc/<pid>/fd/ shows the descriptor pointing at a path marked (deleted).

  5. Last close frees it: when the final descriptor closes, the inode and its blocks are reclaimed and the space appears.

Deleted but still openNamed and openlinks = 1Unlinked, openlinks = 0Freedblocks reclaimedTransitionsunlink (rm)Named and openlinks = 1Unlinked, openlinks = 0last closeUnlinked, openlinks = 0Freedblocks reclaimedNamed and openlinks = 1Unlinked, openlinks = 0Freedblocks reclaimedTransitionsunlink (rm)Named andopenlinks = 1Unlinked,openlinks = 0last closeUnlinked,openlinks = 0Freedblocksreclaimed
A deleted file whose inode and data blocks are still allocated because a running process holds an open file descriptor, visible under /proc/pid/fd with a (deleted) marker.
Deleted, but still open: the data lives on
learner@embeddex:~/lab$ echo secret > data.bin
learner@embeddex:~/lab$ # open the file on descriptor 3
learner@embeddex:~/lab$ exec 3<data.bin
learner@embeddex:~/lab$ rm data.bin
learner@embeddex:~/lab$ # the kernel still knows the file, marked (deleted)
learner@embeddex:~/lab$ ls -l /proc/$$/fd/3 | sed 's/.*-> //'
/home/learner/lab/data.bin (deleted)
learner@embeddex:~/lab$ cat <&3
secret
03

Common mistakes and how professionals diagnose them

3 min read

These are the mistakes learners hit most, with the symptom, the cause, and the diagnostic that proves it.

  • cat link.txt: No such file or directory. The symlink's target was deleted, so the link dangles. Diagnose with ls -l to see the arrow, then either recreate the target or remove the link.
  • f?.txt did not match f10.txt. ? matches exactly one character and f10 has two. Diagnose by reading the pattern aloud, then use f*.txt, where * matches any number of characters, including none.
  • ln: Invalid cross-device link. A hard link cannot cross filesystems, because inode numbers are local to one filesystem. Diagnose with df on both paths; use ln -s instead.
  • Disk still full after rm. A process still holds the deleted file open. Diagnose with lsof +L1, which lists files with a link count below 1, then restart or close that process.
  • du and df disagree wildly. Either deleted-but-open files (found with lsof +L1) or files hidden underneath a mount point (mounting over a non-empty directory hides its contents from du but not from df).
  • No space left on device while df -h shows free space. The inode table is full. Check df -i.
  • cd .. at / did nothing. The root has no parent; the kernel maps .. at the root to / again. This is expected, not an error.
  • Confused by stat showing Links: 1. It is the count of names pointing at the inode. Hard-link the file and watch it become 2; a symlink does not change it.
Going deeperInode exhaustion on small embedded filesystems

A filesystem can be full in two independent ways: out of data blocks, or out of inodes. Embedded flash filesystems are sized tightly, and a directory full of thousands of tiny files can exhaust the inode table long before the data blocks are used. df -i shows inode usage separately from df's block usage, and a filesystem at 100% inodes refuses to create new files even with gigabytes free. This shows up on real devices as log files that cannot be written or packages that fail to install with No space left on device. On ext4 the inode count is fixed at mkfs time, so the fix is either to remove small files or recreate the filesystem with a larger inode count. Tools like find -printf or a count of directory entries reveal where the inodes went.

Verify before you fix

Before changing anything, confirm the diagnosis with one command: ls -l for a dangling link, lsof +L1 for a deleted-but-open file, and df -i for inode exhaustion. The fix that matches the proven cause is usually small.

Disk-full diagnosisdf full, dusmalllsof +L1deleted-but-open?restartprocessdf -iinodesexhausted?Exit from step 2: no deleted filescheck df -i and mountsremovesmall filesdf full, dusmalllsof +L1deleted-but-open?Exit from step 2: no deletedfilescheck df -i and mountsrestartprocessdf -iinodesexhausted?removesmall files
A diagnostic flow from the symptom of a full disk or a failing link through lsof +L1, df -i and ls -l checks to the correct fix.
04

Try it

1 min read

Open the lab for this session and work through it in a scratch directory. Organise 500 mixed files into four folders using four mv commands, run the hard-link and symlink experiment and record which link still reads after you delete the original, then hold a file open, delete it, and recover the data through /proc/$$/fd/. Each exercise proves one idea from this lesson, and the practice pack's questions and interview prompts cover the same ground. Do the lab before moving on to viewing and editing text.

Key takeaways

  • Globbing is done by the shell: * matches any run, ? exactly one character, and {a,b} is brace expansion.
  • Deleting a name only lowers the link count; the data lives until the last open file descriptor closes.
  • A deleted-but-open file still uses blocks: df counts them while du does not, and /proc/pid/fd shows the descriptor marked (deleted)
  • A full disk can mean a full inode table; df -i shows inode usage separately, and mounting over a non-empty directory hides files from du but not df
Reflect
  1. Explain to a colleague, in two sentences: select files with glob patterns and tell globbing apart from brace expansion and regular expressions.
  2. Which objective are you least sure about? Re-read that section, then do its exercise in the practice pack.
  3. What surprised you when you ran the commands yourself?
On a real board

Repeat the key commands on your Raspberry Pi or lab board over SSH or the serial console. Note anything that behaves differently from your PC, such as missing tools, a BusyBox shell or a read-only filesystem.

References and further reading

Find your next step

↑ ↓ to move · Enter to open · Esc to close