Search code examples
bashterminalsolaris

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?


Solution

  • 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 '{}'.