DZone 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

Snippets has posted 5883 posts at DZone. View Full User Profile

NEVER RUN UNIX FIND AGAIN

05.04.2006
| 7124 views |
  • submit to reddit
        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

Snippets Manager replied on Fri, 2007/05/11 - 6:26pm

Wow very fast indeed, even on XP. To print out file name and path I use : print $File::Find::name . "\n";

Snippets Manager replied on Fri, 2006/12/01 - 11:25am

To get filenames, add this code to prints: print "$_ $mode $size $mtime\n";

Snippets Manager replied on Fri, 2006/12/01 - 11:25am

Nice :-)