Saftest way to remove expired files from a given directory

  • Follow


Which one is recommended?


1.

find /tmp/ -mtime +30 -type f -delete

2.

find /tmp/ -mtime +30 -type f  | xargs rm -f

3.

find /tmp/ -mtime +30 -type f  -exec rm {}
0
Reply howachen (515) 10/1/2009 4:08:44 AM

On Thursday 01 October 2009 06:08 in comp.os.linux.misc, somebody
identifying as howa wrote...

> Which one is recommended?
> 
> 
> 1.
> 
> find /tmp/ -mtime +30 -type f -delete
> 
> 2.
> 
> find /tmp/ -mtime +30 -type f  | xargs rm -f
> 
> 3.
> 
> find /tmp/ -mtime +30 -type f  -exec rm {}

Homework assignment again? :p

-- 
*Aragorn*
(registered GNU/Linux user #223157)
0
Reply aragorn (581) 10/1/2009 4:10:57 AM


On 2009-10-01, Aragorn <aragorn@chatfactory.invalid> wrote:
> On Thursday 01 October 2009 06:08 in comp.os.linux.misc, somebody
> identifying as howa wrote...
>
>> Which one is recommended?
>> 
>> 1.
>> 
>> find /tmp/ -mtime +30 -type f -delete
>>
>> [...]
>
> Homework assignment again? :p


*ROTFL*

Aragorn, you're still one of the main reasons i still read the Linux
USENET groups. Spot on as ever. Thanks for spilling my morning coffee
once again. ;-)

-- 
Jon Solberg (remove "nospam" from email address).
0
Reply jon6202 (32) 10/1/2009 6:40:32 AM

On Thursday 01 October 2009 08:40 in comp.os.linux.misc, somebody
identifying as Jon Solberg wrote...

> On 2009-10-01, Aragorn <aragorn@chatfactory.invalid> wrote:
>> On Thursday 01 October 2009 06:08 in comp.os.linux.misc, somebody
>> identifying as howa wrote...
>>
>>> Which one is recommended?
>>> 
>>> 1.
>>> 
>>> find /tmp/ -mtime +30 -type f -delete
>>>
>>> [...]
>>
>> Homework assignment again? :p
> 
> 
> *ROTFL*

:pp

> Aragorn, you're still one of the main reasons i still read the Linux
> USENET groups.

Why thank you, I didn't know I had this many fans on Usenet. :-)

> Spot on as ever.

Well, after a while, one gets to easily spot them, and G/2 as the
NNTP_Posting_Host is a giveaway. ;-)

> Thanks for spilling my morning coffee once again. ;-)

I hope you have a very sturdy keyboard. ;-)  

Speaking of coffee... Hmm...  Good idea... :p

-- 
*Aragorn*
(registered GNU/Linux user #223157)
0
Reply aragorn (581) 10/1/2009 7:08:17 PM

On 2009-10-01, Aragorn <aragorn@chatfactory.invalid> wrote:
> On Thursday 01 October 2009 08:40 in comp.os.linux.misc, somebody
> identifying as Jon Solberg wrote...
>
>> Thanks for spilling my morning coffee once again. ;-)
>
> I hope you have a very sturdy keyboard. ;-)  

Not really. When I look at it I see the letters "Micros.." and then my
vision goes blurry. Coffee tends to help the situation.

-- 
Jon Solberg (remove "nospam" from email address).
0
Reply jon6202 (32) 10/1/2009 7:20:36 PM

howa wrote:

> Which one is recommended?
> 
> 
> 1.
> 
> find /tmp/ -mtime +30 -type f -delete

-delete?  Really?

> 2.
> 
> find /tmp/ -mtime +30 -type f  | xargs rm -f

Why the extra work/processing?

> 3.
> 
> find /tmp/ -mtime +30 -type f  -exec rm {}

Yes.  Don't forget your '\;' and to pass '-f' to 'rm' if 'i' is set.
-- 
Not really a wanna-be, but I don't know everything.
0
Reply sysadmin6 (148) 10/1/2009 8:28:26 PM

howa <howachen@gmail.com> wrote:

>=20
> find /tmp/ -mtime +30 -type f  | xargs rm -f
>=20

What about files with names starting with a minus sign?
What if you've too many files (e.g. 10 megabytes of file names)?

--=20
Andr=C3=A9 Gillibert
0
Reply MetaEntropy.removeThis (91) 10/1/2009 8:40:07 PM

On Thursday 01 October 2009 21:20 in comp.os.linux.misc, somebody
identifying as Jon Solberg wrote...

> On 2009-10-01, Aragorn <aragorn@chatfactory.invalid> wrote:
>
>> On Thursday 01 October 2009 08:40 in comp.os.linux.misc, somebody
>> identifying as Jon Solberg wrote...
>>
>>> Thanks for spilling my morning coffee once again. ;-)
>>
>> I hope you have a very sturdy keyboard. ;-)
> 
> Not really. When I look at it I see the letters "Micros.." and then my
> vision goes blurry.

Ah, I'm using a trusted Cherry G81-3000 here.  They're hard to still
come by - I think the model is no longer being manufactured, but they
are still in stock at various Cherry Corp. branches - but these are
quite good. :-)

> Coffee tends to help the situation. 

Yes, I can imagine that pouring coffee into your keyboard helps you
acquire one from another brand sooner... :p  

(A former coworker of mine also discovered that a Dell PowerEdge doesn't
like the taste of Coca-Cola... :pp)

-- 
*Aragorn*
(registered GNU/Linux user #223157)
0
Reply aragorn (581) 10/1/2009 8:59:16 PM

On Thursday 01 October 2009 22:40 in comp.os.linux.misc, somebody
identifying as André Gillibert wrote...

> howa <howachen@gmail.com> wrote:
> 
>> find /tmp/ -mtime +30 -type f  | xargs rm -f
> 
> What about files with names starting with a minus sign?
> What if you've too many files (e.g. 10 megabytes of file names)?

Those two questions were not part of his homework assignment. ;-)

-- 
*Aragorn*
(registered GNU/Linux user #223157)
0
Reply aragorn (581) 10/1/2009 9:01:56 PM

Andr� Gillibert <MetaEntropy.removeThis@gmail.com> writes:

> howa <howachen@gmail.com> wrote:
>> 
>> find /tmp/ -mtime +30 -type f  | xargs rm -f

....

> What if you've too many files (e.g. 10 megabytes of file names)?

That's what xargs is for.

But newer find does it internally.
"find ... -exec rm -f {} +" or something like this.

Vilmos
0
Reply vilmos2 (252) 10/1/2009 9:19:55 PM

André Gillibert wrote:

> howa <howachen@gmail.com> wrote:
>
>> 
>> find /tmp/ -mtime +30 -type f  | xargs rm -f
>> 
>
> What about files with names starting with a minus sign?

All files found will start with /tmp/ :P

-- 
Huibert
"The Commercial Channel! All commercials all the time.
 An eternity of useless products to rot your skeevy little mind, forever!"
   -- Mike the TV (Reboot)
0
Reply huibert.bol (74) 10/1/2009 9:27:18 PM

Am Donnerstag, 1. Oktober 2009 22:28, Wanna-Be Sys Admin a écrit :

>> find /tmp/ -mtime +30 -type f -delete
> 
> -delete?  Really?

GNU find (extension).

>> find /tmp/ -mtime +30 -type f  | xargs rm -f
> 
> Why the extra work/processing?

Missing '-E' option, and won't work for path names
with [:space:], \, " or '. See,
http://www.opengroup.org/onlinepubs/9699919799/utilities/xargs.html

>> find /tmp/ -mtime +30 -type f  -exec rm {}
> 
> Yes.  Don't forget your '\;' and to pass '-f' to 'rm' if 'i' is set.

It's '-exec command {} +' (invoke command with as many
path names as possible). See,
http://www.opengroup.org/onlinepubs/9699919799/utilities/find.html

-- 
printf -v email $(echo \ 155 141 162 143 145 154 142 162 165 151 \
156 163 155 141 100 171 141 150 157 157 056 143 157 155|tr \  \\\\)
#  Live every life as if it were your last!  #
0
Reply mb148 (28) 10/1/2009 10:05:51 PM

André Gillibert wrote:

> howa <howachen@gmail.com> wrote:
> 
>> 
>> find /tmp/ -mtime +30 -type f  | xargs rm -f
>> 
> 
> What about files with names starting with a minus sign?

find will use the path with the leading forward slash and thus will deal
with any file names starting with a dash.  I.e., it'll rm
-f /tmp/-filename, etc.

> What if you've too many files (e.g. 10 megabytes of file names)?

What if he does?
-- 
Not really a wanna-be, but I don't know everything.
0
Reply sysadmin6 (148) 10/1/2009 10:06:00 PM

Marcel Bruinsma wrote:

> Am Donnerstag, 1. Oktober 2009 22:28, Wanna-Be Sys Admin a écrit :
> 
>>> find /tmp/ -mtime +30 -type f -delete
>> 
>> -delete?  Really?
> 
> GNU find (extension).

