28 Welcome, New User! Then there’s the story of the poor student who happened to have a file called “-r” in his home directory. As he wanted to remove all his non directory files (I presume) he typed: % rm * And yes, it does remove everything except the beloved “-r” file… Luckily our backup system was fairly good. Some Unix victims turn this filename-as-switch bug into a “feature” by keeping a file named “-i” in their directories. Type “rm *” and the shell will expand this to “rm -i filenamelist” which will, presumably, ask for confirmation before deleting each file. Not a bad solution, that, as long as you don’t mind putting a file named “-i” in every directory. Perhaps we should modify the mkdir command so that the “-i” file gets created auto- matically. Then we could modify the ls command not to show it. Impossible Filenames We’ve known several people who have made a typo while renaming a file that resulted in a filename that began with a dash: % mv file1 -file2 Now just try to name it back: % mv -file2 file1 usage: mv [-if] f1 f2 or mv [-if] f1 ... fn d1 (‘fn’ is a file or directory) % The filename does not cause a problem with other Unix commands because there’s little consistency among Unix commands. For example, the file- name “-file2” is kosher to Unix’s “standard text editor,” ed. This example works just fine: % ed -file2 4347 But even if you save the file under a different name, or decide to give up on the file entirely and want nothing more than to delete it, your quandary remains: 7The “434” on the line after the word “ed” means that the file contains 434 bytes. The ed editor does not have a prompt.
Consistently Inconsistent 29 % rm -file usage: rm [-rif] file ... % rm ?file usage: rm [-rif] file ... % rm ????? usage: rm [-rif] file ... % rm *file2 usage: rm [-rif] file ... % rm interprets the file’s first character (the dash) as a command-line option then it complains that the characters “l” and “e” are not valid options. Doesn’t it seem a little crazy that a filename beginning with a hypen, espe- cially when that dash is the result of a wildcard match, is treated as an option list? Unix provides two independent and incompatible hack-arounds for elimi- nating the errantly named file: % rm - -file and: % rm ./-file The man page for rm states that a lone hypen between the rm command and its first filename tells rm to treat all further hypens as filenames, and not options. For some unknown reason, the usage statements for both rm and its cousin mv fail to list this “feature.” Of course, using dashes to indicate “please ignore all following dashes” is not a universal convention, since command interpretation is done by each program for itself without the aid of a standard library. Programs like tar use a dash to mean standard input or standard output. Other programs sim- ply ignore it: % touch -file touch: bad option -i % touch - -file touch: bad option -i Amuse Your Friends! Confound Your Enemies! Frequently, Unix commands give results that seem to make sense: it’s only when you try to apply them that you realize how nonsensical they actually are:
Previous Page Next Page