ls Command – How to Show Only Hidden Files (Dot Files) in ls Alias

aliaslswildcards

I'm using the command

ls -a | grep '^\.'

for showing only the hidden files.
I added the line

alias hidden='ls -a | grep '^\.'' # show only hidden files

to .bash_aliases file

but this does not work. It's probably the problem with ' character.

Could you please help me write the correct alias?

Best Answer

Either make the inner pair of quotes double quotes:

alias hidden='ls -a | grep "^\."'

Or make the outer pair of quotes double quotes:

alias hidden="ls -a | grep '^\.'"

Or make all quotes double quotes and escape the inner pair:

alias hidden="ls -a | grep \"^\.\""

Or make it a function, so you can pass some arguments when calling:

hidden() { ls -a "$@" | grep '^\.'; }