Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete all hidden files in folder and subfolders

I need to delete all hidden files in the current folder and its sub folders. Is there any way to do it with a single line command without creating a script?

like image 854
Dinesh Avatar asked Feb 11 '14 15:02

Dinesh


2 Answers

Use

find "$some_directory" -type f -name '.*' -delete

If you want to remove hidden directories as well, you'll need to take a little more care to avoid . and .., as mentioned by Ronald.

find "$some_directory" -name '.*' ! -name '.' ! -name '..' -delete

With either command, you should run without the -delete primary first, to verify that the list of files/directories that find returns includes only files you really want to delete.

For completeness, I should point out that -delete is a GNU extension to find; the POSIX-compliant command would be

find "$some_directory" -type f -name '.*' -exec rm '{}' \;

i.e., replace -delete with -exec ... \;, with ... replaced with the command line you would use to remove a file, but with the actual file name replaced by '{}'.

like image 158
chepner Avatar answered Sep 30 '22 16:09

chepner


For my Netgear Stora, I wanted to remove all the hidden .webview .thumbnails .AppleDouble etc files and folders. This works from the /home/yourusername/ folder:

find -type f -name '.*' ! -name '.' ! -name '..'  -exec rm -fv '{}' \;

and then

find -type d -name '.*' ! -name '.' ! -name '..'  -exec rm -frdv '{}' \;
like image 33
lemoncurry Avatar answered Sep 30 '22 14:09

lemoncurry