Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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 (and possible rename the title for this question)?

share|improve this question
add comment

6 Answers

up vote 9 down vote accepted

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 '^\.'; }
share|improve this answer
add comment

You can use double quotes:

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

or concatenate more single quoted strings

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

or remove at all internal quotes

alias hidden='ls -a | grep ^\\.'
share|improve this answer
add comment

For the record this doesn't seem to work with me, since ls -a prints two (sometimes more columns). I would recommend using the -1 option to make sure every file is in its own line. Something like this:

alias hidden='ls -a1 | grep "^\."'
share|improve this answer
    
I just tried ls -a | cat it still outputs on 2 columns. I should mention I'm using Solaris and not the GNU ls. Maybe that's why. In all cases your -1 solution worked perfectly and is much more elegant than my hackish workaround. I'm updating my answer. –  rahmu Sep 28 '11 at 18:53
    
I probably missed this discussion, but look here if something will go wrong. –  xralf Sep 28 '11 at 19:31
add comment

Have the shell list the dot files, and tell ls not to see through directories:

ls -d .*
share|improve this answer
add comment

Does your ls support -A? From man ls:

    -a, --all
          do not ignore entries starting with .

   -A, --almost-all
          do not list implied . and ..

$ ls --version
ls (GNU coreutils) 8.5
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  
Written by Richard M. Stallman and David MacKenzie.
share|improve this answer
    
Yes, it does, but I wanted only hidden files. –  xralf Sep 29 '11 at 6:44
    
ls -A -1 -d -F .* | egrep -v '/$' –  waltinator Sep 30 '11 at 21:01
add comment
ls -Ad .* #This will list all the hidden files & directories while retaining the color & formatting

OR

To create an alias of the same:

alias lh='ls -Ad .*'

OR

Same thing could be done via grep command and pipe operator; however it would loose the color and formatting:

ls -a|grep "^\." 

OR

Via alias:

alias lh='ls -a|grep "^\."'
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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