Linux / Unix – Find And List All Hidden Files Recursively

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.
Tutorial details
Difficulty level Easy
Root privileges No
Requirements None
Est. reading time 2m

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:

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 .[!.]?*
$ 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

Where ls options are as follows:

  • -l : Use a long listing format.
  • -d : Show directories themselves, not their contents.
  • .[!.]?* OR .?* : Force ls to show only hidden files.

Recursively list all hidden files and directories on Linux/Unix

The basic syntax is as follows for the find command:
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

Conclusion

You learned how to find and list all hidden files under Linux and Unix-like systems using the ls command/find command. See find(1) command man page for more information:
$ man find
$ man ls


🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 6 comments so far... add one


CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
6 comments… add one
  • Vinoth Apr 8, 2014 @ 5:15

    Please try below with ls command.

    ls -lRa > List.txt

  • Chris F.A. Johnson Apr 8, 2014 @ 20:05

    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.

  • daniel andrade Apr 10, 2014 @ 14:51
    find . -name ._\* -print0 | xargs -0 rm -f

    to remove all hidden files recursively! :D

  • beware Jun 16, 2015 @ 15:11

    @daniel andrade

    Why would you ever remove all hidden files recursively?

  • rrrrrrrrrrrrrr Nov 9, 2016 @ 20:48

    directions unclear, my computer gave me a message to press alt + f4

  • Al Aug 3, 2020 @ 12:55

    To ensure your find command does not capture the current directory “.” or the parent directory “..”, use the following “-name” option instead:

    find . -name .[^.]* -print | xargs rm -f

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum