Delete Troublesome Files

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!

Advertisements
Posted in Linux
2 comments on “Delete Troublesome Files
  1. GabrielNar says:

    I had the similar problem and tried Long Path Tool, available at
    http://www.pathtoodeep.com/. It helped me to solve this problem easily.
    Try it, and you will definitely love this tool. Its free.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: