LinuxQuestions.org
Support LQ: Use code LQCO20 and save 20% on CrossOver Office
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 04-06-2005, 10:33 AM   #1
cricos
LQ Newbie
 
Registered: Feb 2004
Posts: 6

Rep: Reputation: 0
Find out if a file was modified in the last 2 minutes....


Hello,

Newbie and need some help with a shell script to find out if a file was modified in the last n minutes (for example 2 minutes). Much appreciated.

Thank you
cricos
 
Old 04-06-2005, 10:51 AM   #2
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,282

Rep: Reputation: 61
I would use the find command (maybe there is a better way ?)

Code:
#!/bin/bash
dir="/path/to/dir/of/the/file"
name="myfile"

test=$(find $dir -name "$name"  -mmin -2 -maxdepth 1)
[ ! -z $test ] && echo "$name was modified in the last 2 minutes"
 
Old 04-06-2005, 11:43 AM   #3
puffinman
Member
 
Registered: Jan 2005
Location: Atlanta, GA
Distribution: Gentoo, Slackware
Posts: 217

Rep: Reputation: 30
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;
}
 
Old 04-06-2005, 12:05 PM   #4
puffinman
Member
 
Registered: Jan 2005
Location: Atlanta, GA
Distribution: Gentoo, Slackware
Posts: 217

Rep: Reputation: 30
Or here's a super condensed linux only version more in the perl spirit...

Code:
#!/usr/bin/perl                                                                 
foreach (`ls`) { chomp; print "$_\n" if (-M $_)*24*60 < $ARGV[0] }
 
Old 04-06-2005, 02:52 PM   #5
cricos
LQ Newbie
 
Registered: Feb 2004
Posts: 6

Original Poster
Rep: Reputation: 0
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.

Thanks again for help.
 
Old 04-06-2005, 02:57 PM   #6
puffinman
Member
 
Registered: Jan 2005
Location: Atlanta, GA
Distribution: Gentoo, Slackware
Posts: 217

Rep: Reputation: 30
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Tail Last Modified File in a Directory tyoungblood Linux - Software 3 08-12-2005 08:42 AM
Get directory stats when a file has been modified? marri Programming 2 05-13-2004 08:44 PM
vi won't save modified file. mimsmall Linux - General 1 03-26-2004 10:04 PM
Configuration file I modified will be reset after restart. ICO Slackware 3 02-12-2004 08:00 PM
Can an iso image file be modified? Berhanie Linux - General 3 02-01-2004 09:40 PM


All times are GMT -5. The time now is 04:30 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration