Ubuntu – Syntax error while deleting files with `(` character in the name

command line

I'm trying to delete a file using the console with this command

rm ./google-chrome-stable_current_amd64(1).deb

but I get a syntax error. What is wrong with this command?

Best Answer

  • Parentheses are treated specially by the shell. Quote them to remove their special meaning so the shell treats them literally.

    There are three major forms of quoting, and for this purpose any of them will do:

    • Since ( and ) are not the ' character, they're treated literally inside single quotes (so long as the single quotes are not themselves quoted).
    • Since they are not $, `, \, or !, they're treated literally inside double quotes (so long as the double quotes are not themselves quoted).
    • Like any character, they're treated literally after a \ (so long as the backslash is not itself quoted).

    Thus any of the following will work:

    rm ./google-chrome-stable_current_amd64\(1\).deb  # backslashes
    
    rm './google-chrome-stable_current_amd64(1).deb'  # single quotes
    
    rm "./google-chrome-stable_current_amd64(1).deb"  # double quotes
    

    Though less commonly done, you can also use single quotes or double quotes to quote less than the full argument. For example, these also work:

    rm ./google-chrome-stable_current_amd64'(1)'.deb
    
    rm ./google-chrome-stable_current_amd64"(1)".deb
    

    As a separate matter, note that you don't need the leading ./ in the path here, though it's fine to include it. With rm, you'd only need it if the path started with a - character, which rm would attempt to treat as one or more options rather than a path. (Though you could alternatively pass a separate -- argument before the path to indicate that all separate arguments are paths and not options.)

  • Related Question