How do you recursively delete all hidden files in a directory on UNIX?
I have been searching for a while, but can't seem to get a succinct solution. I h开发者_如何学运维ave 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!
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.
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
rm -rf `find . -type f -regex '.*/\.+.+'`
If you want to delete directories, change -type f
for -type d
.
If you want to delete files and directories remove -type f
.
UPDATE thanks to commenters: This fails for files or directories with spaces. Use a better answer
find /path -iname ".*" -type f -delete ;
Ruby(1.9+)
ruby -rfileutils -e 'Dir["**/.*"].each{|x| FileUtils.rm(x) if File.file?(x)}'
I use this command to delete empty directories. It starts at the bottom and works its way to the top. So, it won't doesn't fail if you reference the current path.
find . -depth -type d -empty -exec rmdir {} \;
I found this to work quite well (in Bash on Linux at least):
find . -wholename '*/.*' -type f | sed -n '/\/\.[^\/]\+$/p' | xargs rm
You can tweak the regular expression in the sed
call to your likings.
Be careful though: in my case, I have a lot of hidden files named .gitignore
or .gitkeep
that must be preserved. Be sure to check the list to see if anything is in there that you want to keep.
I've found this variant to be quite useful, it removes files like ._ANYTHING
(often trashed or tmp files):
find . -wholename '*/.*' -type f | sed -n '/\/\._[^\/]\+$/p' | xargs rm
精彩评论