/bin/sh: Delete files with weird characters in Unix
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 {} \;
Oh, years later, I found this to work, too: rm `find .|grep exclude`
Perfect!