1837095 Members
2455 Online
110112 Solutions
New Discussion

Removes files

 
SOLVED
Go to solution
intp
Frequent Advisor

Removes files

Hi,

How can i delete files those are greater than 30 days old

Thanks
8 REPLIES 8
Ivan Ferreira
Honored Contributor
Solution

Re: Removes files

Use:

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

Be carefull when you specify the time, if you specify:

-mtime 30 - Exactly 30 days ago
-mtime -30 - From 30 days ago until now (dangerous)
-mtime +30 - 30 or more days ago (correct)
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
A. Clay Stephenson
Acclaimed Contributor

Re: Removes files

And to be precise, you are actually removing files that have not been modified in 30 days; UNIX has no concept of a creation time for a file and if you happen to know it, you only know it by accident.
If it ain't broke, I can fix that.
intp
Frequent Advisor

Re: Removes files

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

should i always give complete path ?

1) what if i'm in current drectory and
want to delete files those are older than 30 days in current drectory ? Still i need to specify full path name ?

2) what is the usage of " -type f ", how can i delete only *.log , i dont have manual for find in my system.

HELP THANKS.
V. Nyga
Honored Contributor

Re: Removes files

Hi,

1) what if i'm in current directory and
want to delete files those are older than 30 days in current drectory ? Still i need to specify full path name ?

No, you can do a 'find .' - note: find always also search directories under this dir. I not yet seen an option so find doesn't do that.

2) what is the usage of " -type f ", how can i delete only *.log , i dont have manual for find in my system.

'-type f' ensures that you don't erase directories
To delete *.log:
find /path -mtime +30 -type f -name "*.log" -exec rm {} \;

To be sure that you don't delete wrong files, you can do a
find /path -mtime +30 -type f -name "*.log" -exec ll {} \;
first.

HTH
Volkmar
*** Say 'Thanks' with Kudos ***
F Verschuren
Esteemed Contributor

Re: Removes files

there are 3 oitings in find, maks sure you got the right one....

-atime n True if the file access time subtracted from
the initialized time is n-1 to n multiples of
24 h. The initialization time shall be a time
between the invocation of the find utility and
the first access by that invocation of the find
utility to any file specified by its path
operands. The access time of directories in
pathname_list is changed by find itself.

-mtime n True if the file modification time subtracted
from the initialization time is n-1 to n
multiples of 24 h. The initialization time
shall be a time between the invocation of the
find utility and the first access by that
invocation of the find utility to any file
specified in its path operands.

-ctime n True if the time of last change of file status
information subtracted from the initialization
time is n-1 to n multiples of 24 h. The
initialization time shall be a time between the
invocation of the find utility and the first
access by that invocation of the find utility
to any file specified by its path operands.
Tom Henning
Trusted Contributor

Re: Removes files

You might not have the manual pages installed on your system, which in my opinion is a mistake given the low price of disk space these days, but you do internet access otherwise you would not be able to post on these forums. You can always use the manual pages located at the HP documentation site: http://docs.hp.com

For example, the find command (in English) is at:
http://docs.hp.com/en/B2355-90689/find.1.html

Also: please take the time to provide points to those folks who answer your questions. This will help to continue your receiving responses fom the extremely knowledgable folks (not me, I just lurk here mostly) who read this forum.
"I have assigned points to 14 of 86 responses to my questions."
What is it that possesses otherwise sane individuals to change something just because it has not been changed in a while?
intp
Frequent Advisor

Re: Removes files

Quote >>
No, you can do a 'find .' - note: find always also search directories under this dir. I not yet seen an option so find doesn't do that.

<<


but i dont want to delete files in subdirectories..how to limit it to current directory only.

Michael D. Zorn
Regular Advisor

Re: Removes files

As always, whenever you're doing a "find ... rm", run it first with an ll, to see what's going to happen:

find /path -mtime +30 -type f -exec ll {} \;

('ctime' never seems to give the right results)

Giving 'rm' to a command that's going to generate a lot of filenames is always risky.

"find" is designed to be recursive. One thing you might do is run find with ls, send the output to a file

find /path -mtime +30 -type f -exec ls {} \; > flist

Then edit the file to delete subdirectory files, then do something like (in ksh)

for file in $(cat flist)
do
rm $file
done