Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

remove files selectively

0 views
Skip to first unread message

Matthew Tan

unread,
Oct 14, 1999, 3:00:00 AM10/14/99
to
Hello,

I am looking for a way to remove temp files based on month. I have tried
using the find command but it seems that there are no command for this
requirement.

I would like to first search based on month then removed it.

Something like ls -lt | grep Nov, then rm this files.

Any ideas ?

Thanks in advance !

Real

unread,
Oct 14, 1999, 3:00:00 AM10/14/99
to
Matthew Tan wrote;
# Hello,
#
# I am looking for a way to remove temp files based on month. I have tried
# using the find command but it seems that there are no command for this
# requirement.
#
# I would like to first search based on month then removed it.
#
# Something like ls -lt | grep Nov, then rm this files.
#
# Any ideas ?

for fn in $(find /temp -ls | grep ' Nov ' | awk '{print $11}')
do
rm $fn
done

And, of course, you can use variables for the starting directory, month,
etcetera.

Good luck,
Real

Kurt J. Lanza

unread,
Oct 14, 1999, 3:00:00 AM10/14/99
to
Matthew Tan wrote:

> Hello,


>
> I am looking for a way to remove temp files based on month. I have tried

> using the find command but it seems that there are no command for this

> requirement.


>
> I would like to first search based on month then removed it.
>

> Something like ls -lt | grep Nov, then rm this files.
>

You started out right, why did you stop? Pipe your output to something that
just leaves the filename. Pipe that to a while loop that does the remove.
Sheesh.

Christopher J. Mattern

unread,
Oct 14, 1999, 3:00:00 AM10/14/99
to

Why not

rm $(find /temp -ls | grep ' Nov ' | awk '{print $11}')

?

Or, if you're afraid there will be too many files for rm to handle in one go:

find /temp -ls | grep ' Nov ' | awk '{print $11}' | xargs rm

Chris Mattern

Peter Sundstrom

unread,
Oct 15, 1999, 3:00:00 AM10/15/99
to

Matthew Tan wrote in message <7u496r$cml$1...@violet.singnet.com.sg>...
>Hello,

>
>I am looking for a way to remove temp files based on month. I have tried
>using the find command but it seems that there are no command for this
>requirement.

>
>I would like to first search based on month then removed it.
>
>Something like ls -lt | grep Nov, then rm this files.


rm -f *`date +%b`*

Pete Houston

unread,
Oct 15, 1999, 3:00:00 AM10/15/99
to
Christopher J. Mattern <sys...@gwis2.circ.gwu.edu> wrote in comp.unix.shell:

|Why not
|
|rm $(find /temp -ls | grep ' Nov ' | awk '{print $11}')
|
|?

Why not

rm $(find /temp -ls | awk '/ Nov / {print $11}')

?

Pete

--
J.J. Thomson Physical Laboratory | Email: p.h.r....@reading.ac.uk
PO Box 220, Whiteknights, Reading, | Phone: +44-118-9875123 ext 7594
Berkshire, RG6 6AF, United Kingdom | Fax: +44-118-9750203
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
WWW: http://www.rdg.ac.uk/~spr96phh/pete.html Use lynx - you know you want to!


Christopher J. Mattern

unread,
Oct 15, 1999, 3:00:00 AM10/15/99
to
Pete Houston <pe...@sppc75.reading.ac.uk> wrote:
> Christopher J. Mattern <sys...@gwis2.circ.gwu.edu> wrote in comp.unix.shell:
> |Why not
> |
> |rm $(find /temp -ls | grep ' Nov ' | awk '{print $11}')
> |
> |?

> Why not

> rm $(find /temp -ls | awk '/ Nov / {print $11}')

> ?

<Hangs head in shame> Yer right. Filtering with grep & then
using awk is a no-no. I should know that.

Chris Mattern

James A. Robinson

unread,
Oct 18, 1999, 3:00:00 AM10/18/99
to comp.un...@list.deja.com
> >I would like to first search based on month then removed it.
> >
> >Something like ls -lt | grep Nov, then rm this files.
>
>
> rm -f *`date +%b`*

I think he means he wants to delete a file that shows the last modified
date as being in a certain month. Not that the file has the month name
in it.

So his example of ls -lt was on the right track, though I'm not sure
I see why he would really needs the timestamp sort. Perhaps something
like

ls -l | grep Nov | awk '{print $9}' | xargs rm

or to be safer

ls -l | awk '{print $6" "$9}' | egrep '^Nov ' | cut -d' ' -f2 | xargs rm

with the warning that these won't catch file names with spaces in them.


Jim


Sent via Deja.com http://www.deja.com/
Before you buy.

sweth...@gwu.edu

unread,
Oct 18, 1999, 3:00:00 AM10/18/99
to
In article <1999101814...@highwire.stanford.edu>,

normally, i don't care about useless uses of (fill in your pet
peeve here), but this one is so ugly it hurts. awk is a record parsing
tool that is an almost total superset of both *grep and cut; the middle
three commands can thus all be combined into one:

$ ls -l | awk '$6 ~ /^Nov$/ { print $9 }' | xargs rm

if you are worried about spaces, there are a couple of alternatives,
including using an ls -li to return the inode and pass it to a find
that matches on that inode and does an exec rm, or change the print
command to a printf command in a for loop that iterates from $9 through
$NF (without newlines until the last field is output) and pipe the
output through a `sed 's/./\\\&/g'' to escape all the metachars
(except embedded newlines, unfortunately). but you still don't need
to use all of egrep and cut. (for reference, the spaces in the egrep
are unnecessary, since you anchor the string anyway so it could only
match on the date; similarly, the concatenation of $6 and $9 with a
space can be replaced by a comma, since the default OFS for awk is
a space.)

-- sweth.

--
Sweth Chandramouli ; <sw...@gwu.edu>
<a href="http://astaroth.nit.gwu.edu/resume/">Will Work For Food.</a>
<a href="http://astaroth.nit.gwu.edu/~sweth/disc.html">*</a>

James A. Robinson

unread,
Oct 20, 1999, 3:00:00 AM10/20/99
to comp.un...@list.deja.com
> normally, i don't care about useless uses of (fill in your pet
> peeve here), but this one is so ugly it hurts. awk is a record parsing
> tool that is an almost total superset of both *grep and cut; the middle
> three commands can thus all be combined into one:
>
> $ ls -l | awk '$6 ~ /^Nov$/ { print $9 }' | xargs rm

Golly, that's really ugly on the eyes. =) But I guess it's so much more
efficient, it's useful to learn. My knowledge of awk never went beyond
the print() stage.

0 new messages