2

I'm trying to display hidden files in the current directory using grep and ls.

The following command works fine:

ls -a | egrep '^\.+',

but this commands don't work:

ls -al | egrep '\<\.+' or ls -al | egrep '\<[.]+'.

Does anyone know why the symbol \< doesn't work with a period (.)? Btw. I'm using Ubuntu 18.04.

  • 2
    What do you expect \< to do? – Eduardo Trápani Nov 18 '19 at 14:23
  • I think, \< should match the regex to the beginning of the word. – errorOK Nov 18 '19 at 14:30
  • 2
    on GNU grep, \< will match the start of a word, since . is not a word character, it will not match in this case... if you want the opposite of word boundary, you can use \B but that will also depend on what character is present before the . – Sundeep Nov 18 '19 at 14:30
  • @Sundeep, thanks for the explonation. But now, how to display the file or word with only one . at the beginning? – errorOK Nov 18 '19 at 14:47
  • 3
    I'd rather use ls .[!.]* instead of piping to grep, see glob – Sundeep Nov 18 '19 at 15:03
2

The anchor to the beginning is the caret, so this should work:

ls -a | grep '^\.'
  • Yes, it works and I mentioned it. – errorOK Nov 19 '19 at 15:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.