Skip to main content
deleted 58 characters in body
Source Link
terdon
  • 252.7k
  • 69
  • 481
  • 719

TheYour regular expression doesn't work because -D. gives an error, you don't want or need it thereis not a word character. That said, yesGrep only considers a-z, your regular expression should work. ApparentlyA-Z, it doesn't work with .0-9 for some reason:and _ to be word characters.

$ ls | grep  '\<t'  ## works as expected
test1
test2
$ ls | grep '\<\.' ## no output

In any case, this is really not the right way to do this. First of all, parsing ls is very fragile and almost never a good idea. Here are some other ways of listing directories whose name starts with a .:

find . -type d -name '.*'

Or, if you don't want it to descend into subdirectories (and if you have GNU find, the default on Linux):

find . -maxdepth 1 -type d   -name '.*' 

Alternatively, you could just use echo and a shell glob:

echo .*

That will also show files. To avoid that, use a loop like:

for i in .*; do [ -d "$i" ] && printf '%s\n' "$i"; done

The -D gives an error, you don't want or need it there. That said, yes, your regular expression should work. Apparently, it doesn't work with . for some reason:

$ ls | grep  '\<t'  ## works as expected
test1
test2
$ ls | grep '\<\.' ## no output

In any case, this is really not the right way to do this. First of all, parsing ls is very fragile and almost never a good idea. Here are some other ways of listing directories whose name starts with a .:

find . -type d -name '.*'

Or, if you don't want it to descend into subdirectories (and if you have GNU find, the default on Linux):

find . -maxdepth 1 -type d   -name '.*' 

Alternatively, you could just use echo and a shell glob:

echo .*

That will also show files. To avoid that, use a loop like:

for i in .*; do [ -d "$i" ] && printf '%s\n' "$i"; done

Your regular expression doesn't work because . is not a word character. Grep only considers a-z, A-Z, 0-9 and _ to be word characters.

In any case, this is really not the right way to do this. First of all, parsing ls is very fragile and almost never a good idea. Here are some other ways of listing directories whose name starts with a .:

find . -type d -name '.*'

Or, if you don't want it to descend into subdirectories (and if you have GNU find, the default on Linux):

find . -maxdepth 1 -type d   -name '.*' 

Alternatively, you could just use echo and a shell glob:

echo .*

That will also show files. To avoid that, use a loop like:

for i in .*; do [ -d "$i" ] && printf '%s\n' "$i"; done
Source Link
terdon
  • 252.7k
  • 69
  • 481
  • 719

The -D gives an error, you don't want or need it there. That said, yes, your regular expression should work. Apparently, it doesn't work with . for some reason:

$ ls | grep  '\<t'  ## works as expected
test1
test2
$ ls | grep '\<\.' ## no output

In any case, this is really not the right way to do this. First of all, parsing ls is very fragile and almost never a good idea. Here are some other ways of listing directories whose name starts with a .:

find . -type d -name '.*'

Or, if you don't want it to descend into subdirectories (and if you have GNU find, the default on Linux):

find . -maxdepth 1 -type d   -name '.*' 

Alternatively, you could just use echo and a shell glob:

echo .*

That will also show files. To avoid that, use a loop like:

for i in .*; do [ -d "$i" ] && printf '%s\n' "$i"; done