
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
Globbing and the shell
3 min readYou 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 a filename pattern using wildcards such as * and ? that the shell expands into a list of matching names 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).
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.
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 logDeleted but still open
3 min readrm does not destroy a file; it calls the the operation that removes a name from a directory and decrements the inode's link count 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 a process's handle to an open file, used by read and write, independent of the file's name, 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
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
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
cat <&3
The data is still there, read through the open descriptor. Close it and the blocks are finally freed.
Named and open: the file has one name and one open descriptor; the link count is 1.
Unlinked:
rmremoves the name and the link count drops to 0, but the descriptor keeps the inode referenced.Still allocated: blocks stay allocated, so
dfstill counts them whileduno longer sees them.Visible as (deleted):
/proc/<pid>/fd/shows the descriptor pointing at a path marked(deleted).Last close frees it: when the final descriptor closes, the inode and its blocks are reclaimed and the space appears.
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
Common mistakes and how professionals diagnose them
3 min readThese 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 withls -lto see the arrow, then either recreate the target or remove the link.f?.txtdid not matchf10.txt.?matches exactly one character andf10has two. Diagnose by reading the pattern aloud, then usef*.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 withdfon both paths; useln -sinstead.- Disk still full after
rm. A process still holds the deleted file open. Diagnose withlsof +L1, which lists files with a link count below 1, then restart or close that process. duanddfdisagree wildly. Either deleted-but-open files (found withlsof +L1) or files hidden underneath a mount point (mounting over a non-empty directory hides its contents fromdubut not fromdf).No space left on devicewhiledf -hshows free space. The inode table is full. Checkdf -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
statshowingLinks: 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.
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.
Try it
1 min readOpen 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
- Explain to a colleague, in two sentences: select files with glob patterns and tell globbing apart from brace expansion and regular expressions.
- Which objective are you least sure about? Re-read that section, then do its exercise in the practice pack.
- 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
- Man pageunlink(2): Linux man page
- Man pageproc(5): Linux man page
- BookThe Linux Programming Interface (Michael Kerrisk)
- StandardFilesystem Hierarchy Standard 3.0
- Man pagehier(7): filesystem layout
- Man pageglob(7): pathname patterns
- Free bookThe Linux Command Line (William Shotts)
Official documentation first. Links checked when the lesson was published.