http://www.perlmonks.org?node_id=1005788


in reply to Re: Search for a file created today and rename it by appending the current date and copy it to a different location
in thread Search for a file created today and rename it by appending the current date and copy it to a different location

Thanks. Could you please tell me how can i use the File::Find function to search for a file modified or created today?

  • Comment on Re^2: Search for a file created today and rename it by appending the current date and copy it to a different location

Replies are listed 'Best First'.
Re^3: Search for a file created today and rename it by appending the current date and copy it to a different location
by afoken (Chancellor) on Nov 27, 2012 at 05:11 UTC

    File::Find traverses a directory tree and calls the wanted function for each directory entry found. Inside the wanted function, you can use lstat or stat or one of the -X functions to decide if you want to process that directory entry. Example:

    #!/usr/bin/perl use strict; use warnings; use File::Find; find( { wanted => sub { return unless -l $File::Find::name; print "Found a symlink: $File::Find::name\n"; } }, '.' );

    File::Find comes with a script named find2perl. You invoke it like find, but instead of traversing directories, it writes perl code for performing the directory traversal using File::Find to STDOUT. The generated code is not pretty, may have some unused parts, but it generally works:

    /tmp>find2perl . -type l #! /usr/bin/perl -w eval 'exec /usr/bin/perl -S $0 ${1+"$@"}' if 0; #$running_under_some_shell use strict; use File::Find (); # Set the variable $File::Find::dont_use_nlink if you're using AFS, # since AFS cheats. # for the convenience of &wanted calls, including -eval statements: use vars qw/*name *dir *prune/; *name = *File::Find::name; *dir = *File::Find::dir; *prune = *File::Find::prune; sub wanted; # Traverse desired filesystems File::Find::find({wanted => \&wanted}, '.'); exit; sub wanted { my ($dev,$ino,$mode,$nlink,$uid,$gid); (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) && -l _ && print("$name\n"); }

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)