Operating System - HP-UX
1833776 Members
2068 Online
110063 Solutions
New Discussion

deletion of files in single stroke

 
abhijeet_7
Advisor

deletion of files in single stroke

Hello gurus,
i have a directory wherein system puts thousands of files everyday ..now i have above 5000 files in that directory containing files of 'nov','dec','jan'...
currently we are facing space problem...
so
can anyone tell me how can i delete files of 'nov' & 'dec' in a single stroke ?

i thought of following:
for file in `ls -lrt|grep 'dec'|awk '{print $9}'`
do
rm -fr $file
done

is this correct?

Will find command be of any help? if yes,how?

thanks all,
regards

ABHI k
we work, to become...not acquire
10 REPLIES 10
Henk Geurts
Esteemed Contributor

Re: deletion of files in single stroke

hi ABHI

you might want to use the find command:
first see which files you select with:
# find /dirname -mtime +12 -exec ll {} \;

where you select all files modified 12 days ago or more
and display them.

if oke

then use
# find /dirname -mtime +12 -exec rm {} \;

for removal

good luck.
Henk
Biswajit Tripathy
Honored Contributor

Re: deletion of files in single stroke

If all files are in 1 directory, then:

$ ls -1 | grep "nov|dec" | xargs rm -rf

- Biswajit
:-)
Biswajit Tripathy
Honored Contributor

Re: deletion of files in single stroke

Oops .. I should have used "egrep" instead of
"grep" in my previous message.

$ ls -1 | egrep "nov|dec" | xargs rm -rf

Since egrep is going to be obsolete soon, you
can do the same thing using "grep":

$ ls -1 | grep -e nov -e dec | xargs rm -rf

- Biswajit
:-)
abhijeet_7
Advisor

Re: deletion of files in single stroke

Hi Henk,
thanks for the suggestion,
but i don't want to check the access/modify peroid while deleting the these files.

i must delete them,only problem is that the
count is huge( above 5000)...

i must preserve the files for "january"..
but i need to delete "nov" ,"dec" files..

any comments on the 'for' loop?

regards
ABHI K
we work, to become...not acquire
Henk Geurts
Esteemed Contributor

Re: deletion of files in single stroke

ABHI

the loop looks fine but it removes not only

-r--r--r-- 1 root sys 4585 dec 13 23:59 syslog.log.13-dec-04
but also the files of recent date with nov/dec in the name :
-r--r--r-- 1 root sys 963 jan 13 23:59 deco.log.13-jan-05

so be carefull.

regards.
Peter Godron
Honored Contributor

Re: deletion of files in single stroke

Hi,
the problem with deleting huge number of files only gives a problem if you exceed the max length for the command.
So your approach with a loop is correct as it only deletes one file in each pass.
If you have a certain filename format, wou may want to limit your ls -1 with wildcards.
Michael Schulte zur Sur
Honored Contributor

Re: deletion of files in single stroke

Hi,

you can avoid deleting files which contain dec in the name with
grep '_dec_'
_ is blank

greetings,

Michael
Chris Vail
Honored Contributor

Re: deletion of files in single stroke

Have you considered compressing your files as opposed to deleting them?
for FILE in `ls -1 *Nov*`
do
echo $FILE
gzip $FILE
done


Chris
Cesare Salvioni
Trusted Contributor

Re: deletion of files in single stroke

hi,
i too think that find would be the better way.
Anyway if you want a loop in order to choose in some strange way, eg. deleting all files except March and June, I suggest you to avoid building a long argument list as in ur example, instead use a pipeline.

To do that find a command that write on stdout the names you want to delete and pipe this command with:
while read fname; do rm -f $fname; done

for instance

ll | awk '$6 != "Mar" && $6 != "Jun" {print $9}' | while read fname; do rm -f $fname; done

the awk should (check it please) print names of files with date not in March or June, the while loops for all this names using one for every loop and so avoiding the possible problem of having too much args on command line

hope it helps
Gordon  Morrison
Trusted Contributor

Re: deletion of files in single stroke

To get around the problem that Henk pointed out, try this:

for file in *-dec-*
do
rm -f $file
done
What does this button do?