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

Deleting or renaming badly named files revisited UNIX
Ever had a file with a name like -myfile and you can't get rid of it? When you try to run a command on it such as rm -, Unix interprets the "-" as an option or other things. You can't do it!

Here's a solution - although I can not take credit for this, I did modify it a bit. This tip appeared in today's Unix Guru Universe Tips mailing list (a great mailing list of a single tip each day ... I have learned LOTS just from this list).

Read the rest of the article for the details...

[Editor's note: We have another hint that deals with deleting badly named files, but this method seemed unique enough to merit a hint of its own...]

First change dir to the location of the poorly named file.
cd /dir/where/filename/is/located
Then look for the file's "inode" number - the inode is the unique numeric identifier for this file:
ls -i filename
You will get back a line that looks like this:
296053 filename
The inode number is shown in the left column. Now use the "find" cmd plus the inode number to locate the file and rename it to something more UNIX friendly that you can then delete:
find . -inum 296053 -exec mv {} ByeBye ;
In this example, I have located the file and renamed it to "ByeBye", which I can then easily delete with rm. If you are feeling a little brave you can go ahead and tack on the "rm" statement right to the end of the "find" cmd and do it all it one motion:
find . -inum 296053 -exec mv {} ByeBye ; -exec rm ByeBye ;
This finds the file, renames the file, and then deletes the renamed file. Note: The spaces in the commands above are required.
    •    
  • Currently 0.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (0 votes cast)
 
[7,160 views]  

Deleting or renaming badly named files revisited | 11 comments | Create New Account
Click here to return to the 'Deleting or renaming badly named files revisited' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Easier ways
Authored by: gidds on Jul 31, '02 09:30:54AM
That's a very convoluted way of doing it.
For the specific case of a leading hyphen, as has been said elsewhere, most Unix commands have a neat way around it: if you give -- as a parameter, then all following parameters are taken as filenames and not as parameters.  So
rm -- -myfile
does the trick very easily, as does
rm ./-myfile
For other awkward or unprintable characters, it's often easier to identify the file using the filename completion features built into the shell.  If your file is called myfile^?, say, and it's the only one beginning with myfile, then
rm myfile*
could hardly be easier.  If the file is harder to separate out, you can use characters after the *, or instead use one or more of the ? wildcard which matches a single character.  You can ensure you're only deleting the right file by trying
ls (whatever)
before doing the
rm (whatever)


[ Reply to This | # ]
Easier ways
Authored by: macsforever2000 on Jul 31, '02 11:24:57AM

My favorite way is to use the -i modifier and a name close to the desired one.

In this case:

rm -i *myfile

Then is goes through all matching files and asks if you want to delete them.



[ Reply to This | # ]
Easier ways
Authored by: ber on Jul 31, '02 04:49:32PM

-i is useful but needs to be used with gidd's suggestions or you fall into the same trap.

sh-2.05a$ rm -i -x
rm: illegal option -- x
usage: rm [-f | -i] [-dPRrvW] file ...
unlink file
sh-2.05a$ rm -i ./-x
remove ./-x?



[ Reply to This | # ]
Easier ways
Authored by: DavidRavenMoon on Jul 31, '02 06:57:46PM

Or ... just drag it to the Trash! (Cmd-Del) How novel! ;)

[ Reply to This | # ]

Easier ways
Authored by: bluehz on Jul 31, '02 11:42:53PM

Thnx Gidds - that is much easier - plus the code above got mucked up on its way to the posting....



[ Reply to This | # ]
or use quote
Authored by: SOX on Jul 31, '02 03:52:25PM

just quote the file name



[ Reply to This | # ]
or use quote - not
Authored by: ber on Jul 31, '02 04:53:01PM

Quoting doesn't cut it. Use gidd's suggestions...

sh-2.05a$ rm "-x"
rm: illegal option -- x
usage: rm [-f | -i] [-dPRrvW] file ...
unlink file
sh-2.05a$ rm '-x'
rm: illegal option -- x
usage: rm [-f | -i] [-dPRrvW] file ...
unlink file
sh-2.05a$ rm \-x
rm: illegal option -- x
usage: rm [-f | -i] [-dPRrvW] file ...
unlink file



[ Reply to This | # ]
or use quote - not
Authored by: snit on Jul 31, '02 05:59:10PM
works fine for me:

[localhost:~/Desktop] user% rm -- -x
remove -x? y
[localhost:~/Desktop] user%


I think you left used the name x instead of -x in your example.



[ Reply to This | # ]
inodes
Authored by: koen on Aug 02, '02 12:27:46PM

Hi,

Would somebody be so kind to explain what the inode files are? Sometimes they are several MB big on my harddrive. Are they there for a special purpose? I am just worried that the number of inodes is growing steadily, and thus filling up my HD.

thanks,

- Koen.



[ Reply to This | # ]
What about...
Authored by: Rosyna on Aug 04, '02 06:45:32PM

What about those files with odd characters at the beginning (or throughout) their name? Like option 8 option 8 option 8? or those files with returns at the beginning of their names? or those with nothing but japanese characters in their names?



[ Reply to This | # ]
Deleting files?
Authored by: irishscott on Aug 08, '02 02:00:49AM

I recently installed XFree86 and WindowMaker in an attempt to familiarize myself with Darwin and its power. Once that process was complete, I downloaded my first binary file through Fink (a space invaders type game). What can I say, I like the classics! Needless to say, I can't get the game to run and would like to delete it. It is located in a folder called SW. When I find the files (Sherlock) and try to put them in the trash, I get a message stating they can only be deleted by the "root". Not being shy, I log-in as "root" (sudo -s) and try to change to the SW folder. Only, it is nowhere to be found. My question is how do I see hidden folders? Once I can find the folder I am sure I can remove the files (rm command).



[ Reply to This | # ]