Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Deleting or renaming badly named files UNIX
In the spirit of learning from others' mistakes, I'm sharing the following slightly embarassing story.

In the process of trying to use my web host's Linux OS "tar" command to compress a directory while excluding another, I managed to create a file called "-X". When I went to delete the mistakenly-named file, I quickly found myself stuck. Typing "rm -X" didn't work, because 'rm' interprets the "-" as the sign for a command-line switch, and it doesn't know what to do with "-X" as an option, leading to "unknown option" errors.

Thinking I was smarter than the box, I then tried to use the various UNIX quote characters to 'mark' the hyphen: rm "-X", rm \-X, and rm '-X'. None of these worked; each generated the same error message about unknown options. On my Mac, I would have simply used the GUI and dragged the file to the trash. On the Linux box, though, I was stumped - no GUI available, and a badly named file stuck in my directory.

One amusing ("You won't believe what I've done now...") call to a UNIX-knowledgeable friend provided two options. The first is to use two hyphens to let 'rm' know that there are no command-line switches: rm -- -X. The second is to refer to the file via its relationship to the parent directory: rm ./-X. Either of these will work just fine to delete the file (or you could use "mv" with the same syntax to rename it if you want to keep the file).

At least with OS X we have the option of using the GUI to correct our stupid mistakes! As such, there's an easy way out for X users ... but perhaps this story will save someone some command-line frustration at some point in the future. And please, all you advanced UNIX wizards out there, hold the snickering to a minimum! ;-)
    •    
  • Currently 0.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (0 votes cast)
 
[4,947 views]  

Deleting or renaming badly named files | 2 comments | Create New Account
Click here to return to the 'Deleting or renaming badly named files' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Mac OS X Hints -
Authored by: krreagan on Aug 22, '01 02:23:52PM
Another method is the let 'rm' figure out the file name by using it interactivley. Try "rm -i *" It will prompt you with the file name and you can decline the files you don't want to delete. I discovered this while trying to delete a directory left by somone using my ftp server, they used several unprintable characters as a directory name (such as: FF, LF...). This worked great.

[ Reply to This | # ]
Another approach to the iremoval of 'obnoxious' files (ie names)
Authored by: barrysharp on Dec 27, '01 03:38:37PM
Sometimes files get created with very obnoxious names -- ones that are very difficult protecting from being interpreted as Shell meta characters and/or Shell substitutions. For example (a very simple case), take a file name such as '-lo' and try /bin/rm-ing it. Even protecting it with single quotes or backslash yields annoying difficulties. As original posting states/shows -- it can be removed if you're knowledgeable. Note: I've used "backslash" as I didn't find an obvious way to insert the backslash character without it getting removed ;-) Here's how I deal with (ie deleting) obnoxious file names
> echo xxx  >'-lo''      # Create an obnoxious file with name '-lo'
> ls -i                  # List the files to obtain file '-lo' inode number
This shows the inode number for file '-lo' to be 374807. Now use the find command to remove the obnoxious file
> find . -inum 374807 -print -exec /bin/rm -- {}  "backslash";
./-lo
This process can be made even easier by making a function for the find command. Call the function 'remove' as shown below.
function remove
{
     inode=$1
     find . -inum $inode -print -exec /bin/rm -- {}  "backslash";
     return 0
}
So this can be used as follows remove 374807 Regards... Barry Sharp

[ Reply to This | # ]