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?
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 '{}'
.
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 '{}' \;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With