How do you recursively delete all hidden files in

2019-03-09 18:23发布

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!

标签: macos shell unix
6条回答
时光不老,我们不散
2楼-- · 2019-03-09 18:33

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 {} \;
查看更多
姐就是有狂的资本
3楼-- · 2019-03-09 18:35
find /path -iname ".*" -type f -delete ;

Ruby(1.9+)

ruby -rfileutils -e 'Dir["**/.*"].each{|x| FileUtils.rm(x) if File.file?(x)}'
查看更多
狗以群分
4楼-- · 2019-03-09 18:45

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.

查看更多
Deceive 欺骗
5楼-- · 2019-03-09 18:49

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
查看更多
神经病院院长
6楼-- · 2019-03-09 18:49

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
查看更多
乱世女痞
7楼-- · 2019-03-09 18:55
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

查看更多
登录 后发表回答