1826703 Members
2724 Online
109696 Solutions
New Discussion

Re: filedate

 
Laszlo Csizmadia
Frequent Advisor

filedate

Must be a trivial question...
but how to determine a file creation date in seconds? Is there one command like filedate in solaris? I want to delete some files older than 30 minutes. Find's -ctime only accepts days.
Thanks in advance.
8 REPLIES 8
Pete Randall
Outstanding Contributor

Re: filedate

Use the touch command to create a reference file, then use the -newer option of find.

Pete

Pete
Paul Sperry
Honored Contributor

Re: filedate

Something like this may work for you.

find . -mtime +1 -exec rm {} \;


This would delete everything older than one day.
monasingh_1
Trusted Contributor

Re: filedate

do not know about filedate but a little lengthy way is to get the inode of file by ls -lai and then:

echo 12345i |fsdb /dev/vgxx/lvolX

where 12345 is your inode number of file.

This will tell ctime.mtime atime which you can grep for doing stuff,

But I will agree with earlier approach of touching a file and then using -newer option in find...

hope this helps..
Sridhar Bhaskarla
Honored Contributor

Re: filedate

Hi,

Pete's method can be used to achive your task.

Create a file by touching it with a timestamp of 30mins back. Then use find with -newer option.

$touch 0305173003 myreference

This creates the file myreference stamped with 17:30 hrs today.

$find /dir -newer myreference

Use it with exec or xargs to delete the thus found files.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Rajeev  Shukla
Honored Contributor

Re: filedate

Hi,
Yes there are ways to find the time of file in seconds but not a straight one. You'll have to write a C program using stat() function and extract st_mtime in structure stat.

Let me know if you need the codes i have for this.

Rajeev
john korterman
Honored Contributor

Re: filedate

Hi Laszlo,
attached is something similar I once did myself. The attachment consists of two scripts.
The following variables of the first script must be configured:
RETURN_SECONDS must point to the second script in the attachment!
DIR must specify the directory starting point under which the files are to be deleted.
AGE must specify in seconds how old the files are allowed to be.

The first script, which is the one you must execute, tries to convert passed seconds into a more user-friendly date format: the month is expressed as three chars, e.g. May or Maj or something else (language dependent). As I do not know which language you use, the script may not make the correct mapping and might therefore fail. Hope you can correct this yourself in the case sentence.
The script makes use of the find command:
! -newer
which means "older than". If you are 100% sure that you get hold of the correct files, you can change the ls -l to rm in the find command. Be really careful.

regards,
John K.
it would be nice if you always got a second chance
Laszlo Csizmadia
Frequent Advisor

Re: filedate

Thanks to all the assistance!
Neither of these was exactly what I was looking for. I think you cannot really deside with shell scripts if a file is older than X seconds (X is configureble parameter) or not. Or at least is too complicated:)
I wrote a little C code and attached. Feel free to use it...
Usage: filedate [-f filename] [-d duration] [-p] [-h]
-d duration exit code 1 if file older than specified duration
duration is in sec. default is 12 hours.
-f filename Checks the data modification date of the specific file
-h Wot d'ya think? :-)
-p Prints the date of the file in %c%y/%m/%d %H:%M:%S
Ralph Grothe
Honored Contributor

Re: filedate

Although you can use the stat() syscall and do more or less weird calculations this is straight forward in Perl.

Remember that the -M test takes the seconds since the epoch when your script started and substracts the file's mtime in seconds since the epoch, and returns the value in days.

(this is much better explained in the POD, type "perldoc -f -x")

So a oneliner to demostrate:

# touch /tmp/smp;sleep 12; perl -e '$t=-M "/tmp/smp";print $t*=86400,"\n"'
12
Madness, thy name is system administration