views:

74

answers:

2

As the title states, 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 pesky buggers? Thanks!

+2  A: 

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 {} \;

Gabi Purcaru
@Gabi: Given the use of “recursively” in the question, I think this should be `-name ".*"`. Also, unless your system is quite old and doesn't support it yet, `-exec rm {} +` will call `rm` in batch instead of once per file.
Gilles
@Gilles fixed it, thanks
Gabi Purcaru
A: 

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.

Alkaline
@Alkaline: 1. The argument to `-name` should be just `'.*'`. 2. This won't work if there is a file name (including leading directories) containing spaces or `\"'`, because `xargs` treats these characters specially. Don't use `xargs` unless you know for sure that these characters don't occur in any file or directory name. Use `-exec` instead, as in Gabi's answer. Here, since the command is `rm`, the potential for disaster is particularly great.
Gilles
The -type f makes sure that dirs including ., .. are ignored. Good point though on having files with <code>'</code> and <code>"</code> in their names. xargs tries to interpret them and that causes problem.I'll update my answer with your caveat. I still think that it's useful to include this way of doing things. A simple search shows many mentions of it.
Alkaline