1831426 Members
3164 Online
110025 Solutions
New Discussion

Delete Files

 
SOLVED
Go to solution
Prabhu_7
Frequent Advisor

Delete Files

For one of my process i receive a input data file everyday (mon thru friday).

[
02-June-2003 Monday , as of today i'll be having the following list of files.

(Mon thru Friday)
History.dat052603
History.dat052703
History.dat052803
History.dat052903
History.dat053003

History.dat060203 ]
..
..
..
..

Delete Files
History.dat060903
..

My requirement is , on every monday i need to delete the input files those i received week before last week.
i.e in the above case , on 06/09/03 i need to delete files received from 05/26/03 thru 05/30/03.

Hope i'm making myself clear.

How to get the last modified date of particular file OR is there any simple solution for my requirement.

Thanks in Advance.
Raj !!!!!!!!!!


22 REPLIES 22
Pete Randall
Outstanding Contributor

Re: Delete Files

If I understand the question correctly, a find command should do the trick:

find /directory -type f -name History.dat* -mtime +5 -exec rm {} \;


Pete

Pete
Helen French
Honored Contributor

Re: Delete Files

You can do this with "mtime" option of find command:

# find /dir_name -name History.dat* -type f -xdev -mtime +7 -exec rm {} \;

This will delete all files which was not modified for the last 7 days. If you want to include last accessed option too, then add -atime with find command. If you want to print a list of files which were deleted, then inlcude -exec ll {} \; with find command.
Life is a promise, fulfill it!
Dario_1
Trusted Contributor

Re: Delete Files

Hi!!

Try the following:


find /dirname -name History.dat* -type f -mtime +5 -exec rm {} \;

You can add this line to a cleanup/housekeeping script or create a script that contains this line and add the script to the user's crontab.

Regards,

DR
Prabhu_7
Frequent Advisor

Re: Delete Files

$ find /home/dmeload/CMI -name History.dat* -type f -xdev -mtime +7 exec rm {} \;

when i give the above it gives me error saying,
"find: missing conjunction"
Helen French
Honored Contributor

Re: Delete Files

For the last question:

Put the file name - History.dat* in quotes:

# find ..options ..-name 'History.dat*' ..options

That will solve the issue.
Life is a promise, fulfill it!
A. Clay Stephenson
Acclaimed Contributor

Re: Delete Files

you need to enclose the 'History.dat*' in single quotes so that the shell does not expand the '*'.
If it ain't broke, I can fix that.
Pete Randall
Outstanding Contributor

Re: Delete Files

Sorry about that, Raj. I always forget the quotes until it errors on me. And I should have made the mtime 7 instead of 5. But you got the idea.


Pete

Pete
James R. Ferguson
Acclaimed Contributor

Re: Delete Files

Hi:

Using an 'find' expression with a wildcard should be quoted to prevent the shell from expanding it:

# find /home/dmeload/CMI -name "History.dat*" -type f -xdev -mtime +7 exec rm {} \;

Regards!

...JRF...
Prabhu_7
Frequent Advisor

Re: Delete Files

$ find /home/dmeload/CMI -name 'History.dat*' -type f -xdev -mtime +7 exec rm {} \;

$ find /home/dmeload/CMI -name "History.dat*" -type f -xdev -mtime +7 exec rm {} \;

Both throws same error.
:-(

"find: missing conjunction"
Helen French
Honored Contributor

Re: Delete Files

Again, are you using exec or -exec ? From your posts, it looks like you are using exec with minus (-) sign.
Life is a promise, fulfill it!
Prabhu_7
Frequent Advisor

Re: Delete Files

FYI...

$ uname -a
HP-UX dmeserv B.11.00 U 9000/899 1459612391 unlimited-user license

Is anything to do with this ?
Pete Randall
Outstanding Contributor

Re: Delete Files

Raj,

You need a minus sign in front of "exec":

-exec rm {} \;


Pete

Pete
Helen French
Honored Contributor

Re: Delete Files

Oops.! Correction on my last post. "It look like you are using exec WITHOUT minus (-) sign." You should use - sign woth exec option.

Also, the uname output doesn't have ny impact on your find command. What type of shell are you in?
Life is a promise, fulfill it!
Prabhu_7
Frequent Advisor

Re: Delete Files

Nopes...

I'm not using -exec

This is how i'm giving,


$ find /home/dmeload/CMI -name 'History.dat*' -type f -xdev -mtime +7 exec rm {} \;

$ find /home/dmeload/CMI -name "History.dat*" -type f -xdev -mtime +7 exec rm {} \;
A. Clay Stephenson
Acclaimed Contributor

Re: Delete Files

in addition to the quotes, you need -exec rather than just exec.

A man find would tell you all of this.
If it ain't broke, I can fix that.
Pete Randall
Outstanding Contributor

Re: Delete Files

You need -exec.


Pete

Pete
Dario_1
Trusted Contributor
Solution

Re: Delete Files

Raj:

Just add the minus (-) sign before the exec.

Regards,

DR
Prabhu_7
Frequent Advisor

Re: Delete Files

Thanks all...

-exec solved the issue.

Clay Stephenson: I'm a new bee to unix..also i needed to solve this in quick time.. so didnt have time to look at manuals...please dont mind...

anyways....thanks all.

Meet you all with next query
;-)
James R. Ferguson
Acclaimed Contributor

Re: Delete Files

Hi Raj:

With regard to "manuals", whether or not you're new to Unix or old friends with it, the man pages are one of the greatest sources of information at your disposal. Don't ever *not* take a minute to look.

Regards!

...JRF...
Pete Randall
Outstanding Contributor

Re: Delete Files

Actually, Raj, in this case, the man page has this example almost literally - it probably would have been faster. I quite often do a man on something and then search for the examples by using "/EXAMPLES". Frequently, the examples can be more helpful than wading through the whole man page.


Pete

Pete
Dario_1
Trusted Contributor

Re: Delete Files

Hi Again!!

Another way to get information (after checking the man pages) is to do a search in the forum. You will see the amount of information you'll find in the forums and most of the time you will understand them better than the man pages.

Welcome to the UNIX world.

Regards,

DR
Garry Ferguson
Frequent Advisor

Re: Delete Files

Raj,
I think a cron job that does a simple "find" should do it. If they are all in a directory called fred, say, then something like
find /fred -name Hist\* -ctime +7 -print -exec rm {} \;

You'd better experiment without the "-exec rm {} \;"
to make sure you're getting the files you want.

Regards,
Garry Ferguson