Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you recursively delete all hidden files in a directory on UNIX?

Tags:

shell

unix

macos

I have been searching for a while, but can't seem to get a succinct solution. I have a Mac with a folder that I want to clean of all hidden files/directories - anything hidden. It used to be a Eclipse workspace with a lot of .metadata/.svn stuff, and I'm fine with all of it being removed. How can I do this (either with a shell script, Applescript, etc). Thanks a lot in advance!

like image 648
SharkCop Avatar asked Feb 21 '11 02:02

SharkCop


People also ask

How remove all files hidden files Linux?

rm -rf /some/path/* deletes all non-hidden files in that dir (and subdirs). rm -rf /some/path/. * deletes all hidden files in that dir (but not subdirs) and also gives the following error/warning: rm: cannot remove directory: `/some/dir/.

How do you remove all recursive files from a directory?

To remove a directory and all its contents, including any subdirectories and files, use the rm command with the recursive option, -r . Directories that are removed with the rmdir command cannot be recovered, nor can directories and their contents removed with the rm -r command.


2 Answers

find . -name ".*" -print

I don't know the MAC OS, but that is how you find them all in most *nix environments.

find . -name ".*" -exec rm -rf {} \;

to get rid of them... do the first find and make sure that list is what you want before you delete them all.

The first "." means from your current directory. Also note the second ".*" can be changed to ".svn*" or any other more specific name; the syntax above just finds all hidden files, but you can be more selective. I use this all the time to remove all of the .svn directories in old code.

like image 58
jmq Avatar answered Sep 20 '22 16:09

jmq


You need to be very careful and test any commands you use since you probably don't want to delete the current directory (.), the parent directory (..) or all files.

This should include only files and directories that begin with a dot and exclude . and ...

find . -mindepth 1 -name '.*' -delete 
like image 41
Dennis Williamson Avatar answered Sep 20 '22 16:09

Dennis Williamson