58

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?

1
  • 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 imgur.com/a/VIVFP – Dylan Sep 29 '16 at 0:21

12 Answers 12

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 '^\.'; }
0
114

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

ls -d .*
5
  • 7
    This has the advantage of allowing multi-column listing, unlike the grep-based approach. – anol Oct 12 '14 at 13:17
  • However, shows also directories (as mentioned). Any way to keep it simple as is and show only files? – honzajde Sep 10 '16 at 9:47
  • @honzajde The question didn't ask to exclude directories. If you want to exclude directories, you can use ls -d .* | grep -v '^d' or (zsh only) print -lr .*(.) – Gilles 'SO- stop being evil' Sep 10 '16 at 21:39
  • this not work for me, on Ubuntu 16.04 – Andrew_1510 Oct 8 '17 at 8:41
  • This has the advantage of showing directories as well – young_souvlaki Aug 23 '20 at 22:07
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 "^\."'
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 ^\\.'
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 "^\."'
2
  • 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
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.

1

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/
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.
2
  • 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
0
find . -type f -name ".*"

OR

find . -type f -name ".*" | while read file; do basename $file; done
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 5 '17 at 0: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 {} \;
        }
-3

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

ls -A -1 -d -F .* | egrep -v '/$'
2
  • 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"
3
  • 2
    Can you please clarify your answer, one line of code is not counted as good answer – Romeo Ninov May 22 '17 at 5: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 3: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 3:12

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.