Sunday, April 29, 2012

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
OR
$ 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.

No comments:

Post a Comment