[ Table Of Contents ][ Answer Guy Current Index ] greetings   bios   1   2   3   4   5   6   7   8   9   10   11   12 [ Index of Past Answers ]

(?) The Answer Gang (!)

By Jim Dennis, Ben Okopnik, Dan Wilder, Breen, Chris, and the Gang, the Editors of Linux Gazette... and You!
Send questions (or interesting answers) to tag@lists.linuxgazette.net

There is no guarantee that your questions here will ever be answered. Readers at confidential sites must provide permission to publish. However, you can be published anonymously - just let us know!

(?) Dash it All! Coping with ---Unruly--- Filenames

From John Murray

Answered By Jim Dennis, Andrew Higgs, Ben Okopnik

I was experimenting with the TAR command and trying to get it to "exclude" some files. I finally got it to work but in one of my tests I ended up sending the output of the TAR command to a file called -exclude. I want to delete -exclude, but when I type in the command to delete it "rm -f -exclude", the command thinks the file name is an option. The same thing happens when I try to rename the file. Any ideas??

(!) [JimD] This is a classic UNIX FAQ. I pose variations of it to prospective sysadmins and support personnel when I interview them.
(!) [Andrew] Hi John
Try 'rm -f -- -exclude'. The '--' terminates the options list.
Kind regards
(!) [JimD] ./ is more portable (works with all UNIX commands on all versions of UNIX) and is even shorter (saves one space).
The current directory in UNIX (and Linux, of course) is always the "dot" directory. When we use a filename with no slashes (directory/subdirectory delimiters) in it; that's merely a shorthand for "the one in the current directory." So any file foo is also ./foo.
More importantly -foo is the same as ./-foo (Of course it's also the same as $(pwd)/-foo --- the full path to the filename).
NOTE: I give half credit for people who suggest " -- " (a double dash argument to the rm or some other commands; signifying the end of all options processing by that command and thus forcing all following arguments to be taking literally rather than as possible option switches).
(!) [Ben] <laugh> How about for using Midnight Commander? Scroll down to the filename, hit 'F8'. Gone.
(!) [JimD] Half credit. What if mc isn't on this box? How about mc's "undelete" feature? Is that file really gone?
(!) [Ben] Erm... the "undelete" feature is a facet of the ext2 file system; nothing to do with MC itself, which just provides an interface to it. <grin> You'd get a _debit_ for that one.
(!) [JimD] I'll take the hit --- but I'll stick by my guns on the opinion that "rm is forever" (even though I've recovered deleted files using grep, dd and vi). I've read about mc's undelfs before, but I've never used it and I didn't remember the details.
(!) [Ben] "undelfs", despite the confusing name, is no more a filesystem than is their "tarfs": 'foo'fs, in MC parlance, is simply a way of looking at 'foo', where 'foo' is presented as a filesystem - sort of like what the 'mount' command does with a DOS floppy. In the case of "tarfs", it allows you to look at tar files as if they were directories and subdirectories containing files; with "undelfs", it allows you to look at the deleted-but-not-yet-reused filespace in ext2 as if it was a directory. Pretty neat gadget that greatly facilitates the undeletion process; however, the initial read (it would have to scan the entire partition, obviously) takes a while. MC, despite its ease of use, goes very deep; you can actually edit the various 'fs' files, and even create your own!
(!) [JimD] I'll give 3/4 credit for anyone who says: "I just use (x)emacs dired-mode for all my file management, so I'd just C-s (search) to move point to it, and hit the 'd' key."
(!) [Ben] Hey! What if emacs isn't on the box? In my opinion, that's a higher probability than with MC.
(!) [JimD] I'll even give 90% credit for someone who says: "I forgot all my shell tricks since I do everything in perl. So I'd just use: perl -e 'unlink("-foo");' and be done with it"
(!) [Ben] <laugh> That's what I used the last time I had to rename a bunch of MP3s (KOI8-R (Russian) song names on my 'LANG=C' system). Even MC had a problem; Perl chewed it right up. Except in my case, the above would be
perl -we'unlink -foo'
It's not a list and there's no precedence issue (no parens), and there's no interpretation necessary (no double-quotes). Also, enable warnings to tell you if something weird happens.
(!) [JimD] I give extra credit for those who recognize it as an FAQ and laugh at me for posing the question.
I'll give double credit to anyone that recites the FAQ, and ticks off six different ways to do it (--, ./, mc, perl, dired, and one I've never heard of) and tells me that he saw the whole thread on TAG!
(!) [Ben] Edit the inode directly - hexedit, MC, etc. Use C's "int unlink (const char *FILENAME)". Tell an assembler programmer that you've found something that can't be done in it. <evil grin>
(!) [JimD] That would be using fsdb or debugfs and use clri and unlink commands therein.
(!) [Ben] (and, from the "incidental feature" side of the fence...)
Use a large electromagnet. Take a hammer to the hard drive. Take off and nuke the site from orbit (it's the only way to be sure.)
And, as the bonus option, go to Jim Dennis and say, "Jim, I have this beautiful barometer I'll give you if you..." Oh, oops. Wrong story. :)
...Oh. You said "_one_ [way] I've never heard of"? :)
(?) Now, don't you wish I was a hiring manager at someplace you wanted to work?
(!) [Ben] If I wanted to be a sysadmin (which I don't), definitely. Knowing that the people around me are vetted by someone knowledgeable instead of some HR bozo with a checklist <barely managing to supress Major Rant with a sack> would be a Good Thing!
(!) [JimD] I give zilch credit for any efforts to use quotes, backslashes, or other shell "escaping" techniques.
The leading dash is conventionally used by UNIX commands (and specified in the POSIX standards for many of them) as a way to distinguish "object" arguments (things the command will work on) from "options" (or adverbial) arguments (also known as "switches") (which specify how the command will work). Note that this is a convention; and that it applies to commands. The shell (command interpreter) does no special parsing of leading dashes. Quoting and escapingn only affect how the shell parses the command line (how it is passed to the commands) --- so no amount of shell shenanigans will ever overcome a "dashed" filename problem.
(BTW: That's the easiest question on my list of "qualifiers" for sysadmins. No offense intended, but I need people to know the FAQs before I can let them loose with a root prompt on my production systems)
Here's a slightly tougher one: "due to some merger we need to migrate a set of users to a block of (formerly unused) UIDs; why and how? Also what are some 'gotchyas' in this process?" (Preferred answer to "how" is in the form of shell script pseudocode; feel free to gloss over details with comments on how you'd safely figure out implementation specifics).
That's a medium level qualifier.
BTW changing chowning all of the files belonging to just one user is a one-liner; However, it's not as simple as you'd think. Try that for 3/4 credit.
Answer next month; if you remind me! (My rough cut at this is just over 20 lines long).
(!) [JimD] The UNIX FAQ is at:
... and this is question 2.1
Read that before you su again!

This page edited and maintained by the Editors of Linux Gazette Copyright © 2001
Published in issue 68 of Linux Gazette July 2001
HTML script maintained by Heather Stern of Starshine Technical Services, http://www.starshine.org/

[ Table Of Contents ][ Answer Guy Current Index ] greetings   bios   1   2   3   4   5   6   7   8   9   10   11   12 [ Index of Past Answers ]