APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

Perl directory listing

A "pretty" or custom directory listing is a good place to start developing your scripting skills if you want to. There certainly are utilities that do pretty listings, but the advantage of rolling your own is that you can do EXACTLY what you want and gain some useful skills while you are at it.

You can do this in the shell, but I'd strongly recommend Perl because in the long term Perl can do so much more for you. Since your scripting skills are near to zero right now, it actually might be LESS painful to develop some Perl expertise.

I have a number of Perl scripts and recommended books etc. on my site: a search for Perl there could keep you occupied for a while.

Just to get you started, here's a simple Perl script that walks down subdirectories and prints a two column list consisting of just the filename and then the full path.

#!/usr/bin/perl
use File::Find;
use File::Basename;

@ARGV=qw(.) if not @ARGV;
find(\&doit, @ARGV);

sub doit {
   $fpath=$File::Find::name;
   $fname=basename($fpath);
   print "$fname $fpath\n";
}
 

Pipe that to "sort" and you have your first listing. The purpose of this might me to help spot duplicate filenames or to look for specific names more easly.

a ./a
b ./b
c ./c
fred ./a/fred
fred ./c/fred
fred ./fred
john ./b/john
john ./john

As you can probably imagine, it's pretty easy to add to this for whatever your needs are. Note that Perl does most of the hard work for you, leaving you to be creative. To do this in the shell isn't exceedingly difficult, but it does require a lot more thinking than this did.

Note: You can do whatever you need to do in the "doit" subroutine (rename files, open them, and so on) but you need to know that File::Find has moved into that directory - for example, if you were going to rename all "fred" files "Fred", you'd just do rename "fred", "Fred".


Got something to add? Send me email.





(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> -> Perl directory listing


2 comments



Increase ad revenue 50-250% with Ezoic


More Articles by

Find me on Google+

© Tony Lawrence







Sun Sep 7 16:52:54 2008: 4533   anonymous


What if you wanted to pipe the results to another subroutine, to rename certain files that matched a filter for example? How would you do that without taking up all your memory if there were millions of files?



Sun Sep 7 17:16:13 2008: 4534   TonyLawrence

gravatar
Obviously you'd need to break things up into chunks.

Programming isn't about "this is the way to do everything" and the purpose of this post certainly wasn't to discuss extreme conditions.

------------------------
Kerio Samepage


Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us





There are two major products that came out of Berkeley: LSD and UNIX. We do not believe this to be a coincidence. (Jeremy S. Anderson)

The nice thing about standards is that you have so many to choose from. (Andrew S. Tanenbaum)







This post tagged: