Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Clean .DS_Store, .Trash, and ._resources files prior to copy UNIX
Frequently we need to clean a directory before zipping it or copying it to an external USB drive to be used by Windows or Linux users.

Apple Finder has the custom of populating directories with those unavoidable .DS_Store files, volumes with .Trashes, and some files (especially pictures) with ._resources. The following interactive script will safely remove these files prior to copying.
#!/bin/sh
# bash script to clean (delete) Finder .DS_Store, .Trashes and ._resources
# Use cleandsstores.sh 
# juanfc 2010-03-06

if [ $# != 1 ]
then
  echo "ERROR:  use\n\t`basename $0` dirtoclean"
  exit 1
fi

res=`find "$@" \( -name ".DS_Store" -or -name ".Trashes" -or -name "._*" \) -print`


if [[ -z $res ]]; then
  echo "nothing to delete"
  exit 0
else
  echo "Going to delete:"
  echo $res
fi
read -p "Ok (yYsS1)?" ok

case $ok in
  [yYsS1] )
    find "$@" \( -name ".DS_Store" -or -name ".Trashes" -or -name "._*" \) -exec rm -rf "{}" \; -prune ;;
  * )
    echo "aborted."
esac

exit 0
[robg adds: I haven't tested this one. This older hint contained a one-liner to clean up just the .DS_Store files, and a commenter there notes the existence (beginning in 10.5) of the dot_clean command to simplify the task.]
    •    
  • Currently 2.32 / 5
  You rated: 4 / 5 (19 votes cast)
 
[24,295 views]  

Clean .DS_Store, .Trash, and ._resources files prior to copy | 12 comments | Create New Account
Click here to return to the 'Clean .DS_Store, .Trash, and ._resources files prior to copy' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Clean .DS_Store, .Trash, and ._resources files prior to copy
Authored by: TonyT on Mar 09, '10 01:13:14PM

Why is this even here as a hint? This script should not be used.

After copying the files to a USB, run dot_clean.
dot_clean has been around since 2006:

http://developer.apple.com/mac/library/documentation/Darwin/Reference/ManPages/man1/dot_clean.1.html

NAME
dot_clean -- Merge ._* files with corresponding native files.

SYNOPSIS
dot_clean [-fmnsv] [--keep=[mostrecent|dotbar|native]] [dir ...]

DESCRIPTION
For each dir, dot_clean recursively merges all ._* files with their cor-
responding native files according to the rules specified with the given
arguments. By default, if there is an attribute on the native file that
is also present in the ._ file, the most recent attribute will be used.

If no operands are given, a usage message is output. If more than one
directory is given, directories are merged in the order in which they are
specified.

Edited on Mar 09, '10 01:14:50PM by TonyT



[ Reply to This | # ]
Clean .DS_Store, .Trash, and ._resources files prior to copy
Authored by: joeyblades on Mar 09, '10 01:32:01PM

dot_clean only removes the ._* files, not the other .files that Mac OS leaves around.



[ Reply to This | # ]
Clean .DS_Store, .Trash, and ._resources files prior to copy
Authored by: TonyT on Mar 10, '10 01:32:01PM

ok, fair enough, but as others have commented, better to use dot_clean then rm .DS_Store, etc..



[ Reply to This | # ]
Clean .DS_Store, .Trash, and ._resources files prior to copy
Authored by: TimBonnici on Mar 09, '10 01:38:02PM
If you don't want to use the command line then I'd heartily recommend Blue Harvest. You can either set it to automatically clean specified disks/folders/servers/non-HFS formatted disks or you can manually clean specific folders/disks. It's worth every cent of the $16.95 registration fee. (I've got no relation to the developer, just a satisfied user.)

[ Reply to This | # ]
Clean .DS_Store, .Trash, and ._resources files prior to copy
Authored by: Fillman on Mar 11, '10 05:39:18PM

I'll second the recommendation for Blue Harvest. I'm also a well satisfied user. I don't care about dot files in the Mac but my SysAdmin gets cranky about the trail of superfluous files littering the SMB mounts of our corporate network or USB sticks I share with colleagues.

Blue Harvest does its job quietly and efficiently.



[ Reply to This | # ]
Clean .DS_Store, .Trash, and ._resources files prior to copy
Authored by: joeyblades on Mar 09, '10 01:53:55PM

I think it's bad practice to delete these files all willy-nilly in the source directory. It's better to move them first, then remove them from the destination directory.

I have a little bash script I run for this and it even has a failsafe to prevent running in the wrong directory. If I get time later this evening, I may add it to this thread.



[ Reply to This | # ]
Clean .DS_Store, .Trash, and ._resources files prior to copy
Authored by: RawkBob on Mar 09, '10 02:17:15PM
I tend to use dot_claen too, but it doesn't remove ds_store files, only the resource forks! for ejecting disks i use the following.
dot_clean -m -keep=mostrecent "$@"

find "$@" -name ".DS_Store" -exec rm {} \;

rm -f "$@"/.VolumeIcon.icns
rm -Rf "$@"/.fseventsd
rm -Rf "$@"/.TemporaryItems
rm -Rf "$@"/.Spotlight-V100
rm -Rf "$@"/.Trashes

diskutil eject "$@">/dev/null
where $@ is the usb drive - I put it into an automator action. Download here if you want... you click the action then choose the usb drive. http://offsite.rawkstar.net/temp/CleanupMyDrive.zip
---
http://www.rawkstar.net - music news and reviews


[ Reply to This | # ]
Clean .DS_Store, .Trash, and ._resources files prior to copy
Authored by: tedw on Mar 09, '10 09:42:24PM

For zipping files, the 'zip' command line tool has an option (-df or --datafork) which excludes Mac OS specific files from the archive. it also has other useful options, such as for converting text document line-endings and adjusting filenames to conform to MSDOS naming conventions.

Both the 'cp' and 'rsync' command lines tools have options for excluding resource forks during copying.

When you're doing transfers like this, it's generally better to exclude than delete - keeping the originals whole benefits those working on the Macs who may have (unwittingly) stored comments or other useful information in resource forks.



[ Reply to This | # ]
Clean .DS_Store, .Trash, and ._resources files prior to copy
Authored by: joeyblades on Mar 09, '10 10:33:55PM

As promised, here's the script I run... but first, a word about my fail-safe mechanism. I didn't want to accidentally nuke dot files in the wrong directory or volume, so I only run this script after cd'ing to the directory where I want to recursively remove dot files. For this script to run, I require a special file named "!UnDotAble" to be in the directory. This file doesn't have any content, so you can just create it with 'touch'. I generally lock it so I don't accidentally delete it when I'm moving stuff back and forth.

OK, here's my script:

#!/bin/bash

if [ -e "!UnDotAble" ]
then
    dot_clean -m --keep=mostrecent .
    find . -name "\.*" -exec rm -rf {} \;
else
    echo "!UnDotAble file not found - dot files untouched"
fi


[ Reply to This | # ]
There´s an app for that ...
Authored by: tice on Mar 10, '10 02:51:58AM
Just use the freeware CleanArchiver. Creates clean ZIP files.

[ Reply to This | # ]
Clean .DS_Store, .Trash, and ._resources files prior to copy
Authored by: luai on Mar 15, '10 09:21:23AM

You could try " OptmUSB". Nice, easy and clean

http://www.arraez.cat/Arraezcat/HOME.html

or

http://www.macupdate.com/info.php/id/33684/optimusb



[ Reply to This | # ]
Clean .DS_Store, .Trash, and ._resources files prior to copy
Authored by: iangoldby on May 25, '10 11:40:49PM

Do NOT use OptmUSB if you have any other files or directories on the disk that begin with a dot. I tried it on a media player with Rockbox installed, which puts all its system files in a directory called ".rockbox".

OptmUSB completely deleted the .rockbox directory, leaving me with a media player that wouldn't boot. I was not happy.

OptmUSB seems to delete all files it finds with names starting with a dot, irrespective of whether they are the special Mac OS trashes/Spotlight/fsevents files. Anyone who stores preferences from UNIX style programs on a USB stick will also fall foul of this, since the convention there is to name settings files starting with a dot. Web developers will lose files like .htaccess, etc.

You have been warned.



[ Reply to This | # ]