Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

NEVER RUN UNIX FIND AGAIN (See related posts)

Everyone knows the unix find command is very slow and drains system resources. This perl code walks a file system so fast. As it walks it gathers some great info on each an every file. This script is just a template for walking the file system. The wanted section is where you add your code as needed. Maybe you need to check to see if a file has the UID of 534 and make a list. It's all up to your needs.

#!/opt/perl/bin/perl

use File::Find;
find \&wanted, "/";


sub wanted {
    
my $dev;         # the file system device number
my $ino;         # inode number
my $mode;        # mode of file
my $nlink;       # counts number of links to file
my $uid;         # the ID of the file's owner
my $gid;         # the group ID of the file's owner
my $rdev;        # the device identifier
my $size;        # file size in bytes
my $atime;       # last access time
my $mtime;       # last modification time
my $ctime;       # last change of the mode
my $blksize;     # block size of file
my $blocks;      # number of blocks in a file

#Right below here your telling lstat to retrieve all this info on each and every file/directory.  Each and every file/directory is written to $_.

(($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = lstat($_));
    print "Files UID = $uid\n";
    print "Files GID = $gid\n";
    print "Files ctime = $ctime\n";

}


Comments on this post

maciek71 posts on Dec 01, 2006 at 10:29
Nice :-)
maciek71 posts on Dec 01, 2006 at 10:32
To get filenames, add this code to prints:

print "$_ $mode $size $mtime\n";



Sir_-_Jeff posts on May 11, 2007 at 18:29
Wow very fast indeed, even on XP.
To print out file name and path I use :
print $File::Find::name . "\n";

You need to create an account or log in to post comments to this site.


Click here to browse all 5059 code snippets

Related Posts