- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- How do I create a script to Purge all files in a D...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-21-2000 08:32 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-21-2000 08:35 AM
тАО08-21-2000 08:35 AM
Re: How do I create a script to Purge all files in a Directory except for the latest 100???
Please define what you mean by "latest". Is this a modification timestamp, or simply the last 100 files in a directory, as ordered by normal collation?
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-21-2000 08:44 AM
тАО08-21-2000 08:44 AM
Re: How do I create a script to Purge all files in a Directory except for the latest 100???
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-21-2000 08:58 AM
тАО08-21-2000 08:58 AM
Re: How do I create a script to Purge all files in a Directory except for the latest 100???
rm `ls -1t | tail +102`
Please test this on some unneeded files first to insure it works as desired!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-21-2000 09:08 AM
тАО08-21-2000 09:08 AM
Re: How do I create a script to Purge all files in a Directory except for the latest 100???
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-21-2000 09:11 AM
тАО08-21-2000 09:11 AM
Re: How do I create a script to Purge all files in a Directory except for the latest 100???
Try something like this:
FILES=`ls -t $MYDIR` #...collect by mod time
COUNT=`echo $FILES | wc -w` #...get how many
let N=COUNT-100 #...keep 100 of them...
FILES=`echo $FILES | cut -f1-$N -d " "`
rm $R
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-21-2000 09:17 AM
тАО08-21-2000 09:17 AM
Re: How do I create a script to Purge all files in a Directory except for the latest 100???
Sorry, the script should look like:
Shaun:
Try something like this:
FILES=`ls -t $MYDIR` #...collect by mod time
COUNT=`echo $FILES | wc -w` #...get how many
let N=COUNT-100 #...keep 100 of them...
FILES=`echo $FILES | cut -f1-$N -d " "` #...list's head
rm $FILES
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-21-2000 09:37 AM
тАО08-21-2000 09:37 AM
Re: How do I create a script to Purge all files in a Directory except for the latest 100???
Is that the only 5 lines I would need in my script? Like I said, unfortunately my scripting ability is very poor right now.
Shaun Aldrich
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-21-2000 09:55 AM
тАО08-21-2000 09:55 AM
Re: How do I create a script to Purge all files in a Directory except for the latest 100???
OK
What about with FOR :
#
for FILE in `ls -t|head -n10`
do
rm $FILE
done
Regards
Victor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-21-2000 10:02 AM
тАО08-21-2000 10:02 AM
Re: How do I create a script to Purge all files in a Directory except for the latest 100???
Other than defining $MYDIR to that of your choosing, yes, the script is complete as written. You could define MYDIR as, for instance MYDIR=/tmp/log* to match all files beginning with "log" in the /tmp.
My suggestion is but one solution. Welcome to UNIX where there is always more than one way to do anything!!!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-21-2000 10:04 AM
тАО08-21-2000 10:04 AM
Re: How do I create a script to Purge all files in a Directory except for the latest 100???
Thanks for the response. In the first line of the FOR LOOP what is the second word FILE specified for? Is it a variable?
Will this delete all of the files in the directory except for the latest 100 in terms of timestamp?
Your help is greatly appreciated...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-21-2000 10:20 AM
тАО08-21-2000 10:20 AM
Re: How do I create a script to Purge all files in a Directory except for the latest 100???
I was on the phone while writing the first post and got confused with what I was asked by phone...
Of course not my script would have erased the last 100..
Since you want to keep the last 100 there is the solution that James put down for you:
1) count first to know how many files to be removed
2) remove them.
Other solutions exists but James has an elegant one...
Best regards
Victor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-21-2000 10:41 AM
тАО08-21-2000 10:41 AM
Re: How do I create a script to Purge all files in a Directory except for the latest 100???
for FILE in `ls |grep -v "$(ls -t|head -n100)"`
do
echo $FILE
done
James, I want your comments, what do you think of this?
Best regards
Victor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-21-2000 10:43 AM
тАО08-21-2000 10:43 AM
Re: How do I create a script to Purge all files in a Directory except for the latest 100???
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-21-2000 10:48 AM
тАО08-21-2000 10:48 AM
Re: How do I create a script to Purge all files in a Directory except for the latest 100???
Victor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-21-2000 10:52 AM
тАО08-21-2000 10:52 AM
Re: How do I create a script to Purge all files in a Directory except for the latest 100???
Where do you put the rm command within the four lines of the statement you just sent me? All files but the latest 100 must be deleted in the directory. Does the rm command go in after that 4 line statement?
Thanks for your help Victor and others....
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-21-2000 10:56 AM
тАО08-21-2000 10:56 AM
Re: How do I create a script to Purge all files in a Directory except for the latest 100???
echo $FILE
You should put rm $FILE
Best regards
Victor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-21-2000 10:59 AM
тАО08-21-2000 10:59 AM
Re: How do I create a script to Purge all files in a Directory except for the latest 100???
Victor's solution is an elegant solution too.
Shaun, you might want to add some logic to exit if there are less than 100 files. In total, a recap of my solution is as follows. Variables are in UPPERCASE.
MYDIR=/tmp/mylog* #...whatever...
FILES=`ls -t $MYDIR` #...collect by mod time
COUNT=`echo $FILES | wc -w` #...get how many
if (( COUNT < 100 ))
then
exit 0
fi
let N=COUNT-100 #...keep 100 of them...
FILES=`echo $FILES | cut -f1-$N -d " "` #...list's head
rm $FILES
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-21-2000 11:07 AM
тАО08-21-2000 11:07 AM
Re: How do I create a script to Purge all files in a Directory except for the latest 100???
will work even if there are less than 100 files.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-21-2000 11:10 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-21-2000 11:34 AM
тАО08-21-2000 11:34 AM
Re: How do I create a script to Purge all files in a Directory except for the latest 100???
Just a quick question for you James. In the last response you answered I noticed that in the following line -
FILES=`echo $FILES | cut -f1-$N -d " "` #...list's
in the cut portion you have -d. What does that do? If you get a chance what exactly does this whole line do?
I really like the logic that you put into this. I will have to learn this a lot better.
One other question is why do you have the head statement by itself above the rm?
Does it not need any options? Look forward to hear from you. Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-22-2000 12:17 AM
тАО08-22-2000 12:17 AM
Re: How do I create a script to Purge all files in a Directory except for the latest 100???
Your solution is a nice one liner..
For our friend why not try to fit it in a script using while since our friend wants to learn the art of scripting
Regards
Victor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-23-2000 07:36 AM
тАО08-23-2000 07:36 AM
Re: How do I create a script to Purge all files in a Directory except for the latest 100???
cd (To directory)
ls -lrt|head -100 > ttt
ms=`ls -l|cat ttt|cut -c59-90`
set -hk $ms; nn=`echo $#`; n=1
while [ $n -le 2 ]
do
me=`ls -l|cat ttt|cut -c59-90`
rm -i $me
n=`expr $n+1`
shift 1
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-23-2000 09:38 AM
тАО08-23-2000 09:38 AM
Re: How do I create a script to Purge all files in a Directory except for the latest 100???
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-23-2000 10:26 PM
тАО08-23-2000 10:26 PM
Re: How do I create a script to Purge all files in a Directory except for the latest 100???
If you want to delete by timestamp, for instance everything but yesterday or everything but the last 2 days, you can use this:
find / -mtime +n -name '/dir/*' -print -exec rm {} ;
where +n = number of days back.
Between the quotes you can give the name or extension.