10

I wanted to list the content of a pwd and display only file starting with dot. I tried ls -a | grep ^\. but I cannot figure out why the output contains also the files which do not start with dot. For example:

Pictures
.pip
.pki
.profile
projects
Public

I know that I can achieve what I want with ls -ld .* I am just curious about this behaviour of grep which I can't explain.

Michael Mrozek
  • 91,316
  • 38
  • 238
  • 232
ps-aux
  • 311
  • 1
  • 3
  • 9
  • See also: [Why do I have to escape a “dot” twice?](http://unix.stackexchange.com/q/144752) – don_crissti Apr 05 '15 at 14:33
  • Why not just `ls -d .*` to list all files starting with `.`? – Barmar Apr 08 '15 at 21:40
  • A cautionary note: Never pipe the output of ls into another command. Use find instead. ls has way too many ideosyncrasies to be trusted. See: Pitfall 1 at http://mywiki.wooledge.org/BashPitfalls - it made #1! The rest of the site is great too. – Joe Apr 11 '15 at 07:35

3 Answers3

20

Quote the argument to grep, thus ls -a | grep '^\.'

The reason for this is that the shell handles \. and turns it back into plain ., which grep then treats as a single-character wildcard. If in doubt, always quote a string that contains (or might contain) a character that's special to the shell.

roaima
  • 107,089
  • 14
  • 139
  • 261
  • Why does shell ignore \? – ps-aux Apr 05 '15 at 11:25
  • \ is an escape sequence which makes the shell to treat the following character as a literal one. In this `grep ^\\.`, \\ is treated by the shell as a single backslash and again the grep treats single backslash followed by the dot as literal dot. – Avinash Raj Apr 05 '15 at 11:28
11

You need to put the grep regex inside quotes.

ls -a | grep '^\.'

Note: Don't parse the output of ls command.

Avinash Raj
  • 3,653
  • 4
  • 20
  • 34
0

If you execute cmd ls -a | grep ^\. then grep do not consider "\" as a special character and meaning of "." do not get escaped.

When we use cmd ls -a | grep "^\." then grep considers "\" as a special character and meaning of "." gets escaped. It will give you a result as expected.

If you want to use grep cmd without quotes then you should escape "\" character also. You can have expected result without double quotes by following command. ls -a | grep ^\\.

For details about meaning of special characters in Regex, refer following link. http://www.regular-expressions.info/quickstart.html

AVJ
  • 505
  • 8
  • 17