Command Line Access to OS X Filesystem Meta Data
Apple’s OS X is cool.
It occurred to me today to wonder what features of Spotlight could be accessed through the command line (terminal). After some poking around on the Internet I came across some documentation for the “mdls” and “mdfind” commands.
The “md” in the file names stands for “meta data”. For those of you who are unaware, meta data is data about data or data describing data. All files on your computer have meta data, stuff like the date of creation of the file or the file type or size. Apple makes meta data very rich within OS X and has even made file contents part of the searchable meta data as well, which is cool.
Here’s the documentation on mdls and mdfind.
So what sort of things become possible with this sort of functionality? Lots of super geeky things, it turns out. More practically, consider that, with a “cron” job creating a nightly archive of all recently modified files, a script could then be written to “tar” those files and ftp them to a web server. My Dreamhost account gives me 200GB of storage, so I should be good to go for a while since my Mac only has an 80GB HDD. Here’s what the bash script to get the file listing and tar the files would look like:
#!/bin/bash
MYDATE=$(date +%Y%m%d)
mdfind '(kMDItemContentModificationDate > $time.today)' >archive.txt
tar cf archive_$MYDATE.tar -T archive.txt
perl send2ftp.pl archive_$MYDATE.tar
The Perl script that is called in the end of the bash script would look something like this:
use Net::FTP;
$ftpobj = Net::FTP -> new ("mysite.com");
$ftpobj -> login("user","password");
$ftpobj -> cwd ("/archives");
$ftpobj -> put ($ARGV[0]);
$ftpobj -> quit;
If this script were run daily at a few minutes before midnight, it would capture everything done in a day and send it to the web server. That’s cool.

Post a Comment