And, but why? :-)

>>> find /tmp/ -mtime +30 -type f  | xargs rm -f
>> 
>> Why the extra work/processing?
> 
> Missing '-E' option, and won't work for path names
> with [:space:], \, " or '.

That, too.

>>> find /tmp/ -mtime +30 -type f  -exec rm {}
>> 
>> Yes.  Don't forget your '\;' and to pass '-f' to 'rm' if 'i' is set.
> 
> It's '-exec command {} +' (invoke command with as many
> path names as possible).

Why is that? I didn't get the impression they needed/wanted {} + here.
-- 
Not really a wanna-be, but I don't know everything.
0
Reply sysadmin6 (148) 10/1/2009 10:12:21 PM

Am Freitag, 2. Oktober 2009 00:12, Wanna-Be Sys Admin a écrit :

> Marcel Bruinsma wrote:
> 
>>>> find /tmp/ -mtime +30 -type f  -exec rm {}
>>> 
>>> Yes.  Don't forget your '\;' and to pass '-f' to 'rm' if 'i' is set.
>> 
>> It's '-exec command {} +' (invoke command with as many
>> path names as possible).
> 
> Why is that? I didn't get the impression they needed/wanted {} + here.

Invoking rm 1000 times for 1000 files (that's what happens with \;)
is more expensive then invoking rm 10 times with an argument list
of 100 files each time (when using +). The result is the same, 1000
files removed.

The '{} +' is similar to using xargs, except for the mutilation of file
names by xargs (unless you have GNU find and GNU xargs, which
allow 'find [...] -print0 | xargs -0 rm'; but more efficient with GNU
find would be 'find [...] -delete').

-- 
printf -v email $(echo \ 155 141 162 143 145 154 142 162 165 151 \
156 163 155 141 100 171 141 150 157 157 056 143 157 155|tr \  \\\\)
#  Live every life as if it were your last!  #
0
Reply mb148 (28) 10/1/2009 11:06:03 PM

Marcel Bruinsma wrote:

> Invoking rm 1000 times for 1000 files (that's what happens with \;)
> is more expensive then invoking rm 10 times with an argument list
> of 100 files each time (when using +). The result is the same, 1000
> files removed.

Problem is, if the number of files it'll be told to remove at once is
too long (too many).
-- 
Not really a wanna-be, but I don't know everything.
0
Reply sysadmin6 (148) 10/2/2009 7:12:36 PM

Andr� Gillibert <MetaEntropy.removeThis@gmail.com> writes:

> howa <howachen@gmail.com> wrote:
>
>> 
>> find /tmp/ -mtime +30 -type f  | xargs rm -f
>> 
>
> What about files with names starting with a minus sign?

Not a problem. QED.
Filenames with odd characters is another issue.
0
Reply nospam63 (610) 10/2/2009 8:20:45 PM

Vilmos Soti <vilmos@soti.ca> wrote:
> Andr=C3=A9 Gillibert <MetaEntropy.removeThis@gmail.com> writes:
>=20
> > howa <howachen@gmail.com> wrote:
> >>=20
> >> find /tmp/ -mtime +30 -type f  | xargs rm -f
>=20
> ...
>=20
> > What if you've too many files (e.g. 10 megabytes of file names)?
>=20
> That's what xargs is for.
>=20

That's what "xargs -n 1" is for.
By default, xargs passes all args to a single rm instance.
Most systems have a relatively low limit on env+args size.
IIRC, Linux <=3D 2.6.22 has a 256KB limit and Linux >=3D 2.6.23 is limited =
by the max stack size: 8 megabytes by default, but it may be increased with=
 ulimit.

--=20
Andr=C3=A9 Gillibert
0
Reply MetaEntropy.removeThis (91) 10/2/2009 10:06:01 PM

Wanna-Be Sys Admin <sysadmin@example.com> wrote:
> Andr=C3=A9 Gillibert wrote:
>=20
> > howa <howachen@gmail.com> wrote:
> >=20
> >>=20
> >> find /tmp/ -mtime +30 -type f  | xargs rm -f
> >>=20
> >=20
> > What about files with names starting with a minus sign?
>=20
> find will use the path with the leading forward slash and thus will deal
> with any file names starting with a dash.  I.e., it'll rm
> -f /tmp/-filename, etc.
>=20

Indeed.
But, files with newlines in their names may be a problem. :p

> > What if you've too many files (e.g. 10 megabytes of file names)?
>=20
> What if he does?

Old Linux kernels had a pretty low limit on max args length, but it looks l=
ike latest kernels don't have it anymore.

--=20
Andr=C3=A9 Gillibert
0
Reply MetaEntropy.removeThis (91) 10/2/2009 10:24:59 PM

Andr=C3=A9 Gillibert <MetaEntropy.removeThis@gmail.com> wrote:
> Wanna-Be Sys Admin <sysadmin@example.com> wrote:
> > Andr=C3=A9 Gillibert wrote:
> >=20
> > > howa <howachen@gmail.com> wrote:
> > >=20
> > >>=20
> > >> find /tmp/ -mtime +30 -type f  | xargs rm -f
> > >>=20
> > >=20
> > > What about files with names starting with a minus sign?
> >=20
> > find will use the path with the leading forward slash and thus will deal
> > with any file names starting with a dash.  I.e., it'll rm
> > -f /tmp/-filename, etc.
> >=20
>=20
> Indeed.
> But, files with newlines in their names may be a problem. :p
>=20
Or even spaces since -d '\n' isn't even passed to xargs.

--=20
Andr=C3=A9 Gillibert
0
Reply MetaEntropy.removeThis (91) 10/2/2009 10:28:11 PM

On 2009-10-01, howa wrote:
> Which one is recommended?
>
>
> 1.
>
> find /tmp/ -mtime +30 -type f -delete
>
> 2.
>
> find /tmp/ -mtime +30 -type f  | xargs rm -f
>
> 3.
>
> find /tmp/ -mtime +30 -type f  -exec rm {}

find /tmp/ -mtime +30 -type f  -exec rm {} +


-- 
   Chris F.A. Johnson, author   |    <http://cfaj.freeshell.org>
   Shell Scripting Recipes:     |  My code in this post, if any,
   A Problem-Solution Approach  |         is released under the
   2005, Apress                 |    GNU General Public Licence
0
Reply cfajohnson (1784) 10/3/2009 2:55:05 AM

Am Freitag, 2. Oktober 2009 21:12, Wanna-Be Sys Admin a écrit :

> Marcel Bruinsma wrote:
> 
>> Invoking rm 1000 times for 1000 files (that's what happens with \;)
>> is more expensive then invoking rm 10 times with an argument list
>> of 100 files each time (when using +). The result is the same, 1000
>> files removed.
> 
> Problem is, if the number of files it'll be told to remove at once is
> too long (too many).

That is not possible. Read,
http://www.opengroup.org/onlinepubs/9699919799/utilities/find.html

Snippet:
« An argument containing only the two characters "{}" shall
» be replaced by the set of aggregated pathnames, with each
» pathname passed as a separate argument to the invoked
» utility in the same order that it was aggregated. The size of
» any set of two or more pathnames shall be limited such that
» execution of the utility does not cause the system's
» {ARG_MAX} limit to be exceeded. »

-- 
printf -v email $(echo \ 155 141 162 143 145 154 142 162 165 151 \
156 163 155 141 100 171 141 150 157 157 056 143 157 155|tr \  \\\\)
#  Live every life as if it were your last!  #
0
Reply mb148 (28) 10/3/2009 3:46:57 AM

Am Samstag, 3. Oktober 2009 00:06, André Gillibert a écrit :

>> >> find /tmp/ -mtime +30 -type f  | xargs rm -f
>> 
>> > What if you've too many files (e.g. 10 megabytes of file names)?
>> 
>> That's what xargs is for.
> 
> That's what "xargs -n 1" is for.
> By default, xargs passes all args to a single rm instance.

Please, read
http://www.opengroup.org/onlinepubs/9699919799/utilities/xargs.html

Section DESCRIPTION, first paragraph.

Of course, xargs is still useless in combination with find, but for
a very different reason, described in the second paragraph.

The GNU find and GNU xargs have extensions, to work around
the problems : find [...] -print0 | xargs -0 [...]
 
-- 
printf -v email $(echo \ 155 141 162 143 145 154 142 162 165 151 \
156 163 155 141 100 171 141 150 157 157 056 143 157 155|tr \  \\\\)
#  Live every life as if it were your last!  #
0
Reply mb148 (28) 10/3/2009 3:54:43 AM

André Gillibert wrote:

> Or even spaces since -d '\n' isn't even passed to xargs.

Yeah, if xargs is used.
-- 
Not really a wanna-be, but I don't know everything.
0
Reply sysadmin6 (148) 10/3/2009 8:17:36 PM

23 Replies
68 Views

(page loaded in 0.231 seconds)


Reply: