Linux / Unix: Find And List All Hidden Files Recursively

in Categories , , , last updated January 5, 2018

I am a new Unix system shell user. I need to find and list all hidden files including directories on a Linux or Unix-like system. How can I recursively list all hidden files and directories? How do I save result in a text file?

You need to use the find command to list all hidden files recursively on a Linux or Unix like systems. . You can also use the ls command to list hidden files.

What is a hidden file in Linux or Unix?

In the Unix and Linux based system, a hidden file is nothing but file name that starts with a “.” (period). You can not see hidden files with the ls command.

View hidden files with the ls command

You can pass the -a options to the ls command to see hidden file:
ls -a
OR
ls -al
OR
ls -al | more
Sample outputs:

Unix Linux View Hidden Files
Fig.01: Use ls command to see hidden files

I have two hidden files in the current directory. Let us run the following ls command to see all hidden files in the current directory:
$ ls -d .[!.]?*
$ ls -l -d .[!.]?*

Sample outputs:

drwxr-x---  2 vivek vivek  4096 Dec  9 11:23 .android
drwx------  4 vivek vivek  4096 Oct 20 06:56 .ansible
-rw-r--r--  1 vivek vivek    96 Nov  6 14:13 .backup.conf
-rw-r--r--  1 vivek vivek    57 Nov  8 12:31 .bash_aliases
-rw-------  1 vivek vivek 56819 Jan  5 20:54 .bash_history
-rw-r--r--  1 vivek vivek   220 Oct 14 03:03 .bash_logout
-rw-r--r--  1 vivek vivek  4529 Jan  4 23:06 .bashrc
-rw-r--r--  1 vivek vivek  4165 Jan  4 23:06 .bashrc.backup
drwx------ 37 vivek vivek  4096 Dec 26 00:32 .cache
drwxr-xr-x  3 vivek vivek  4096 Oct 18 19:12 .cert
....
..
drwx------  4 vivek vivek  4096 Nov  9 19:16 .thunderbird
drwxr-xr-x  4 vivek vivek  4096 Nov  6 14:31 .vim
-rw-------  1 vivek vivek 28383 Jan  5 20:31 .viminfo
-rw-r--r--  1 vivek vivek   801 Jan  3 14:16 .vimrc
-rw-rw-r--  1 vivek vivek   383 Dec  2 16:44 .wget-hsts
-rw-------  1 vivek vivek    54 Oct 18 13:57 .Xauthority
-rw-r--r--  1 vivek vivek 39811 Nov 28 00:47 .zcompdump

Recursively list all hidden files and directories on Linux/Unix

The basic syntax is as follows:

find /dir/to/search/ -name ".*" -print

OR

find /dir/to/search/ -name ".*" -ls

Search only hidden files:

find /dir/to/search/ -type f -iname ".*" -ls

Search only hidden directories:

find /dir/to/search/ -type d -iname ".*" -ls

Use find command as follows:
find /dir/to/search -path '*/.*' -print
find /dir/to/search -path '*/.*' -ls

In this example, search $HOME for all hidden files and dirs:
find $HOME -name ".*" -ls
Sample outputs:

6553607    4 -rw-r--r--   1 admi     admi          100 Sep 22  2004 ./.cshrc
6553664    4 -rw-------   1 admi     admi         1024 Nov 17 04:40 ./.rnd
6553604    4 -rw-r--r--   1 admi     admi           18 May 20  2009 ./.bash_logout
6553665    4 drwx------   2 admi     admi         4096 Dec 18  2012 ./.gnupg
6553671    0 -rw-------   1 admi     admi            0 Dec 18  2012 ./.gnupg/secring.gpg
6553669    0 -rw-------   1 admi     admi            0 Dec 18  2012 ./.gnupg/pubring.gpg~
6553673    4 -rw-------   1 admi     admi          899 Dec 18  2012 ./.gnupg/pubring.gpg
6553667    8 -rw-------   1 admi     admi         7856 Dec 18  2012 ./.gnupg/gpg.conf
6553668    4 -rw-------   1 admi     admi         1200 Dec 18  2012 ./.gnupg/trustdb.gpg
6553602   20 -rw-------   1 admi     admi        17462 Apr  7 10:55 ./.bash_history
6553610    4 drwx------   3 admi     admi         4096 Jan 16  2013 ./.config
6553660    4 drwx------   2 admi     admi         4096 Jan 16  2013 ./.config/htop
6553681    4 -rw-r--r--   1 admi     admi          597 Apr 11  2013 ./.config/htop/htoprc
6553723   12 -rw-------   1 admi     admi         9629 Mar 30 23:56 ./.viminfo
6553615    4 drwxr-xr-x   3 admi     admi         4096 Nov 29  2012 ./.original-configs

To save the results to a file called foo.txt, use a redirection as follows:

 ## save result to a special file  called foo.txt ##
find $HOME/projects/ -name ".*" -ls > foo.txt
cat foo.txt

See find(1) for more information.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Share this on (or read 5 comments/add one below):

5 comment

  1. There is no point to using -iname since there are no alpha characters to match, and since it is non-standard, it will only work with GNU find.

    Have a question? Post it on our forum!