skip to Main Content

Running rm projects/artwork/My Project (543893)/Images/*.png at the command line in Debian does not work because of the spaces and the parenthesis. I need to escape them. But I thought quotation marks “” were an alternative to escaping. It works with other commands, such as cd, but not with rm. Why is that?

2

Answers


  1. Because globbing does not work with quoting:

    $ ll /tmp/"d i r"/*.png
    -rw-r--r--. 1 postgres postgres 0 May 26 14:02 /tmp/d i r/a.png
    -rw-r--r--. 1 postgres postgres 0 May 26 14:02 /tmp/d i r/p.png
    $ ll "/tmp/d i r/*.png"
    ls: cannot access /tmp/d i r/*.png: No such file or directory
    $ rm "/tmp/d i r/*.png"
    rm: cannot remove ‘/tmp/d i r/*.png’: No such file or directory
    $ rm /tmp/"d i r"/*.png
    $ ll /tmp/"d i r"/*.png
    ls: cannot access /tmp/d i r/*.png: No such file or directory
    
    Login or Signup to reply.
  2. You should ensure that:

    • the spaces and parentheses are inside the quotation marks, so that they are treated as literals, because otherwise these characters will have special meanings for the shell (spaces to separate command-line arguments, and parentheses for a subshell command)

    • but for the * you want the exact opposite: it must be outside the quotation marks so that it is indeed given its special meaning as a wildcard, not a literal *.

    Remember that you do not need to apply quotation marks to the whole string, only the part that needs treating as a literal and otherwise would be given a special meaning by the shell.

    So for example you could use:

    rm projects/artwork/"My Project (543893)"/Images/*.png
    

    Similarly, if you use escapes, escape the spaces and parentheses, but not the wildcard:

    rm projects/artwork/My Project (543893)/Images/*.png
    

    (In other words, you would not use *.)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search