Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Traverse a directory

by singho (Novice)
on Sep 05, 2012 at 07:52 UTC ( #991767=perlquestion: print w/ replies, xml ) Need Help??
singho has asked for the wisdom of the Perl Monks concerning the following question:

I am writing a perl script, to find me some files in the directory provided either as a command line arg or in the variable, what I am trying to do is, to go in the dir, look for files, run file command(also want to know if there is any alternative of unix file command) on files owned by particular user, and move it to some location if some pattern matched. Want to know how File::Find can help me with this. Below is the code.
#!/usr/bin/perl use strict; use warnings; my $dirname = <STDIN>; #chomp($dirname); opendir(DIR, $dirname) or die "Can't opendir $dirname : $!\n"; while (defined(my $file = readdir(DIR))) { print "$file\n"; } closedir(DIR);

Comment on Traverse a directory
Download Code
Re: Traverse a directory
by Corion (Pope) on Sep 05, 2012 at 08:08 UTC

    File::Find can help you with this, but the benefits of using File::Find over opendir, readdir or glob when you are not recursing downwards through child directories are very limited.

    For getting the user information, stat is what you want.

      Thanks for your input.

      Well, i was looking for something similar to which gives me output like file command on unix. stat, won't solve my problem.

      my issue is more of finding core files which are owned by root, but also if generated by certain application it should be moved to different areas depending on which program has generated it.

        Ah - if you want to determine the "file type" (which is always guesswork), there is MMagic and File::MimeInfo.

        Update: Looking at File::MMagic, it doesn't seem to have a magic number for recognizing core dump files. A likely faster way would be to determine the magic number(s) for core dump files on your system (maybe from the file magic file, maybe under /etc somewhere) and then write Perl code that reads just enough data to just (reliably) detect those core dump files.

Re: Traverse a directory
by zentara (Archbishop) on Sep 05, 2012 at 10:48 UTC
    Also see Extract file type from stat() mode
    #!/usr/bin/perl -w use strict; use File::Find; # my @directories = ('/dir1','/dir2'); my @directories = ('.'); # for testing this example find(\&wanted,@directories); sub wanted { my ($dev, $ino, $mode, $nlink, $uid, $gid) = lstat($_); print $File::Find::name . " $dev "."$mode "."\n"; }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: Traverse a directory
by asqwerty (Acolyte) on Sep 05, 2012 at 11:47 UTC

    This works for me (using find in linux):

    #!/usr/bin/perl use strict; use warnings; my $dirname = "your_directory"; my $user = "your_user"; my @fn = map {split /\n/} `find $dirname -user $user`; foreach my $f (@fn){ print "$f\n"; }

      Apart from forking to an external process being of questionable use, your code will fail if $dirname contains a space, or a file name contains a line break; also, you cannot iterate over the list until you get it in full (could take a few minutes).

      Anyway, I'm saying that using modules is usually more robust and flexible and portable than shelling out to some command.

Re: Traverse a directory
by pvaldes (Chaplain) on Sep 05, 2012 at 16:49 UTC

    I want to know if there is any alternative of unix file command

    Yes, and you have an example here

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2014-07-31 03:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (244 votes), past polls