unix toolbox
Mac OS X comes with so many tools now, that there are myriad ways of getting things done. I had to present a list of files in a directory with the modification dates recently. I came up with this one liner:
ls -lT | awk ‘{print $10, $7, $8, $9}’ | bbedit
if you do a ls -l you get a long list in your Terminal. The thing left out that I needed was the time on the modification. So if you add in the capital T it gives you the time as well as day, hour and year. I pipe that into awk and use it to print the fields of the ls I need. In this case $10 is the file name, so wanted that on the front of my list. Then I got the Day, Time and Year into $7, $8 and $9. I then piped it into bbedit. If you don’t have bbedit it could get written to a file or piped into vi or something.
After I fulfilled this request I was asked to find out which text files in a directory used one of a number of file names inside of it. I wrote a perl script to find all uses of the name Story00001 - Story00065.
#!/usr/bin/perl
use constant SOURCEPATH => ‘/Volumes/Users/Admin/Sites/check/’;
opendir(DOCS1, “/Volumes/Users/Admin/Sites/check/”);
my @files = grep !/^\.\.?/, readdir DOCS1;foreach $file(@files) {
open (READFILE, SOURCEPATH . $file);
while () {
if ($_ =~ /Story000[0-6][1-5]/g) {
print $file . “\n”;
}
}
}
Basically this perl script reads a directory of filenames into an array. Then it iterates through the array and finds any uses of the reqular expression /Story000[06][0-5]/g. Perl reads each file line by line. I did a directory of 130 files in less than a minute. If it finds that expression in the file then it prints the file name.
We have such a rich capability of script languages on the mac. It just takes a while to harness them. Do man ls, man awk and man perl for local information. Perl obviously is huge. Learning Perl is a good place to start.