I have a directory that contains thousands of files, some of them are hidden.

The command ls -a list all files, including hidden ones, but I need just to list hidden files.

What command should I use?

  • A simple ls -ld .* or ls -ald .* will work – John Strood May 1 at 15:11

12 Answers 12

up vote 143 down vote accepted

The command :

ls -ld .?* 

Will only list hidden files .

Explain :

 -l     use a long listing format

 -d, --directory
              list  directory entries instead of contents, and do not derefer‐
              ence symbolic links

.?* will only state hidden files 
  • 5
    You don't need the two question marks in there, the * covers it ( ? only matches any single character, * matches any number of them ). – psusi May 19 '14 at 0:50
  • 10
    @psusi, I think the intent is to exclude . and .. from the match. However it will also exclude (perfectly legal) single-character hidden filenames such as .a, .1 and so on. Perhaps a better extended glob would be .!(|.) i.e. literal dot followed by anything except nothing or another (single) dot i.e. ls -d .!(|.) – steeldriver May 19 '14 at 1:25
  • @steeldriver, neat, the ?? version does exclude "." and "..". This seems to be the result of an interesting quirk: neither ? nor * will match a dot, but the ? must match something otherwise the name is ignored. – psusi May 19 '14 at 3:37
  • To identify directories and files add the F option, i.e., ls -ldF .?* directory names have "/" as last displayed character files don't. – RCF May 19 '14 at 21:30
  • This almost works except it also list hidden folder like .vim, which I consider not a file here. I modify it a bit like ls -ldp .?* | grep -v / This only list hidden files not hidden folder – Fred Yang Jan 26 at 5:33
ls -d .!(|.)

Does exactly what OP is looking for .

  • How does that work? – SarcasticSully Jun 7 at 17:56
  • list directory anything with a . and do not list anything without one is what it pretty much translates into. – patrick Jun 12 at 15:22
  • I understand that in !(pattern-list), pattern-list is a list of one or more patterns separated by a |, but it is confusing for me that you don't have any pattern to the left of |. Could you please explain that syntax to me? – Carlos Mendoza Sep 18 at 13:35
  • 1
    @CarlosMendoza that would become the empty pattern, so this is like . not followed by (nothing or .), excluding . itself and ... – muru Sep 26 at 2:27

If you just want the files in your current directory (no recursion), you could do

echo .[^.]*

That will print the names of all files whose name starts with a . and is followed by one or more non-dot characters. Note that this will fail for files whose name starts with consecutive dots, so for example ....foo will not be shown.

You could also use find:

find -mindepth 1 -prune -name '.*'

The -mindepth ensures we don't match . and the -prune means that find won't descend into subdirectories.

ls -ad .*

works for me in Bash.

  • Shows . and .. in Bash – Penghe Geng Aug 29 at 14:09

Using find and awk,

find . -type f | awk -F"/" '$NF ~ /^\..*$/ {print $NF}'

Explanation:

find . -type f --> List all the files in the current directory along with it's path like,

./foo.html
./bar.html
./.foo1

awk -F"/" '$NF ~ /^\..*$/ {print $NF}'

/ as field separator awk checks for the last field staring with a dot or not. If it starts with a dot, then it prints the last field of that corresponding line.

  • You could just use find -type f. You don't need to explicitly set the search path or -name "*". – terdon May 19 '14 at 3:08

find is usually a better option for complicated searches than using name globbing.

find . -mindepth 1 -maxdepth 1 -name '.*'

or

find . -mindepth 1 -maxdepth 1 -name '.*' -o -name '*~'

find . searches current directory

-mindepth 1 excludes . and .. from the list

-maxdepth 1 limits the search to the current directory

-name '.*' find file names that start with a dot

-o or

-name '*~' find file names that end with a tilde (usually, these are backup files from text editing programs)

However, this and all of the other answers miss files that are in the current directory's .hidden file. If you are writing a script, then these lines will read the .hidden file and display the file names of those that exist.

if [[ -f .hidden]] # if '.hidden' exists and is a file
then
    while read filename # read file name from line
    do
        if [[ -e "$filename" ]] # if the read file name exists
        then
            echo "$filename" # print it
        fi
    done < .hidden # read from .hidden file
fi
  • What's the .hidden file? Why would there ever be a file called .hidden that contains the file names? Anyway, if there is one why would you do something that complex when all you would need would be cat .hidden? Your find command is correct(ish) but the -name '*~' is irrelevant. Files that end in tildes are backup files but not hidden in any way. – terdon May 21 '14 at 3:19
  • @terdon The .hidden file is for files and folders you want to hide when you can't change the file/folder name to start with a dot. As for files that end in tildes, it depends on the system. ls -B will ignore such files, as will most GUI file explorers. – Mark H May 2 '16 at 21:31
  • cat .hidden may show files that no longer exist if those files were deleted or moved since being added to the .hidden file. – Mark H May 2 '16 at 21:35
  • This works well. – Penghe Geng Aug 29 at 14:10

I think that you can do it with following command.

ls -a | grep "^\." | grep -v "^\.$" | grep -v "^\..$"

ls -a command you entered, that shows all files and directories in current working directory.

