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

Perl directory listing

© August 2013 Tony Lawrence

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.

use File::Find;
use File::Basename;

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

sub doit {
   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


Inexpensive and informative Apple related e-books:

iOS 8: A Take Control Crash Course

Take Control of Parallels Desktop 12

El Capitan: A Take Control Crash Course

Take Control of Preview

Photos: A Take Control Crash Course

More Articles by © 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

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.


Printer Friendly Version

Have you tried Searching this site?

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

Printer Friendly Version

The Internet? We are not interested in it. (Bill Gates, 1993)

Linux posts

Troubleshooting posts

This post tagged:





Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode