/bin/sh: Delete files with weird characters in Unix

Filed under: Linux, Quick Code — Written by Chrissy on Friday, June 16th, 2006 @ 7:45 pm

I recently used "tar" improperly and inadvertently created a file which seemed near impossible to delete. The file started with two dashes; it was named --exclude.tgz. I issued each of the following commands with no luck:

boudreaux:~ # rm --exclude.tgz
rm: unrecognized option `--exclude'
Try `rm --help' for more information.
 
boudreaux:~ # rm "--exclude.tgz"
rm: unrecognized option `--exclude'
Try `rm --help' for more information.
 
boudreaux:~ # rm "\--exclude.tgz"
rm: unrecognized option `--exclude'
Try `rm --help' for more information.
 
boudreaux:~ # rm *tgz [it was the only tarball in the directory]
rm: unrecognized option `--exclude'
Try `rm --help' for more information.

I was just about fed up so I messaged my friend Larry, a Unix administrator, to ask for help. He initially recommended some of the above techniques but, as you can see, each of them failed. He then gave me this magical command which instantly worked

find . -name \*.tgz -exec rm {} \;

Perfect!

2 Comments   -
  • Comment by krishna | July 6, 2006 @ 5:59 pm

    Hi,
    This way you will remove all tgz found in your dir, including sub dirs!

    Try the full path “rm ./–exclude.tgz”.

    bye

  • Comment by Russell | November 14, 2006 @ 4:41 pm

    Full or partial path works, also the -- option to rm (to end option parsing), like

    rm -- --exclude.tgz

Leave your comment