开发者

How do I recursively delete all files in a folder that begin with "._"?

As the title s开发者_如何学JAVAtates, I'm trying to recursively delete all files in a folder that begin with ._. For each file on my site, there exists a file with the same exact name, but with ._ appended to it. How do I get rid of these?


I think you meant "prepended"

LATER EDIT

SH script:

find . -name ".?*" -exec rm {} \;
The previous code didn't work recursively; this one does. If you want to delete folders too, use this:
find . -name ".?*" -exec rm -R {} \;


Possible duplicate of this post, and I've used it successfully on my own production server:

find /var/www/html \( -name '.DS_Store' -or -name '._*' \) -delete


Edit: User Gilles below correctly mentions that the following alternative technique can be dangerous because xargs doesn't treat single and double quotes in a filename literally. A file with a name of Today's special.txt would cause an error (unterminated quotes). I still include it here for sake of completeness, since it is widely mentioned on the web.

Another similar way of doings this on Unix/Mac by piping to rm.

find . -name '.*' -type f | xargs rm -f

The -type f argument tells it to ignores directories and special files like ., .., symbolic links and devs.

We unfortunately can't use something like rm -R .* because the .* input pattern is expanded by the shell before being given to the rm command. It would result in recursively deleting all files that match those from the starting directory.

The Windows equivalent command (del -R .*) is much simpler for the user because the del program does the work of expanding the wildchards for each subdirectory.


find . \( -name '.DS_Store' -or -name '._*' \) -type f -print0 | xargs -0 rm -f

Use this command to include files and folders with space in their names in your search as well.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