Search code examples
bashls

how to escape parenthesis in ls commands


I do not succed to filter with ls files with parenthesis (on bash)

$ ls -1
a_échéancier(1).pdf
a_échéancier(2).pdf
a_échéancier(3).pdf
a_échéancier(4).pdf
a_échéancier(5).pdf
a_échéancier(6).pdf
a_échéancier.pdf
$

A try here:

$ ls "*).pdf"
ls: cannot access '*).pdf': No such file or directory
$
$ ls '*\).pdf'
ls: cannot access '*\).pdf': No such file or directory
$

Solution

  • You are escaping too many characters; the only character that needs to be escaped is ):

    ls *\).pdf
    

    but everything else except the * can be escaped:

    ls *").pdf"
    

    The shell itself is what expands the glob before ls even runs; ls just gets an explicit list of filenames. Quoting the * makes ls try to list the single file named *).pdf, not every file in the current directory that matches the pattern.