HPE Community read-only access December 15, 2018
This is a maintenance upgrade. You will be able to read articles and posts, but not post or reply.
Hours:
Dec 15, 4:00 am to 10:00 am UTC
Dec 14, 10:00 pm CST to Dec 15, 4:00 am CST
Dec 14, 8:00 pm PST to Dec 15, 2:00 am PST
System Administration
cancel
Showing results for 
Search instead for 
Did you mean: 

find command for specific date and delete

 
SOLVED
Go to solution
Nazaf Khan
Frequent Advisor

find command for specific date and delete

Hi all,

There is a simple query that I want to find and delete the files older then 30 days from specific folder.Example I want to delete the files before 20 Mar anything in that folder. For that I used the commands.

find /directory -type f -mtime +30 -exec rm {}\;

find /data_yopkg01/gieprd -type f -name "*.*" -mtime +30 -exec rm -f {} +

It deletes the file. But when I check in the folder there any still the files older then 30days.When I check with the command

find /directory -type f -mtime +30 | wc -l

That too shows the difference.

I want to clear the folder what ever before 30 days of file. Can you help me in this please...
12 REPLIES
Kenan Erdey
Honored Contributor

Re: find command for specific date and delete

hi,

it can be the directories remaining after deleting. please note that rm does not delete directories.
Computers have lots of memory but no imagination
Nazaf Khan
Frequent Advisor

Re: find command for specific date and delete

No that's what I got confused. There are files that exist. even if I give rm -r parameter then too there are files remaining.

Acutally it deletes the file but only for 30 days i.e. from today if we count it only deletes the file 19th Mar 2010. But when I do ls -lrt there are files that are of 2009 and 2008 that still exists.

I want to remove the files in this /directory which are more then 30 days older.
James R. Ferguson
Acclaimed Contributor

Re: find command for specific date and delete

Hi Zafar:

An easy solution, is to create a reference point (file) and then delete files older than that:

# touch -amt 03210000 /var/tmp/myref
# find /path -type f ! -newer /var/tmp/myref -exec rm {} +

The "problem" with using '-mtime' or its companions, is that it works in 24-hour increments.

Regards!

...JRF...
Nazaf Khan
Frequent Advisor

Re: find command for specific date and delete

Still now working.. It still shows all the files before 30 March. Although I did not use the delete option I only used find and wc to check it is right or not. With the provided command it shows 22 files. But when I check with the Mar option it shows 42 that'sthe difference.

yficcp01$ touch -amt 03300000 /var/tmp/myref
yficcp01$ find /software/oracle/admin/prd206/udump -type f ! -newer /var/tmp/myref | wc -l
22
y
yficcp01$ pwd
/software/oracle/admin/prd206/udump
yficcp01$ ls -ltr |grep Mar|wc -l
42
yficcp01$ ls -ltr |grep Mar
-rw-rw---- 1 oracle dba 111258 Mar 21 01:48 prd206_ora_14347.trc
-rw-rw---- 1 oracle dba 121264 Mar 23 15:56 prd206_ora_4895.trc
-rw-rw---- 1 oracle dba 89518 Mar 23 21:33 prd206_ora_7511.trc
-rw-rw---- 1 oracle dba 109770 Mar 24 19:48 prd206_ora_11461.trc
-rw-rw---- 1 oracle dba 113164 Mar 26 14:52 prd206_ora_15122.trc
-rw-rw---- 1 oracle dba 99354 Mar 26 17:26 prd206_ora_29659.trc
-rw-rw---- 1 oracle dba 87554 Mar 27 06:29 prd206_ora_10175.trc
-rw-rw---- 1 oracle dba 115700 Mar 27 22:10 prd206_ora_5275.trc
-rw-rw---- 1 oracle dba 72254 Mar 28 10:44 prd206_ora_7137.trc
-rw-rw---- 1 oracle dba 84708 Mar 28 20:22 prd206_ora_28043.trc
-rw-rw---- 1 oracle dba 109614 Mar 28 20:35 prd206_ora_29471.trc
-rw-rw---- 1 oracle dba 36388 Mar 29 03:54 prd206_ora_10735.trc
-rw-rw---- 1 oracle dba 77608 Mar 29 04:35 prd206_ora_15657.trc
-rw-rw---- 1 oracle dba 34300 Mar 29 05:25 prd206_ora_20122.trc
-rw-rw---- 1 oracle dba 126112 Mar 29 06:29 prd206_ora_25559.trc
-rw-rw---- 1 oracle dba 82124 Mar 29 06:29 prd206_ora_25557.trc
-rw-rw---- 1 oracle dba 80558 Mar 29 16:21 prd206_ora_20281.trc
-rw-rw---- 1 oracle dba 81666 Mar 29 18:21 prd206_ora_2440.trc
-rw-rw---- 1 oracle dba 36894 Mar 29 18:43 prd206_ora_4674.trc
-rw-rw---- 1 oracle dba 109614 Mar 29 20:22 prd206_ora_13912.trc
-rw-rw---- 1 oracle dba 77034 Mar 29 20:40 prd206_ora_15972.trc
-rw-rw---- 1 oracle dba 122200 Mar 29 23:30 prd206_ora_2685.trc
-rw-rw---- 1 oracle dba 119246 Mar 30 06:50 prd206_ora_15332.trc
-rw-rw---- 1 oracle dba 131776 Mar 30 08:40 prd206_ora_25727.trc
-rw-rw---- 1 oracle dba 76564 Mar 30 08:41 prd206_ora_25803.trc
-rw-rw---- 1 oracle dba 122144 Mar 30 09:25 prd206_ora_29703.trc
-rw-rw---- 1 oracle dba 88182 Mar 30 11:18 prd206_ora_11977.trc
-rw-rw---- 1 oracle dba 79566 Mar 30 11:30 prd206_ora_13143.trc
-rw-rw---- 1 oracle dba 116636 Mar 30 17:05 prd206_ora_15479.trc
-rw-rw---- 1 oracle dba 115304 Mar 30 17:29 prd206_ora_17213.trc
-rw-rw---- 1 oracle dba 112198 Mar 30 18:26 prd206_ora_22730.trc
-rw-rw---- 1 oracle dba 66070 Mar 30 22:59 prd206_ora_17766.trc
-rw-rw---- 1 oracle dba 94264 Mar 30 23:00 prd206_ora_18287.trc
-rw-rw---- 1 oracle dba 103558 Mar 30 23:06 prd206_ora_18909.trc
-rw-rw---- 1 oracle dba 119298 Mar 31 03:15 prd206_ora_12798.trc
-rw-rw---- 1 oracle dba 76330 Mar 31 04:16 prd206_ora_19494.trc
-rw-rw---- 1 oracle dba 34692 Mar 31 04:26 prd206_ora_20195.trc
-rw-rw---- 1 oracle dba 120656 Mar 31 10:58 prd206_ora_29236.trc
-rw-rw---- 1 oracle dba 35204 Mar 31 11:46 prd206_ora_4379.trc
-rw-rw---- 1 oracle dba 95804 Mar 31 14:21 prd206_ora_19492.trc
-rw-rw---- 1 oracle dba 81080 Mar 31 18:30 prd206_ora_13615.trc
-rw-rw---- 1 oracle dba 110318 Mar 31 22:40 prd206_ora_5562.trc