grep "^\." command I appended, that filters output to shows only hidden files(It's name starts with ".").

grep -v "^\.$" | grep -v "^\..$" command I appended, that filters output to exclude ., ..(They are current and parent directory).

If some filenames can have more than a line with "\n", above example could be incorrect.

So I suggest following command to solve it issue.

find -maxdepth 1 -name ".[!.]*"

What else you could have done, is ls .?* Or ls .!(|) that will show you everything in the current dir hidden files/dirs on the top and other files/dirs below

e.g: from my terminal

$ ls .?*       
.bash_history    .dmrc        .macromedia   .weather
.bash_logout     .gksu.lock   .profile      .wgetrc
.bash_profile    .bashrc.save .ICEauthority .toprc           .Xauthority
.bashrc          .lastdir     .viminfo      .xsession-errors
.bashrc~         .dircolors   .lynxrc       .vimrc           .xsession-errors.old

..:
Baron

.adobe:
Flash_Player

.aptitude:
cache  config

.cache:
compizconfig-1                              rhythmbox
dconf                                       shotwell

Now notice in the above results, it shows you every file/dir with its subdir and any hidden files right below.

[1:0:248][ebaron@37signals:pts/4][~/Desktop]
$ ls .!(|)
.bash_aliases  .bashrc1  .bashrc1~

..:
askapache-bash-profile.txt  examples.desktop             Public           top-1m.csv
backups             Firefox_wallpaper.png        PycharmProjects          top-1m.csv.zip
Desktop             java_error_in_PYCHARM_17581.log  Shotwell Import Log.txt  topsites.txt
Documents           Music                Templates            Videos
Downloads           Pictures                 texput.log           vmware

Sorry, I cannot comment. to explain the difference here between ls .?* and @cioby23 answer ls -d .[!.]* .??* And why it is actually printing hidden files twice is because literally you're asking twice .??*, .?*, .[!.]* they're the same thing, so adding any of them with different command characters will print twice.

You can also use:

ls -d .[!.]* .??*

This will allow you to display normal hidden files and hidden files which begin with 2 or 3 dots for example : ..hidden_file

  • 1
    With this I get all hidden files twice. – TuKsn May 19 '14 at 9:02

you can use the command

ls -Ad .??*

This has the advantage of allowing multi-column listing, unlike the grep-based approach in the ls -a | grep "^\." solutions

All the answers so far are based on the fact that files (or directories) which names start with a dot are "hidden". I came up with another solution, that might not be as efficient, but this solution does not assume anything about the names of the hidden files, and therefore avoids listing .. in the result (as does the currently accepted answer).

The full command is:

ls -d $(echo -e "$(\ls)\n$(\ls -A)" | sort | uniq -u)

Explanation

What this does is list all the files (and directories) twice,

echo -e "$(\ls)\n$(\ls -A)"

but only showing hidden files once -A.

Then the list is sorted | sort which makes regular (unhidden) files appear twice and next to each other.

Then, remove all lines that appear more than once | uniq -u, only leaving unique lines.

Finally use ls again to list all the files with the user's custom options and without listing the contents of the directories in the list -d.

EDIT (Limitations):

As muru pointed out, this solution will not work correctly if there are files with names such as escaped\ncharacter.txt because echo -e will split the filename into two lines. It will also not work as expected if there are two hidden files with almost the same name except for a special character, such as .foo[tab].txt and .foo[new-line].txt as both of those are printed as .foo?.txt and would be eliminated by uniq -u

  • 1
    I think that's pretty much the definition of hidden files. Even the origin of these hidden files is from a bug in dealing with . as the first character. – muru Sep 26 at 2:05
  • I mention that (as fact) in the first paragraph. It still however gets a different result than the current accepted answer – alejandro Sep 26 at 2:13
  • Sure, it excludes .., but I wonder how well it works with filenames containing special characters. – muru Sep 26 at 2:15
  • I don't know exactly what you mean by special characters, but I would think it would not pose a problem, as this solution does not assume anything about the filenames – alejandro Sep 26 at 2:21
  • Actually, you do assume at least one thing: filenames don't have newlines in them. Also: filenames don't have escape sequences in them that echo -e will interpret. Also: ls won't mangle filenames so that two different filenames get displayed the same (for example, in some versions ls will use ? for special characters, so .foo\tbar (\t => tab) and .foo\nbar (\n => newline) will both show up as foo?bar). – muru Sep 26 at 2:25

With bash, setting the GLOBIGNORE special variable is some non-empty value is enough to make it ignore . and .. when expanding globs. From the Bash docs:

The GLOBIGNORE shell variable may be used to restrict the set of filenames matching a pattern. If GLOBIGNORE is set, each matching filename that also matches one of the patterns in GLOBIGNORE is removed from the list of matches. If the nocaseglob option is set, the matching against the patterns in GLOBIGNORE is performed without regard to case. The filenames . and .. are always ignored when GLOBIGNORE is set and not null. However, setting GLOBIGNORE to a non-null value has the effect of enabling the dotglob shell option, so all other filenames beginning with a ‘.’ will match.

If we set it to .:.., both . and .. will be ignored. Since setting it to anything non-null will also get this behaviour, we might as well set it to just .

So:

GLOBIGNORE=.
ls -d .*

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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