Operating System - HP-UX
1847432 Members
2477 Online
110264 Solutions
New Discussion

How to modify this simple script???

 
SOLVED
Go to solution
Shaun Aldrich
Frequent Advisor

How to modify this simple script???

Hello everyone,

Currently I have a directory called /home/shaun/boristest. This contains database files which continually grows. There are approximately 200 plus files in it. As a regular maintnance I have been told by the DBA that he only wants to keep the latest 100 files according to timestamp. The below script3 currently removes 100 files. So if there is 300 files then it will remove 100 and there will still be the latest 200. This is close to being correct but does anyone know how I could modify this simple script to only leave the latest 100 and no more. Your help is greatly appreciated...

drwxrwxrwx 2 shaun adm 4096 Aug 23 08:42 boristest
-rwxrwxr-x 1 shaun adm 263 Aug 22 09:22 script1
-rwxrwxr-x 1 shaun adm 84 Aug 22 09:21 script2
-rwxrwxr-x 1 shaun adm 63 Aug 23 08:41 script3
-rwxrwxrwx 1 shaun adm 87 Aug 22 09:26 test2

#!/usr/bin/sh

cd /home/shaun/boristest
rm `ls -t | tail -100`

Thanks
Shaun Aldrich
SAldrich@chaptersinc.com
19 REPLIES 19
Alex Glennie
Honored Contributor

Re: How to modify this simple script???

man tail ;) should to the trick !
Alex Glennie
Honored Contributor

Re: How to modify this simple script???

or try 200 ?
Victor BERRIDGE
Honored Contributor

Re: How to modify this simple script???

You have the solution:
In a previous mail you sent.
Tom's rm `ls -1t | tail +101`
and my rm `ls |grep -v "$(ls -t|head -n100)"`

But didnt pay...
Shaun Aldrich
Frequent Advisor

Re: How to modify this simple script???

Hi Victor/Tom,

Thanks a lot for your reply... I couldn't get either of these to work. Here is a copy of your script Victor which I called script2 and the results when I ran it.

#!/usr/bin/sh

cd /home/shaun/boristest
for FILE in 'ls | grep -v "$(ls -t | head -n100)"'
do
rm $FILE
done


$ ./script2
rm: ls non-existent
rm: | non-existent
rm: -v non-existent
rm: "$(ls non-existent
rm: -t non-existent
rm: | non-existent
rm: head non-existent
rm: -n100)" non-existent

Any help is greatly appreciated...

Shaun Aldrich
Victor BERRIDGE
Honored Contributor

Re: How to modify this simple script???

I guess your next question:
What difference between the 2.
tom: i rm after sorting by time after the 100 first
Victor: i rm all except the result of grep -v

I know my english is hopeless, but I try my best

Victor
Shaun Aldrich
Frequent Advisor

Re: How to modify this simple script???

Hi Victor,

Either script would be good for me if I could get them to work... Thanks for your continous replies...

Shaun Aldrich
Victor BERRIDGE
Honored Contributor
Solution

Re: How to modify this simple script???

Hi Shaun,
You quotes go the wong way I dont know how you call them, you put '
It should be `
(Im in a french speaking world...)
Regards
Victor
Jaroslaw Tomkiewicz
Occasional Advisor

Re: How to modify this simple script???

Dear Shaun

I'd suggest to modify your script as follows

rm $(ls -1t | awk 'NR>100')

regards

Jaroslaw
Shaun Aldrich
Frequent Advisor

Re: How to modify this simple script???

Thanks a lot Victor your script works!!!! I obviously need a class in scripting. Especially since a lot of the work I do is with NNM and IT/Operations...

Thanks again for all of the replies...

Shaun Aldrich
SAldrich@chaptersinc.com
CHRIS_ANORUO
Honored Contributor

Re: How to modify this simple script???

cd /home/shaun/boristest
then do find . -name * -exec +100 ls -latr {} \;
When We Seek To Discover The Best In Others, We Somehow Bring Out The Best In Ourselves.
Shaun Aldrich
Frequent Advisor

Re: How to modify this simple script???

Hi Jaroslaw,

I thought I would try the suggestion that you gave as well and here are the results:

#!/usr/bin/sh

cd /home/shaun/boristest
rm $(ls -1t | awk `NR>100`)

$ ./script4
./script4[4]: NR: not found.
Usage: awk [-F fs][-v Assignment][-f Progfile|Program][Assignment|File] ...
Usage: rm [-Rfir] file ...

Any suggestions on why this failed...

Thanks
Shaun Aldrich
Jaroslaw Tomkiewicz
Occasional Advisor

Re: How to modify this simple script???

Dear Shaun

I suppose that there is the same mistake as previous.
You should use ' instead of `



Regards

Jaroslaw

Jaroslaw
Shaun Aldrich
Frequent Advisor

Re: How to modify this simple script???

Hi Chris,

Thanks for your replies to my questions on the forum. I am going to assign points for you right now. Below is the results from what I just tried after your suggestion:

#!/usr/bin/sh

cd /home/shaun/boristest
find . -name * -exec +100 ls -latr {}

$ ./script5
find: missing conjunction

Do you know what it means by missing conjunction? Thanks for your help.

Shaun Aldrich
Shaun Aldrich
Frequent Advisor

Re: How to modify this simple script???

Hi Jaroslaw,

I did use the proper quotes that you recommended. Any other ideas?

Thanks

Shaun Aldrich
Jaroslaw Tomkiewicz
Occasional Advisor

Re: How to modify this simple script???

Dear Shaun

If I use single quotation mark my command works properly.

[/tmp/jt]# rm $(ls -1t | awk 'NR>1')
[/tmp/jt]# ls
kas.sh
[/tmp/jt]#

Problem arised when I replaced single quotation mark with apostrophe sign:

[/tmp/jt]# rm $(ls -1t | awk `NR>1`)
sh: NR: not found.
Usage: awk [-F fs][-v Assignment][-f Progfile|Program][Assignment|File] ...
Usage: rm [-Rfir] file ...

Regards

Jaroslaw
Peter Scheer
New Member

Re: How to modify this simple script???

I would use:

cd /home/shaun/boristest &&
ls -1t *.dbf | tail +101 | xargs rm

The && after the cd means "run the following command only if the cd succeeds" - this script will eventually end up as a cron-job, maybe run by root - if anybody ever moves or removes /home/shaun/boristest the cd will fail, but whithout the && rm will do its job on other files it finds in the current directory - you do not want that to happen - trust me!

The *.dbf is an example only, be just as specific as possible with the names of the files you want to delete - again just as a precaution.

Shaun Aldrich
Frequent Advisor

Re: How to modify this simple script???

Thanks Jaroslaw,

Your solution works great as well!!! If you get a chance can you explain the following line for me:

rm $(ls -1t | awk 'NR>100')

I not sure what awk or NR do... My understanding of scripting needs to get better...

Thanks
Shaun Aldrich
Jaroslaw Tomkiewicz
Occasional Advisor

Re: How to modify this simple script???

Dear Shaun

According to HP-UX description awk is a "
- pattern-directed scanning and processing language". You can get many details running man awk command.

NR is a special variable which means Number of Records.


regards

Jaroslaw
CHRIS_ANORUO
Honored Contributor

Re: How to modify this simple script???

The attached should be of help.

Cheers!
When We Seek To Discover The Best In Others, We Somehow Bring Out The Best In Ourselves.