Let's say i had a folder named * under /. I know that common commands like rm -rf * won't work. Any help?

  • 2
    Technically, rm * would work to delete that folder. Sep 23 '12 at 23:29

use this short command:

rm /\*
  • 1
    That won't work - it's a directory.
    – naught101
    Sep 24 '12 at 0:39
  • 1
    Then use 'rmdir /\*' instead. The other option is to use 'rm -rf /\*'. But do the latter one is dangerous if you do a mistake.
    – Serge
    Sep 24 '12 at 11:44

You can single-quote arguments to prevent processing by the shell.

rm -r '/*'

Test it safely using ls (in folders containing files):

$ ls '*'
ls: *: No such file or directory

In this case, double quotes would work as well, but if there'd be an $ involved, they wouldn't, as the shell would assume it's a variable:

$ ls "foo$bar"
ls: foo: No such file or directory
$ ls 'foo$bar'
ls: foo$bar: No such file or directory

For GNU rm, you can also add -- before the file name arguments to prevent them from getting parsed as arguments. This'll allow you to delete files named -rf without problems.

  • Not required. You can simply escape the '*' so it isn't expanded by the shell as Serge answered.
    – darnir
    Sep 23 '12 at 15:21
  • 5
    @darnir You don't need to use it if you don't like it. But you have to know which characters to escape when using Serge's approach (consider &), and escape them individually, which can easily become more effort than quoting once. I therefore consider individual escaping inferior and posted this alternative.
    – Daniel Beck
    Sep 23 '12 at 15:26
  • 3
    @Serge I'm not saying your answer is bad. It gets the job done, and was accepted. However, if there are multiple (reasonably different) ways to achieve something with different pros and cons, feel free to list them all. They might not work in all situations, so it's good to have alternatives to choose from. Please also note that users with similar (not identical) issues might arrive at a question from Google. It's also a good idea to broaden the scope if it leads to questions that can help more people. Writing broader scoped answers is a start there.
    – Daniel Beck
    Sep 23 '12 at 20:37
  • 1
    I had no aim to get all my answers accepted and with the highest sore. I spend couple of days (this weekend) with some of these sites. The first impression was that I can share with people my knowledge, this is why I started to provide answers. On SO I got 760 points in these three days. Yes, some my answers was not of hi-end quality from different points. But all of them were correct. But now I see that most people who put questions here - are schoolars who don't want to spend a second to find a solution themselves. What for I will spend my time?
    – Serge
    Sep 23 '12 at 20:46
  • 1
    @Serge Not sure what your comments are about. From what I saw after you mentioned it, you had an impressive start to contributing to the sites. If there's something I can help you with in using the sites, or something else you want to discuss, feel free to ping me on chat. I'm in UTC+2 though, so you might not get a reply very soon. Regarding "beginner" questions: Keep in mind that some information is not easily discoverable. Reading the full bash man page takes a while if you don't know what to search for.
    – Daniel Beck
    Sep 23 '12 at 20:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.