148

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

2

12 Answers 12

177

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 ...
11
  • 2
    I know his question didn't ask for it, but I can never remember: does your example handle filenames with spaces? Commented Jan 6, 2010 at 22:41
  • 4
    Yes, that is what the print0 and the -0 to xargs is for. Normally it wouldn't handle spaces correctly, however with print0 it will print the filename with a null character at the end of the line, which xarg with -0 will then use to pass the full path to xargs without a chance of having the whitespace being used a second or third parameter to the rm command which could be really bad!
    – X-Istence
    Commented Jan 6, 2010 at 22:47
  • 7
    @X: newer findutils supports a -delete action, which could shorten this. @JT: This searches recursively under .. Depends on how many files and subdirectories there are... can't you just just forbid those from being uploaded?
    – ephemient
    Commented Jan 6, 2010 at 23:25
  • 1
    Using the -r (recursive) flag is not recommended in this case since you don't want to delete any directories.. Commented Jul 4, 2013 at 14:49
  • 2
    @IlmariKaronen: good point. Wouldn't command rm be a better (that is, more direct) solution? (By "direct" I mean it deals specifically with the problem at hand and nothing else, and therefore is less likely to have any unwanted side-effects, and is also going to be more self-explanatory to anyone who has to maintain the code later.)
    – iconoclast
    Commented Nov 25, 2014 at 19:55
93
find /var/www/html \( -name '.DS_Store' -or -name '._*' \) -delete
0
93

Newer findutils supports -delete, so:

find . -name ".DS_Store" -delete

Add -print to also get a list of deletions.

Command will work for you if you have an up-to-date POSIX system, I believe. At least it works for me on OS X 10.8 and works for others who've tested it on macOS 10.12 (Mojave).

Credit to @ephemient in a comment on @X-Istence's post (thought it was helpful enough to warrant its own answer).

6
  • 1
    Works back in OSX 10.6 as well.
    – hoss
    Commented Dec 12, 2013 at 14:40
  • 5
    Cleanest and simplest solution, +1!
    – limist
    Commented Feb 19, 2014 at 10:20
  • 7
    @OneOfOne: as my comment says, I got this from @ ephemient. As a beginner to bash, I found your (perhaps more correct) solution far less readable and usable. They may be functionally identical but I find this syntax provides more clarity.
    – rattray
    Commented Mar 6, 2014 at 18:30
  • 1
    The '.' isn't necessary because find recurses into the current working directory by default. Commented Sep 8, 2016 at 17:35
  • In newer versions of the program, the directory may be omitted, and it will imply the current working directory (wikipedia). You still need . on macOS 10.12 (Sierra). Commented Oct 16, 2016 at 5:40
19

Simple command:

rm `find ./ -name '.DS_Store'` -rf
rm `find ./ -name '._'` -rf

Good luck!

2
  • 1
    This works great. BUT a word of caution, I just used this rm -i find ./ -name '*py'` -rf, which did NOT prompt me to delete files. you will need to take it on the end (i.e. -rfi`). Thank god for my backup script. Commented Apr 24, 2013 at 19:11
  • 1
    If there are too many files this breaks with the error: -bash: /bin/rm: Argument list too long
    – Viktor
    Commented Jun 25, 2014 at 10:46
12
cd /var/www/html && find . -name '.DS_Store' -print0 | xargs -0 rm
cd /var/www/html && find . -name '._*' -print0 | xargs -0 rm
1
  • 2
    I liked this one better simply because the "rm" doesn't have "-r" which isn't necessary for this specific task. Commented Aug 17, 2011 at 8:44
6

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.

1
  • 1
    A possible problem: since you don't use xargs, you might run into the command-line argument length restriction. Also, Bash 4 supports ** too :-) (though not by default, shopt -s globstar needs to be set)
    – ephemient
    Commented Jan 6, 2010 at 23:26
4
find . -name "FILE-TO-FIND"-exec rm -rf {} \;
3
  • Using exec for each file found is not fast. It is faster to have find print them out and then use xargs to invoke rm once.
    – X-Istence
    Commented Jan 6, 2010 at 22:38
  • 3
    However, if you have a find that supports it, + is xargs-like: find . -name "FILE-TO-FIND" -exec rm -rf {} + - (also, you're missing a space before -exec) Commented Jan 7, 2010 at 2:38
  • 3
    we never had xargs in my day. And we had to walk to school, uphill both ways ....... Commented Jul 7, 2012 at 1:02
3

Example to delete "Thumbs.db" recursively;

find . -iname "Thumbs.db" -print0 | xargs -0 rm -rf

Validate by:

find . -iname "Thumbs.db"

This should now, not display any of the entries with "Thumbs.db", inside the current path.

1
  • Please be sure your answer prints a list of what's being deleted and explain why someone might want to use xargs and print0 instead of exec (there are reasons).
    – vhs
    Commented Mar 21, 2019 at 9:01
3

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
2

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
3
  • 2
    Running a new copy of rm for each file is wasteful.
    – X-Istence
    Commented Jan 8, 2010 at 17:16
  • 3
    @X-Istence premature optimization is wasteful
    – Lucina
    Commented Nov 16, 2011 at 12:32
  • Upvoted for originality and use of more recent bash innovations.
    – vhs
    Commented Mar 21, 2019 at 8:59
1

A few things to note:

'-delete' is not recursive. So if .TemporaryItems (folder) has files in it, the command fails.

There are a lot of these pesky files created by macs: .DS_Store ._.DS_Store .TemporaryItems .apdisk

This one command addresses all of them. Saves from running find over and over again for multiple matches.

find /home/foo \( -name '.DS_Store' -or -name '._.DS_Store' -or -name '._*' -or -name '.TemporaryItems' -or -name '.apdisk' \) -exec rm -rf {} \;
0

This also works:

sudo rm -rf 2018-03-*

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

keep it simple

1
  • You don't explain when someone might want to prefix a command with sudo nor do you provide any information how rm works in the context of your answer.
    – vhs
    Commented Mar 21, 2019 at 8:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.