enhance my delete skills

Discuss Applications
Post Reply
worker201
guru
guru
Posts: 668
Joined: Sun Jun 13, 2004 6:38 pm
Location: Hawaii

enhance my delete skills

Post by worker201 » Sun Jul 29, 2007 9:10 pm

I'm sure there has to be a way to do this, but I'm not sure if it is rm or not.

I keep my music on my Mac's hard drive, formatted HFS+. I'm about to go overseas, and I am not taking my Mac with me, so I copied all the music to an external drive, so that I can "loan" it to people who have computers or mp3 players. The directory tree of this drive is organized into folders by artist, then by album within the artist folder.

Sometimes, OSX will create what are probably indexing files throughout the directory tree. So if I have the file "O8 Firestarter.m4a", it might produce the file "._08_Firestarter.m4a". These files are always less than 1KB. OSX, as far as the user is concerned, ignores them completely. A Linux machine, for all intents and purposes, will ignore them too. But a Windows machine will see these files and try to add them to playlists, or some other nonsense. Deleting these files doesn't affect disk or file operations in Linux or OSX. So it would be best to delete them before needlessly confusing those unfortunate enough to be using Windows (they're already confused!).

So the question is, how can I recursively delete all these file via my Linux computer? Matching the pattern of "._*.m4a" is easy enough. But there doesn't seem to be a way for rm to cruise through each directory and delete the files. Probably a perl or shell script would do it, and I may just end up writing one. But is there a built-in command that will handle this? A one-liner that goes through and does the whole tree in one swoop would be ideal.


Bonus points to anyone who knows what album the track listed above is from.

User avatar
Void Main
Site Admin
Site Admin
Posts: 5716
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main » Sun Jul 29, 2007 9:47 pm

Change into the directory containing the subdirectories you want to remove the files and:

Code: Select all

find -name '\._*\.m4a' -exec rm -f '{}' \;
Or add the directory in:

Code: Select all

find /mymusicdirectory -name '\._*\.m4a' -exec rm -f '{}' \;
Or:

Code: Select all

find -name '\._*\.m4a' | xargs rm -f

worker201
guru
guru
Posts: 668
Joined: Sun Jun 13, 2004 6:38 pm
Location: Hawaii

Post by worker201 » Mon Jul 30, 2007 1:16 am

Sorry if I have interpreted wrong, had a couple beers :D, but your solutions don't seem like they would work on 3rd level trees...

Will this work for multiple directories? ie:
/music/metallica/killemall/
/music/metallica/ridethelightning/
/music/metallica/masterofpuppets/
?

User avatar
Void Main
Site Admin
Site Admin
Posts: 5716
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main » Mon Jul 30, 2007 7:48 am

What, you don't believe me? :) Yes, all of them will work recursively from the starting directory just as I said:

Code: Select all

$ cd /music
$ find -name '\._*\.m4a' -exec rm -f '{}' \;
or:

Code: Select all

$ find /music -name '\._*\.m4a' -exec rm -f '{}' \;
or:

Code: Select all

$ cd /music
$ find -name '\._*\.m4a' | xargs rm -f
If you want to see what will be deleted without deleting it just do the find without the rm:

Code: Select all

$ find /music -name '\._*\.m4a'

User avatar
Calum
guru
guru
Posts: 1349
Joined: Fri Jan 10, 2003 11:32 am
Location: Bonny Scotland
Contact:

Post by Calum » Tue Jul 31, 2007 10:27 am

they will work recursively because they use `find`, yes? find is recursive, is that right?

if this has been asked over on good old unix.com five'll get ya ten that you'd have got a (to me) incomprehensible answer involving sed!

User avatar
Void Main
Site Admin
Site Admin
Posts: 5716
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main » Tue Jul 31, 2007 10:36 am

http://voidmain.is-a-geek.net/man?param ... d&mode=man

find is an extremely powerful command, especially when combined with other commands. It's definitely worth learning it's capabilities if you are not familiar with it. It is definitely on the short list of must have *NIX administration tools. Just read over the man page and at least look at some of the examples in it....

Post Reply