Unix/MacOS: “rm: illegal option — b” or how to remove files that have their name starting with a minus sign.
Posted by jpluimers on 2026/03/11
I had to remove all text files including a -bar.txt from the current directory using bash, so I automatically typed rm *txt resulting in this nice error:
rm: illegal option -- b usage: rm [-f | -i] [-dPRrvW] file ... unlink file
When there was just a file named -foo.txt in the directory, the error became more interesting:
rm: illegal option -- o usage: rm [-f | -i] [-dPRrvW] file ... unlink file
Then it struck me: rm is one of those old tools where you can smack all options together. Since *txt basically space-separates all filenames ending with txt together on one command-line and the minus (-) sorts very early (no matter ASCII or Unicode or most other encodings) resulting in rm guessing the first file were in fact options.
My go to solution for that is to prepend ./ and indeed, rm ./*txt worked perfectly fine.
[Wayback/Archive] Unix Admin – View topic – Removing file named -abc proposed more solutions that likely work on Unix/Linux:
rm - -abc or rm -- -abc
The first one didn’t work on MacOS though as I got this result:
$ rm - *txt rm: -: No such file or directory
The second one worked fine: rm -- *txt.
Which means I learned a new trick.
Query: [Wayback/Archive] “rm: illegal option — b” – Google Search
–jeroen






Leave a comment