Mac file manipulation for power users?
December 22, 2011 8:07 PM   Subscribe

Is there a SQL-like way to interact with the Mac filesystem? Failing that, what's better than the Finder for batch copies/moves/deletions?

I have a ton of files spanning many folders that I'd like to delete based on certain criteria (label, modification date, etc).

I spend most of my day interacting with relational databases, so if I could just type something like "DELETE FROM directory, subdirectories WHERE LABEL='RED'" that would be amazing. However, I'm open to anything that's an improvement on using the mouse-heavy Finder to manipulate thousands of files and folders.

I'm running Mac OS 10.7, but solutions for Windows are welcome too (I know about the upcoming filesystem-related features in SQL Server Denali but I need something that works easily for an entire drive). Thanks in advance!
posted by ripley_ to Computers & Internet (12 answers total) 6 users marked this as a favorite
Are you familiar with the unix command line tools? I'm not sure about accessing the content of labels, but your other criteria should be easily met using something like grep.

Alternatively, check out Automator. It's sort of a super basic IDE for writing AppleScript and I wouldn't be surprised if it solved your problem out of the box. It's part of the base OS.
posted by feloniousmonk at 8:08 PM on December 22, 2011 [1 favorite]

posted by backwards guitar at 8:14 PM on December 22, 2011

If you're using command line tools, you want "find"

For example
find . -name "red" -type d -ctime 2
will find all directories named "red" that were changed 2 days ago that are somewhere in the hierarchy of directories under the current directory.

You can use this with "xargs" to script what you want to do with these files, or use the -exec option to find to do the same.

I could go into more detail but there are a billion pages on the internet with examples of find.
posted by RustyBrooks at 8:14 PM on December 22, 2011 [2 favorites]

Somewhere on your Mac, probably buried in the Utilities folder, you should find a program called Terminal. This will give you access to the command line.

Here are some basic unix commands. (Note that this list puts one of the most useful commands last: for any command, you can type "man commandname" to get the manual pages on it.)

After a cursory search I can't find a basic tutorial on shell scripting that I like the looks of, but basically a shell script is a text file with several commands on different lines. You run the shell script and it runs all the commands without you having to enter each one separately. Can be useful for quasi-automating a large number of repetitive commands.

There's also an art to stringing together several commands on one line, so that the results of one command feed into the action of the next one, which I haven't fully mastered.
posted by Orinda at 8:21 PM on December 22, 2011

D'oh - I should clarify that I'm moderately familiar with the unix command line, I'm not sure why that didn't occur to me.

I'll look into how to use grep to manipulate files, thanks - didn't even think of piping its output to rm. I also found osxutils which may let me interact with Mac-specific metadata like labels via the CLI, but it hasn't been updated in a long time (there isn't even a PPC binary!). Will give that a shot, but hopefully there's a better-maintained tool around somewhere.

Also thanks for the Hazel suggestion, I'll take a look at that.

Thanks RustyBrooks, but I meant the file label not the filename.
posted by ripley_ at 8:36 PM on December 22, 2011

To delete based on file labels, I'd use applescript.
posted by pompomtom at 8:48 PM on December 22, 2011

The mdfind command consults the metadata database (the same one used by Spotlight). You could use it to search for label colors and such and delete.
posted by sbutler at 8:51 PM on December 22, 2011 [3 favorites]

mdfind is perfect! Thanks so much.
posted by ripley_ at 9:01 PM on December 22, 2011

For example, this command will find all the files with a red label on the desktop and deleted them:
mdfind -0 -onlyin ~/Desktop "kMDItemFSLabel == 6" | xargs -0 rm -fr --
Might want to test that beforehand by leaving off the "| xargs ..." part.
posted by sbutler at 9:02 PM on December 22, 2011 [1 favorite]

Automator for a nice GUI, bash or some other shell for one off commands and powerful scripts. ProTip: Automator makes a nice frontend for bash scripts, especially asking for parameters for creations you give other people.
posted by Brian Puccio at 9:23 PM on December 22, 2011

Pay attention to those -0 flags (-print0 in find); it's the only way to handle file names with spaces correctly
posted by Nelson at 7:36 AM on December 23, 2011

If you don't want to deal with the arcane problems of unix land, the freeware app easyfind provides a nice front-end to find and grep.

Spotlight in finder can do advanced searches here's an example of a search for MSWord documents modified in the last 30 days with with a specific text tag in the filename.
posted by CBrachyrhynchos at 7:42 AM on December 23, 2011

« Older Unexpected fruits of mockery and malice   |   "Kringle was on top of the world, but then it all... Newer »
This thread is closed to new comments.