Except for what little Unix I had in college, I mostly used a Windows laptop until last year when I got a MacBook Pro.
Last week I read Mark Sobell’s excellent “A Practical Guile to UNIX for Mac OS X Users”, playing around with various commands and writing scripts. I got an appreciation for how much UNIX had affected the design and naming of Java.
I was going to blog about it when I decided to add my picture to my blog, but I could not find it.
My first tool to solve this was using Finder with thumbnails, but this was not working and was time consuming.
This gave me a real world use case to try and use Linux, which became my second tool. If I was a master of Linux, I am sure I could have found a single line command to list all the directories that contain a JPEG file. However, I am not that Linux master so the best I came up with was the following that listed all the full matching paths of any JPEG file:
find . -name *.jpg
The resulting list did not contain only the directories, so I threw together a Ruby script to strip off the file from the path and add it as a key to a hash if the key did not already exist:
s = "not_nil"
dirs = {}
while s != nil do
s = gets
if s != nil
m = s.gsub /\/[^\/]*.jpg$/,''
dirs[m] = 'value' if !dirs.key? m
end
end
dirs.keys.each do |key|
puts key
end
I then went ahead and piped everything together:
find . -name *.jpg | ruby get_directories.rb | sort | less
This gave me a master list of all the directories I could look over to see if the file was there.
In the end, it didn’t help. I then remembered the Faces feature in iPhoto, and found it two minutes later. Turns out that was the right tool for the job.
find is useful. However I am using ack a lot more these days. Often I refer to this self made guide for ack syntax. http://github.com/neerajdotname/guides/blob/master/ack_guide.txt