73

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?

xralf
  • 15,415
  • with examples that are even more complex, you very rarely have to switch from single- to double-quotes. This can be done just by butting the quotes up against one another. For this example, it would be 'ls -a | grep '"'"'^\.'"'"' It looks awful (and for this example it is completely unnecessary because you can just swap out double-quotes for either of the pairs), but on very rare occasions it is useful. You might also want -C flag (force multi-column output). See this image http://imgur.com/a/VIVFP – Dylan Sep 29 '16 at 00:21

13 Answers13

131

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

ls -d .*
33

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 '^\.'; }
manatwork
  • 31,277
12
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 "^\."'
jasonwryan
  • 73,126
4

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 ^\\.'
enzotib
  • 51,661
2

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 "^\."'
rahmu
  • 20,023
  • 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
2

First answer which shows an alias allowing to add a directory path as usual

Try

alias lsh='ls -al --ignore="[^.]*"'

where ls ignores all files and directories which NOT start with a .

Or

alias lsh='ls -Al --ignore="[^.]*"'

to avoid . and .. entries

Then just call

lsh /path/directory/
1

Making it slightly more complicated, but avoiding parsing ls.

llsh () { find "${@:-.}/" -maxdepth 1 -type f -name ".*" -ls; }

lsh () { find "${@:-.}/" -maxdepth 1 -type f -name ".*" -print; }

The two shell function will use find to generate a list of all hidden regular files in the current directory, or in the directory given on the command line.

The llsh function will generate a "long listing" which will be only slightly more verbose than ls -l, while lsh generates a single-column listing like ls -1.

Kusalananda
  • 333,661
1

Here's a function version of this instead of an alias. It does rely on extended glob, which can be turned on with shopt -s extglob. This allows for SHELL pattern matching, which in this implementation prevents . and .. from being returned as entries.

hidden() {
    if [[ $# -eq 1 ]]; then
        exec 3>&1
        _hidden=$(cd "${1}"; ls -d .!(|.)?* 1>&3)
        unset _hidden
        exec 3>&-
    elif [[ $# -gt 1 ]]; then
        exec 3>&1
        for i in $@; do
            _hidden=$(cd "${i}"; echo "${i}:" 1>&3; ls -d .!(|.)* 1>&3)
        done
        unset _hidden i
        exec 3>&-
    else
        ls -d .!(|.)*
    fi
}

It cds into the desired directory in a subshell, and redirects the output to the current shell. This stops the path of the provided directory being written preceding each entry. It should behave very similarly to ls when given directory arguments.

If you want to show . and .., just change the three ls commands in the function to ls -d .* which is also not dependant on extended glob. You can also add other arguments to this command, like --color=auto. The function itself will not accept these option arguments. If options are already included in an alias redefining ls, the function will use the alias and therefore implement those options.

0

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.
waltinator
  • 4,865
0
find . -type f -name ".*"

OR

find . -type f -name ".*" | while read file; do basename $file; done
kemiko
  • 1
  • Hi! You could improve your answer by explaining in detail how it works, and also by mentioning why, in your opinion, it answers the question. – dhag May 05 '17 at 00:25
0
# add it to ~/.bashrc

    hidden() {  # lists hidden files and directories
        find . -maxdepth 1 -name '.?*' -exec ls --color -d {} \;
        }

    hiddenfiles() { # lists hidden files
        find . -maxdepth 1 -type f -name '.?*' -exec ls --color -d {} \;
        }

    hiddendirs() { # lists directories
        find . -maxdepth 1 -type d -name '.?*' -exec ls --color -d {} \;
        }
Akhil
  • 1,290
-3

Just simply type the below command and dont confuse with above explained stuff.

ls -A -1 -d -F .* | egrep -v '/$'
Archemar
  • 31,554
  • I think the OP asked for how to write an alias properly. – countermode Sep 21 '16 at 13:24
  • 1
    That's just a copy of waltinator's comment on his/her answer. For this answer to be useful, you need to explain how and why those particular ls flags and the egrep command answer the question. – Anthony Geoghegan Sep 21 '16 at 13:46
-3
ls -lart | sed -n "/ \.[A-Za-z0-9][A-Za-z0-9]*/p"
  • 2
    Can you please clarify your answer, one line of code is not counted as good answer – Romeo Ninov May 22 '17 at 05:49
  • as per requirement he need to list only hidden files. so i have list all files including hidden files by ls -lart. then i have use regex to show that files which is start with dot(.) . I have got outpout like this. – shailesh Chanderiya Aug 30 '17 at 03:12
  • [g906016@hklu2574881:[PTA] ~]$ ls -lart | sed -n "/ .[A-Za-z0-9][A-Za-z0-9]*/p" -rw-r--r-- 1 g906016 g906016 658 Dec 29 2016 .zshrc -rw-r--r-- 1 g906016 g906016 171 Dec 29 2016 .kshrc -rw------- 1 g906016 g906016 27 Dec 29 2016 .k5login -rw-r--r-- 1 g906016 g906016 176 Dec 29 2016 .bash_profile -rw-r--r-- 1 g906016 g906016 18 Dec 29 2016 .bash_logout -rwxr-xr-x 1 g906016 g906016 0 Dec 29 2016 .dir_colors drwxr-x--- 3 g906016 g906016 4096 Dec 29 2016 .subversion – shailesh Chanderiya Aug 30 '17 at 03:12