Check file created is less than 4 hours or not.

Hi,

I need to check some files in one directory whether any files has been created before 4 hours(ie, less than 4 hours from the current time).

Can anybody help me out..?

Thanks in advance..!

Hi
If your's is linux, this should work:

find . -mmin -240

Guru.

Hi Guru,

Thanks for the valuable reply.

But it's not working in my machine..:frowning:

Hi
Do this: You can create a file with a pre-determined date and time. And then you can tell find command to get all the files created after or before this reference file:

$ touch 200706170200    file
$ find . -newer file

The first command will create a file with timestamp 17-June-2007 02:00. find finds all files created after 2'o clock on 17-June-2007.

Guru.

Hi Guru,

But I cant predict the file creation time.. :frowning:

whatever files are present I need to check for all.

Hi Kattoor
This will get you a list of files that were created roughly(not exactly in seconds) 4 hours ago in the current directory.

ls -l . | awk ' BEGIN { "date +\"%Y-%m-%d\" --date=\"4 hours ago\"" | getline hours } $6 >= hours  { print $8 } '

find . -type f -mmin -240

Hi,

This is listing the time when the files are created in the current directory.

I need to check the files created less than 4 hours from the current time :frowning:

I just found out we could use "240 minutes ago", try this for exactly what you want:

ls -l . | awk ' BEGIN { "date +\"%Y-%m-%d\" --date=\"240 minutes ago\"" | getline minutes } $6 >= minutes { print $8 } '

---------- Post updated at 02:54 AM ---------- Previous update was at 02:45 AM ----------

ok, now this should work:

ls -l --time-style=long-iso | awk ' BEGIN { "date +\"%Y-%m-%d\" --date=\"240 minutes ago\"" | getline minutes } $6 >= minutes { print $8 } '

Thanks a lot for you reply.

If am using the above command in the delow directory,
total 59472
-rw-r--r-- 1 an_s users 6920 Jan 30 2009 config.xml
-rw-rw-r-- 1 an_s users 13562 May 24 05:06 build.xml
-rw-r--r-- 1 an_s users 7566860 May 24 05:42 CerD.ear
-rw-r--r-- 1 an_s users 1427 May 24 05:42 log4j.xml
-rw-rw-r-- 1 an_s users 7587840 May 24 05:43 Cer.tar
drwxrwxr-x 3 an_s users 4096 May 24 06:27 may24

I am getting the output as,

05:42
05:43
05:06
05:42
06:27
2009

Here it is just printing the exact time not the files created 4 hours back.

Can you please it....

what did you get when you run "ls -l --time-style=long-iso"?

You could use Perl:

perl -le'(4/24) > -M and print for @ARGV' *

am gettin this one if i run ls -l

-rw-r--r-- 1 hiran_s users 7566860 May 24 05:42 CerD.ear
-rw-rw-r-- 1 hiran_s users 7587840 May 24 05:43 CerD.tar
-rw-rw-r-- 1 hiran_s users 13562 May 24 05:06 build.xml
-rw-r--r-- 1 hiran_s users 1427 May 24 05:42 log4j.xml
drwxrwxr-x 3 hiran_s users 4096 May 24 06:27 may24
-rw-r--r-- 1 hiran_s users 6920 Jan 30 2009 config.xml

try adding the "--time-style=long-iso" parameter

Yes...It is working.. Thanks a lot...!! :slight_smile:

Hi,

Am facing some issues with perl.

I cant use perl in our env..

Can you please suggest any other solution??

What could you use?

i think i was wrong...sorry.

@Kattoor
Please post your exact Operating System and version or at least the output from "uname -a". It is clear that it is not mainstream Linux with extended functionality in the "date" command.
Is this an automated task which checks at fixed time intervals, or an "on demand" task of some sort? If is is an automated task the solution is fairly simple providing that you have access to cron. If it is an "on demand" task will there be one user or multiple users?
A an earlier poster asked, how accurate does the 4 hours need to be?

Sorry for the confusion.

The version I am using is HP-UX BOX B.11.11 U 9000/800
In my env perl is working fine, but I am not supposed to use 'perl' in our script as I am not sure the perl will work in our live env.

I need to check the 4 hours condition in one of my script that will be invoked using crone. I may have many files in my directory and I need to check out whether any files has been created 4 hours ago.

I hope now you are clear with the requirement.