Linux / UNIX: Bash Find And Delete All Hidden Files Directories

How can I find all all hidden files and directories (starting with . character) under UNIX or Linux and delete them using bash or ksh shell?

Use the following command to list all hidden files in /path/to/dest/ directory
$ find /path/to/dest/ -iname ".*" -maxdepth 1 -type f
To list all hidden directories use the following command:
$ find /path/to/dest/ -iname ".*" -maxdepth 1 -type d
To delete all hidden files under UNIX or Linux use the following command:
$ find /path/to/dest/ -iname ".*" -maxdepth 1 -type f -delete
$ find /path/to/dest/ -iname ".*" -maxdepth 1 -type f -exec rm {} \;
To delete all hidden directories under UNIX or Linux use the following command:
$ find /path/to/dest/ -iname ".*" -maxdepth 1 -type d -exec rm -rf {} \;
If you removed -maxdepth 1 it will find all subdirectories and remove them too.

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

🐧 3 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
3 comments… add one
  • Srivignesh Apr 18, 2012 @ 14:47

    find /path/to/dest/ -iname “.*” -maxdepth 1 -type f -exec rm {} \;
    worked for me… Thanks

  • erik Feb 27, 2013 @ 4:51

    this can be very dangerous,
    when the /path/to/dest/ is . -iname “.*” will match .
    which is probably not what you want.
    (with the -exec rm -rf {} \; it could be very very bad)
    i tend to use -name “.[^.]*” or -path “*/.*”
    depending on whether or not I want to match the contents of the hidden directories.

  • Anup May 31, 2013 @ 8:01

    Command : find /path/to/dest/ -iname “.*” -maxdepth 1 -type f gives me below error :
    you have specified the -maxdepth option after a non-option argument -iname, but options are not positional (-maxdepth affects tests specified before it as well as those specified after it)

    Use maxdepth option before -iname
    $ find /path/to/dest/ -maxdepth 1 -iname “.*” -type 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