One of the interesting things about UNIX is that it is an anthropologist’s dream. It is full of artifacts from 35+ years of hundreds of programmers hacking away. One of the things I have always liked about UNIX is the fact that it is old, arcane, and inscrutable. I’ve been working with UNIX systems for more than ten years now, and I’m always surprised by the fact that I’ve only scratched the surface of what is there. Even after ten years, there is always something new to learn.
For instance, on many UNIX and probably most Linux systems too, you will find this in /usr/include/sys/fs/ufs_fs.h:
#define FS_MAGIC 0×011954
This would be the “Magic Number” stamped on a UFS filesystem so that other programs wanting to interact with that filesystem would know what type of filesystem they were dealing with. So, where did the number 011954 come from? Well, it is apparently the birthday of one of the programmers who worked on the filesystem in the 1970’s… 01-19-54. 30 years later it is still there, and probably will remain there until everyone stops supporting UFS. As of Solaris 10, Sun still uses UFS as its default filesystem. Once ZFS is integrated into Solaris production releases, this will hopefully change. Mac OS 10.4 uses HFS+ but will still read UFS volumes. Linux uses a variety of different filesystems, depending on the distribution, but can also read UFS.
Another interesting artifact is the way that the shell is responsible for expanding things like “*”.
Let’s say you have a directory with three files in: file1 file2 file3, and three directories dir1, dir2, dir3. You want to remove the files, so you type:
rm *
Knowing that rm won’t remove the directories without the -r option. Here’s what happens:
| BASH: “Hmm. I need to figure out what * means, so I can pass that to rm… Let’s see, there’s file1, file2, file3, dir1, dir2, dir3. Oh, Mr. RM! “
RM: “You have some files for me to delete, yes? Here I am, brain the size of a planet…”
BASH: “Yes. dir1, dir2, dir3, file1, file2, file3. They are in alphabetical order for you.”
RM: “Thank you kindly. remove:
dir1 (Can’t. It is a directory)
dir 2 (Can’t. It is a directory)
dir3 (Can’t. It is a directory)
file1 (ok)
file2 (ok)
file3 (ok)
DONE!
|
Now, this is fine until we have a file named “-r”
$ touch -- -r$ ls -ltotal 0-rw-r--r-- 1 steelmi1 steelmi1 0 Dec 4 11:03 -rdrwxr-xr-x 2 steelmi1 steelmi1 68 Dec 4 11:03 dir1drwxr-xr-x 2 steelmi1 steelmi1 68 Dec 4 11:03 dir2drwxr-xr-x 2 steelmi1 steelmi1 68 Dec 4 11:03 dir3-rw-r--r-- 1 steelmi1 steelmi1 0 Dec 4 11:04 file1-rw-r--r-- 1 steelmi1 steelmi1 0 Dec 4 11:04 file2-rw-r--r-- 1 steelmi1 steelmi1 0 Dec 4 11:04 file3$ rm *
BASH: “Hmm. I need to figure out what * means, so I can pass that to rm… Let’s see, there’s -r, file1, file2, file3, dir1, dir2, dir3. Oh, Mr. RM! “
RM: “You have some files for me to delete, yes? Here I am, brain the size of a planet…”
BASH: “Yes. -r, dir1, dir2, dir3, file1, file2, file3. They are in alphabetical order for you.”
RM: “Thank you kindly. remove:
-r (Hey, that’s an option! I’d better do what it says from now on!)
dir1 (ok, -r was specified!)
dir 2 (ok, -r was specified!)
dir3 (ok, -r was specified!)
file1 (ok)
file2 (ok)
file3 (ok)
DONE!
$ ls -ltotal 0-rw-r--r-- 1 steelmi1 steelmi1 0 Dec 4 11:03 -r$
|
So, as you can see, if you use the “*” wildcard, your SHELL expands it. Not the command you think you’re passing “*” to. And the result of this can be deadly. With a file called “-r” in our directory, rm * just wiped out things it shouldn’t have been able to. Incredible.