Operating System - Linux
1748181 Members
3603 Online
108759 Solutions
New Discussion юеВ

Re: Wana help in writing a script

 
cse.ashish
Occasional Contributor

Wana help in writing a script

Hi All,

I am looking for a command which can filter out all the files which have been created or modified 15 days or before from the today's date.
And I have to run this script/command on daily basis. So I want it to take current system date itself (I mean I want to make it generalize).
Can any buddy help me out in this.

Regards,
Ashish
Nothing is impossible in this world cauz impossible itself says that "I M POSSIBLE"
5 REPLIES 5
Pete Randall
Outstanding Contributor

Re: Wana help in writing a script

You want to look at the find command. I think what you're saying is you want to exclude the files from the most recent 15 day period? This should do it:

find /start_dir -type f -mtime +15

That will give you a list of files created or modified prior to the last 15 days.

To exclude those:

find /start_dir -type f ! -mtime +15

The results of either of those can be piped to xargs to take some action on them. To remove the files older than 15 days:

find /start_dir -type f -mtime +15 |xargs rm


Pete

Pete
James R. Ferguson
Acclaimed Contributor

Re: Wana help in writing a script

Hi:

First, Unix does not have a "creation" timestamp. There is a last-modified ('mtime') value that at the moment of creation represents the named time but thereafter the value is the last modification time.

# find /path -xdev -type f -mtime +15

See the manpages for 'find(1) and 'stat(2)' for more details.

Regards!

...JRF...
Piergiacomo Perini
Trusted Contributor

Re: Wana help in writing a script

Hi Ashish,

maybe a command like this :

find / -type f -mtime +15 -print -exec ls {} \;

(that take file older than 15 days)
could be a good starting point for your script.

Hth
regards
pg
spex
Honored Contributor

Re: Wana help in writing a script

Hello,

I guess I'm interpreting Ashish's request differently...

Show files in /path with last modified time less than 15 days:

$ find /path -type f -mtime -15 -print

If you wish to perform an action on these files, 'find' has the '-exec ' primary. You could also pipe the output from 'find' to 'xargs', or to an external file, which you would then read in at a later point.

Note that there is no notion of a "creation date" for files under UNIX. However, as long as a file's timestamp hasn't changed since it was created, last modification time will function as a pseudo-creation time.

PCS
cse.ashish
Occasional Contributor

Re: Wana help in writing a script

Thanks a lot to all. I'll try this and use the same in my script.

And yes, It was my mistake to write "creation time" in my question. May be because of too much of work :) I wrote that. But I meant actually with modified time only.

Thanks & Regards,
Ashish
Nothing is impossible in this world cauz impossible itself says that "I M POSSIBLE"