Operating System - HP-UX
1826480 Members
4322 Online
109692 Solutions
New Discussion

Re: How do I create a script to Purge all files in a Directory except for the latest 100???

 
Shaun Aldrich
Frequent Advisor

How do I create a script to Purge all files in a Directory except for the latest 100???

How do I create a script to Purge all files in a Directory except for the latest 100??? I am currently administoring an HP-UX 10.20 server and need to create a script which will delete any files greater than 100.

A DBA who I work with is looking for a script that inside a directory will only keep that latest 100 files. I guess the database he works with continually fills up the directory.

Unfortunately, I am lacking in knowledge of writing scripts and if anyone has any ideas how to do this, has done any or can get me started I would greatly appreciate it.

Thanks! You can email me at SAldrich@chaptersinc.com
9 REPLIES 9
CHRIS_ANORUO
Honored Contributor

Re: How do I create a script to Purge all files in a Directory except for the latest 100???

Include this line in your script:
find . -mtime +100 -ok rm {} \;
When We Seek To Discover The Best In Others, We Somehow Bring Out The Best In Ourselves.
RikTytgat
Honored Contributor

Re: How do I create a script to Purge all files in a Directory except for the latest 100???

Hi,

Try this:

=====begin=====
nr_of_files=$(ll -l /your/directory | grep ^- | wc -l)
set $(ls -art1 /your/directory)

while [ $nr_of_files -gt 100 ]
do
if [ -f $1 ]
then
rm $1
let nr_of_files=nr_of_files-1
fi
shift
done
=====end======

This should remove all but 100 regular files in the directory /your/directory.

Bye,
Rik
Shaun Aldrich
Frequent Advisor

Re: How do I create a script to Purge all files in a Directory except for the latest 100???

Thanks Rick and Chris for the reply. To clarify this better I am looking for more of a script which will delete all but the latest 100 files in the directory according to timestamp.

I am not very good at all at creating scripts so any help is greatly appreciated!!!

Thanks, Shaun Aldrich

You can email me at SAldrich@chaptersinc.com
CHRIS_ANORUO
Honored Contributor

Re: How do I create a script to Purge all files in a Directory except for the latest 100???

Include this line in your script:
find . -mtime +100 -ok rm {} \;
When We Seek To Discover The Best In Others, We Somehow Bring Out The Best In Ourselves.
Shaun Aldrich
Frequent Advisor

Re: How do I create a script to Purge all files in a Directory except for the latest 100???

Thanks Chris... Is that the only line I would need in the script? I really appreciate your help.

Shaun Aldrich
CHRIS_ANORUO
Honored Contributor

Re: How do I create a script to Purge all files in a Directory except for the latest 100???

The command is for files modified more than hundred days ago. mtime is the modification time. Read the online manual for find.
When We Seek To Discover The Best In Others, We Somehow Bring Out The Best In Ourselves.
wyan lowe
Frequent Advisor

Re: How do I create a script to Purge all files in a Directory except for the latest 100???

maybe could try:

x=$(ls -Ft /path/path/path | grep -v "/$" | tail +100)

for i in $(echo "$x")
do
rm ${i}
done

probably should test this.....don't know if it works right
Ray Ward
New Member

Re: How do I create a script to Purge all files in a Directory except for the latest 100???

Yet another removal script.
Try this........
#! /bin/sh
# ---------------------------------
# Removes all but the last two
# files in the redo diretory
# ---------------------------------
X=1
for Files in `ls -t /path/your_file_spec_here.* `
do
# -------------------------------
# Test for your upper limit here
# -------------------------------
if test $X -gt 2
then
echo "Removing File $Files $X"
# Put your removal command here
else
echo $Files not removed.
fi
let X=X+1
done
To err is human. To realy c**k things up you need a computer!
RikTytgat
Honored Contributor

Re: How do I create a script to Purge all files in a Directory except for the latest 100???

Shaun,

That's exactly what my script does.

The command 'ls -art1' gives a 1 column list of all files in the current directory, ordered from oldest ( at the top) to youngest (at the bottom). So, by deleting all files but 100 starting from the beginning of that list, you end up with the 100 youngest file in that directory.

Right?

Bye,
Rik.