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?

- 5,753
- 72
- 57
- 129

- 650
- 1
- 8
- 16
-
What programming language are you using? – Mark Byers Aug 15 '10 at 11:22
4 Answers
I think you meant "prepended"
LATER EDIT
SH script:
The previous code didn't work recursively; this one does.
If you want to delete folders too, use this:
find . -name ".?*" -exec rm {} \;
find . -name ".?*" -exec rm -R {} \;

- 30,940
- 9
- 79
- 95
-
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 'SO- stop being evil' Aug 15 '10 at 22:44
-
You might want to use `find . -name "._?*" -exec rm {} \;` instead, as just the dot will delete all files starting with ".", such as `.git`. – Michael Sauter Jul 03 '13 at 00:28
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

- 1
- 1

- 61
- 3
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.

- 16,149
- 12
- 63
- 66
-
11. 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 'SO- stop being evil' Aug 15 '10 at 22:42
-
The -type f makes sure that dirs including ., .. are ignored. Good point though on having files with
'
and"
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. – Bernard Aug 16 '10 at 02:40
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.

- 35
- 5