Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Expanding directories

by Anonymous Monk
on Jun 24, 2002 at 10:23 UTC ( [id://176731]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I'm trying to expand a directory based on date, log facility, etc. with syslog-ng I have something like
my @dirs = `ls /var/log/HOSTS/*/*/$year/$month/$day/*`
which doesn't seem to work, because some of the directories don't exist. I'm sure there's a better way, I'm just to new. Thanks, Harry

Replies are listed 'Best First'.
Re: Expanding directories
by grinder (Bishop) on Jun 24, 2002 at 12:13 UTC
    my @dirs = `ls /var/log/HOSTS/*/*/$year/$month/$day/*`

    I'm not sure where you want to go with this, but I assume you want to do something in each directory that exists for the day/month/year for each logged host.

    The canonical Perl way of doing this (without relying on an external program) is to use the venerable File::Find module.

    use File::Find; find( sub { return unless $File::Find::name =~ m{^/var/log/HOSTS/([^/]+)/([^/]+)/(\d+)/(\d ++)/(\d+)/(.*)$}; my( $dir1, $dir2, $year, $month, $day, $file ) = ( $1, $2, $3, $4, $5, $6 ); # do stuff with $file }, '/var/log/HOSTS' );
    Or if you do just want to get the names of the directories that actually exist, you would write something like:
    $File::Find::name =~ m{^/var/log/HOSTS/([^/]+)/([^/]+)/(\d+)/(\d+)/(\d+)$} and push, @dirs, $File::Find::name;

    print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u'
Re: Expanding directories
by bronto (Priest) on Jun 24, 2002 at 10:50 UTC

    Maybe you want something like this:

    @dirs = </var/log/HOSTS/*/*/$year/$month/$day/*>

    Anyway, it's not that "clean", so consider glob or File::Glob. Just do a perldoc -f glob to get some details.

    Ciao!
    --bronto

      If you check the man page for glob you get:
      Beginning with v5.6.0, this operator is implemented using the standard File::Glob extension

      gav^

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://176731]
Approved by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-25 21:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found