A bit of experimentation and research this weekend on the MacBook Pro has resulted in some interesting little commandline tricks:
1. Export the results of a MySQL query to a textfile.
One of our social network projects has a database of a few thousand email addresses that we needed to retrieve for use with the email-marketing wonder CampaignMonitor. After a bit of fussing about I found a really simple way to generate this list:
$ echo “SELECT email FROM users” | mysql -u root db_name > list_of_emails.txt
The resulting text list has one email address on each line, which is exactly the format expected by CampaignMonitor, so this was a particularly useful shortcut to figure out.
2. Sending email from the commandline
Of course, now that I had my little list, I then had to send it to my colleague who would be managing the marketing campaign. That turned out to be even easier:
$ mail -v -s “addresses for marketing campaign” user@domain.com < list_of_emails.txt
The “-v” switch means “verbose,” so it’ll actually print some SMTP output to the screen when you run this command. “-s” is the subject, as you probably guessed, and the “<” towards the end means we are using the content of list_of_emails.txt as the message of the email.
3. View the current directory in Firefox/Finder/any other app.
A couple of hours later (and in a totally unrelated capacity), I found myself downloading the entire LinuxCommand.org website for offline viewing (a full archive is provided by author William Shotts here). After doing the usual “tar xvzf” to extract the archive, I started thinking about how cool it’d be to then be able to send a command to Firefox to load up the HTML for viewing. After a little research, it turns out you could do that pretty simply:
$ open -a Firefox `pwd`
The brainstorm here was using the output of pwd (which will print the working directory path) as a parameter to be fed into the particular application. You can do the same thing with Finder as well, just replace the word “Firefox.”
Nifty :)
