Find out if a file was modified in the last 2 minutes....
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Here's something a little more flexible I threw together. This will search the current directory for files modified in the last n minutes, where n is specified as the command argument.
Code:
#!/usr/bin/perl
# test for files recently modified
use warnings;
use strict;
use File::List;
use Cwd;
# the platform independent way to get a directory listing
# i'm on a windows machine right now :(
my $dir=getcwd;
my $search = new File::List($dir);
my @files = @{ $search->find("*") };
# the linux way
# (uncomment this line if you're on linux
# and dont want to install File::List)
# my @files = `ls`;
# pass in number of minutes as argument
my $age_query = shift or die "Usage: $0 minutes";
foreach (@files) {
my $age_in_days = -M $_;
my $age_in_minutes = $age_in_days * 24 * 60;
print "$_\n" if $age_in_minutes < $age_query;
}
Thanks guys but I am looking for something shell script only. Perl is all greek to me.
Actually is better to describe what I am trying to do. My script is going to loop through a directory read each file and find out if the file is used by somebody else or another process (ftp by example). If no, I take that file and process it. That's all.
That's funny, because bash scripts look as alien to me as perl does to bash scripters! You can still use my code up there, just invoke the perl script in the shell script. Perl is standard in linux, I'm sure you have it. It will return you the list of files on stdout.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.