How do you remove a file with a space in it? The normal way of invoking rmthe command, followed by the filenamewon’t work because rm thinks you’re talking about two different files. Actually, removing Cousin Harold’s picture isn’t too hard. Just put the name of the file in quotation marks.
$ ls cousin harold.jpg -cousin_roy.jpg cousin_beth.jpg $ rm cousin harold.jpg rm: cannot remove 'cousin': No such file or directory rm: cannot remove 'harold.jpg': No such file or directory $ rm "cousin harold.jpg" $ ls -cousin_roy.jpg cousin_beth.jpg
Here’s more of a head-scratcher: How do you remove a file whose name starts with -?
$ ls -cousin_roy.jpg cousin_beth.jpg $ rm -cousin_roy.jpg rm: invalid option -- c Try 'rm --help' for more information.
D’oh! The rm command sees the - and thinks it’s the start of an option, but it doesn’t recognize an option that starts with c. It continues with ousin_roy.jpg and doesn’t know what to do.
You have two solutions. You can preface the problematic filename with --, which indicates to the command that anything coming afterward should not be taken as an option, and is instead a file or folder.
$ ls -cousin_roy.jpg cousin_beth.jpg $ rm -- -cousin_roy.jpg $ ls cousin_beth.jpg
Otherwise, you can use the . as part of your pathname, thus bypassing the space before the - that confuses the rm command and leads it into thinking the filename is actually an option.
$ ls -cousin_roy.jpg cousin_beth.jpg $ rm ./-cousin_roy.jpg $ ls cousin_beth.jpg
It just goes to show that the ingenuity of Linux users runs deep. That, and try not to put a hyphen at the beginning of your filename!