5.4 Special Directories and Files
Key Takeaways
- /tmp and /var/tmp hold temporary files; both commonly use the sticky bit so users cannot delete each other’s files
- The sticky bit on a directory (often shown as t in the others-execute position) restricts deletion/rename to file owners (and root)
- ls -d lists directory entries themselves instead of descending into their contents
- ln -s creates a symbolic link—a pointer to another path that can cross filesystems and target directories
- Broken symlinks remain as link files even if the target is missing—verify targets when troubleshooting
5.4 Special Directories and Files
Not every directory behaves like a private home folder. Shared temporary areas need extra rules so users cannot erase each other’s work. Shortcuts across the tree use symbolic links. Topic 5.4 focuses on /tmp, /var/tmp, the sticky bit, ls -d, and ln -s.
Temporary directories: /tmp and /var/tmp
| Directory | Typical role | Persistence note |
|---|---|---|
/tmp/ | General temporary files for programs and users | Often cleared on reboot (policy varies) |
/var/tmp/ | Temporary files that should survive longer | More likely to persist across reboots |
Both locations are usually world-writable so any user can create temp files. Without an extra control, directory write permission would let User A delete User B’s file. The sticky bit solves that classic problem.
The sticky bit on directories
When sticky is set, users may still create files if the directory is writable, but they generally may delete or rename only their own files (root still can manage everything). That is why sticky directories fit shared temp space.
In ls -l output, sticky directories often look like:
drwxrwxrwt ... /tmp
The final t in rwxrwxrwt replaces the usual others-execute x. A capital T can appear if sticky is set without others-execute.
| Symbol in others execute slot | Meaning (directory) |
|---|---|
t | Sticky bit set and others have execute |
T | Sticky bit set without others execute |
x | Others execute, no sticky |
sudo chmod +t /var/tmp
sudo chmod 1777 /shared/dropbox
Octal 1000 adds sticky; 1777 is a common shared-scratch mode (sticky + wide rwx). Essentials wants recognition of sticky-on-directory for /tmp, not a deep setuid lesson.
Worked example: Alice creates
/tmp/alice.out; Bob creates/tmp/bob.out. With sticky set, Bob cannot deletealice.outeven though he can write to/tmp. He can still deletebob.out.
Sticky-bit scenarios
| Situation | Sticky? | Result |
|---|---|---|
Bob runs rm /tmp/alice.out | Yes (t) | Denied (not owner) |
Same rm after sticky cleared | No | Bob can delete Alice’s file |
Carol renames Dave’s file in a 1777 dropbox | Yes | Denied |
| Carol deletes her own file there | Yes | Allowed |
ls -ld /tmp # look for ...rwxrwt
touch /tmp/$USER-test
# other user: rm /tmp/<yours>-test → fails when sticky set
rm /tmp/$USER-test # owner cleanup OK
Applications pick /var/tmp when a temp file should survive reboot; both directories commonly show sticky in ls -ld. If a classmate can delete your /tmp file, the first troubleshooting glance is whether sticky is still present on the directory.
Listing directories with ls -d
By default, ls dirname shows contents. ls -d shows the directory entry itself.
ls -ld /tmp
ls -ld /home/*/project
| Command | Typical result |
|---|---|
ls /tmp | Names inside /tmp |
ls -d /tmp | Just the /tmp path |
ls -ld /tmp | Long listing for /tmp—mode bits (including sticky t) visible |
Use ls -ld when you care about directory permissions, not a flood of temporary filenames.
Symbolic links with ln -s
A symbolic link stores a path pointing to another file or directory:
ln -s /usr/share/doc/demo/README.md ~/readme-link
ln -s /var/log ~/logs
| Feature | Symbolic link (ln -s) |
|---|---|
| Points to | A path (absolute or relative) |
| Can cross filesystems? | Yes |
| Can link to directories? | Yes |
| If target is removed | Link remains but is broken |
Long listings show type l and often linkname -> target:
lrwxrwxrwx 1 alice alice 12 Jul 23 10:00 docs -> /srv/docs
Link mode bits are commonly rwxrwxrwx; the target’s permissions still govern access to the real data when you follow the link.
Worked example:
ln -s /home/alice/projects/notes/todo.txt ~/Desktop/todo.txtputs a Desktop shortcut on the real notes file. Move the real file without updating the link and the Desktop entry becomes broken.
Relative targets (ln -s ../shared/assets assets) travel with a project tree; absolute targets are better when link and target may move separately. ln -s /tmp/missing ~/broken-link still creates type l—fix the target or rm the pointer only. Always confirm with ls -l that you see -> pointing where you intended before you rely on the shortcut.
Hard links vs symlinks (awareness only)
Essentials emphasizes ln -s. Plain ln creates a hard link (same inode, usually same filesystem, not for directories). Shortcuts to directories or cross-filesystem pointers → think symbolic.
Practical checklist for Topic 5.4
- Expect
/tmpand often/var/tmpto be sticky shared temp spaces. - Recognize
t/Tin directory modes as sticky indicators. - Use
ls -ldto inspect a directory’s own mode. - Create shortcuts with
ln -sand verify withls -l. - Deleting a symlink removes the pointer, not necessarily the target.
Together with accounts (5.1–5.2) and chmod/chown (5.3), special directories and symlinks complete Topic 5. Quick self-check: ls -ld /tmp (expect sticky t), create a disposable ln -s, then remove only the link name when cleaning up.
Why do /tmp and similar shared temporary directories typically use the sticky bit?
Which command creates a symbolic link named website pointing to /var/www/html?
You need to see the permissions on the /tmp directory itself, not a list of files inside it. Which command is most appropriate?
In a long listing, a directory mode ends with t as in drwxrwxrwt. What does that primarily indicate?
You've completed this section
Continue exploring other exams