Re: find command for specific date and delete

>Still not working.

It sure looks like it is.

>With the provided command it shows 22 files. But when I check with the Mar option it shows 42 that's the difference.

Those 20 files are for Mar 30 and 31, which are after the reference file.
Nazaf Khan
Frequent Advisor

Re: find command for specific date and delete

Hi Dennis,

Thanks for the reply. But I belive u did not get the point what my actual requirement is. I want to delete the files or director anything in this particualr folder older then the date 30th March 2010 for which I made a reference file 30th March . Now you said the files that was shown is after the reference file. But still in the March list I can see files of 20th March and older till 30th March and also 31st March too. Can u help me with the command for which I can remove all the files above the date.
Nazaf Khan
Frequent Advisor

Re: find command for specific date and delete

Also one more thing I can use the command rm `ls -l|grep "Mar" |awk '{print $9}'

But here too I can delete the files of particular March month only.

I thing we can get some clues in find command only.
Pete Randall
Outstanding Contributor

Re: find command for specific date and delete

I'm not sure you understand your own requirements! In your example:

yficcp01$ touch -amt 03300000 /var/tmp/myref
yficcp01$ find /software/oracle/admin/prd206/udump -type f ! -newer /var/tmp/myref | wc -l
22
y
yficcp01$ pwd
/software/oracle/admin/prd206/udump
yficcp01$ ls -ltr |grep Mar|wc -l
42


You used the find command to count the files (|wc -l) however you did not remove them. Then you complain that the "ls -ltr |grep Mar" command is still showing them. Why wouldn't it show them since you did not remove them?


Pete

Pete
Nazaf Khan
Frequent Advisor

Re: find command for specific date and delete

Hi Pate, Dennis and James. You all did an Excellent Job. Pate was write I did not try to delete the files and was in the dreams of assumptions. Find commands itself worked fine. I accept my mistake. Excellent Job. This should be rewarded to u all..

Thanks again for the help and will close the thread after assigning points.
James R. Ferguson
Acclaimed Contributor

Re: find command for specific date and delete

HI (again):

I'm glad Pete straightened things out for you.

Regards!

...JRF...
Solution

Re: find command for specific date and delete

>I believe you did not get the point what my actual requirement is.

Sure I did. You wanted to verify that the command would work so you didn't delete the files. And when you mistakenly compared the files, you thought it worked incorrectly.

My point was to go ahead and remove the files.

If you want to assign more points, you can reopen the thread:
http://forums.itrc.hp.com/service/forums/helptips.do?#41
Nazaf Khan
Frequent Advisor

Re: find command for specific date and delete

Hi Dennis I really apprecite all 3 of you for your all quick solution. I did reopen and had assigned pointed again. Strangly before closing the thread I did assign points to all 3 of you. Any how I had assinged more point to u all and closing the thread.

Thanks again for the help.