Recursively remove files

2019-01-09 22:07发布

Does anyone have a solution to remove those pesky ._ and .DS_Store files that one gets after moving files from a Mac to A Linux Server?

specify a start directory and let it go? like /var/www/html/ down...

标签: linux bash
12条回答
趁早两清
2楼-- · 2019-01-09 23:01

It is better to see what is removing by adding -print to this answer

find /var/www/html \( -name '.DS_Store' -or -name '._*' \) -delete -print
查看更多
【Aperson】
3楼-- · 2019-01-09 23:02

This also works:

sudo rm -rf 2018-03-*

here your deleting files with names of the format 2018-03-(something else)

keep it simple

查看更多
你好瞎i
4楼-- · 2019-01-09 23:04

change to the directory, and use:

find . -name ".DS_Store" -print0 | xargs -0 rm -rf
find . -name "._*" -print0 | xargs -0 rm -rf

Not tested, try them without the xargs first!

You could replace the period after find, with the directory, instead of changing to the directory first.

find /dir/here ...
查看更多
做个烂人
5楼-- · 2019-01-09 23:04

You could switch to zsh instead of bash. This lets you use ** to match files anywhere in a directory tree:

$ rm /var/www/html/**/_* /var/www/html/**/.DS_Store

You can also combine them like this:

$ rm /var/www/html/**/(_*|.DS_Store)

Zsh has lots of other features that bash lacks, but that one alone is worth making the switch for. It is available in most (probably all) linux distros, as well as cygwin and OS X.

You can find more information on the zsh site.

查看更多
Emotional °昔
6楼-- · 2019-01-09 23:06
find /var/www/html \( -name '.DS_Store' -or -name '._*' \) -delete
查看更多
做自己的国王
7楼-- · 2019-01-09 23:10

if you have Bash 4.0++

#!/bin/bash
shopt -s globstar
for file in /var/www/html/**/.DS_Store /var/www/html/**/._ 
do
 echo rm "$file"
done
查看更多
登录 后发表回答