How to delete files with special characters on Linux?

After more than 8 years using Linux day-by-day, sometimes I face some questions and problems that are completely new for me (I think this is the reason why I love Linux :P).

So, I was beginning a new Ruby on Rails project today and, since I was only interest in a RESTFul API, I decided to use the new Rails-API, which was integrated with the newest version of Rails gem.

To create a Rest API, the following command does the trick:

rails new my_awesome_app --api

However, after running the command above, I realised that the Rails gem version installed on my host wasn’t new enough to recognise the - -api option.

So, the result was a new ordinary Rails project saved in a directory named - -api.

The Problem

How to delete this little bastard?

Running…

$ rm -rf --api
$ rm -rf '--api'
$ ls -1 | grep 'api' | xargs rm -rf # This one is the real face of desperation :P

… results in the same error: - -api option is not recognized.

The solution

After some research, I found a feature, that’s available in many other Linux commands, not only rm, but was completely new for me.

Putting a - - in any part of the command says to the interpreter “Stop parsing option from now on”

So, if I run…

$ rm -- --foo --bar

… it won’t recognize - -foo and - -bar as flags to rm command, but instead will treat them as regular parameters to command.

So, to solve our original problem, the command below is all we need:

$ rm -rf -- --api

Liked this post?

Have questions? Suggestions?

Follow me on Twitter and let's engage in a conversation