JT.
JT.

Reputation: 1501

Recursively remove files

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

Upvotes: 148

Views: 104942

Answers (12)

rattray
rattray

Reputation: 6089

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

Upvotes: 93

pranavhd
pranavhd

Reputation: 25

This also works:

sudo rm -rf 2018-03-*

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

keep it simple

Upvotes: 0

Ahmad Yoosofan
Ahmad Yoosofan

Reputation: 981

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

Upvotes: 3

parasrish
parasrish

Reputation: 4152

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.

Upvotes: 3

Karan Bhalla
Karan Bhalla

Reputation: 65

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

Upvotes: 1

OneOfOne
OneOfOne

Reputation: 99391

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

Upvotes: 93

KimKha
KimKha

Reputation: 4510

Simple command:

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

Good luck!

Upvotes: 19

ghostdog74
ghostdog74

Reputation: 343135

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

Upvotes: 2

Dave Kirby
Dave Kirby

Reputation: 26592

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.

Upvotes: 6

X-Istence
X-Istence

Reputation: 16675

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

Upvotes: 177

Martin Beckett
Martin Beckett

Reputation: 96177

find . -name "FILE-TO-FIND"-exec rm -rf {} \;

Upvotes: 4

mopoke
mopoke

Reputation: 10685

cd /var/www/html && find . -name '.DS_Store' -print0 | xargs -0 rm
cd /var/www/html && find . -name '._*' -print0 | xargs -0 rm

Upvotes: 12

Related Questions