LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   KSH script (https://www.linuxquestions.org/questions/linux-newbie-8/ksh-script-809117/)

cheltz 05-20-2010 12:48 PM

KSH script
 
Anyone has any idea how to do script this:

This will keep two directories whose first fields are the same.

I can do that by:

ls | awk -F '_' ' {print $1} '


If there are more than two dir for each field it will delete anything older than 60 days.

I can't figure out how to pick up two or more dir whose $1 look the same and not the rest.

Then I can rm -r exec the older ones by their mtime or atime or whatevertime, I have to look that up.

Any thoughts?

Tinkster 05-20-2010 12:58 PM

What do you mean by pick-up?
I think you need to clarify your requirements.

cheltz 05-20-2010 01:30 PM

I agree feed a man a fish theory. That is why I only need help with this one section. By pick up I mean in a variable. How do I get a count of each individual file whose first fields are different. In other words I need to keep 2 files that are alike and rm the rest that are older by their date stamp. There are many many files with the same name. I only need the two newest one.

Tinkster 05-20-2010 02:01 PM

find / -type f -name "name_*" |xargs ls -tr | tail -n 2


That, or I still don't get what you wanted ...

Cheers,
Tink

cheltz 05-20-2010 02:43 PM

Here's an example:

this is the $ls

en_05062010_1055 ResMorStorefrontAuthor_05062010_1109
en_05172010_0730 ResMorWebCSRSIT_05062010_1101
fr_05062010_1059 ResMorWebCSRSIT_05172010_0741
fr_05172010_0734 Tomcat_Ager.sh



ls | awk -F '_' ' {print $1} ' gives me this so I can sort them by whatever is in front of the _ ( underscore)

en
en
fr
fr
ResMorStorefront
ResMorStorefrontAuthor
ResMorWebCSRSIT
ResMorWebCSRSIT

I need to keep two of the files from each set of files and clip off anything older than the two newer ones.


Thanks in advance

custangro 05-20-2010 03:07 PM

Quote:

Originally Posted by cheltz (Post 3975792)
Here's an example:

this is the $ls

en_05062010_1055 ResMorStorefrontAuthor_05062010_1109
en_05172010_0730 ResMorWebCSRSIT_05062010_1101
fr_05062010_1059 ResMorWebCSRSIT_05172010_0741
fr_05172010_0734 Tomcat_Ager.sh



ls | awk -F '_' ' {print $1} ' gives me this so I can sort them by whatever is in front of the _ ( underscore)

en
en
fr
fr
ResMorStorefront
ResMorStorefrontAuthor
ResMorWebCSRSIT
ResMorWebCSRSIT

I need to keep two of the files from each set of files and clip off anything older than the two newer ones.


Thanks in advance

It's funny when I understand the commands clearer than the question you're asking ;)

This is where I'm getting lost...

Quote:

Originally Posted by cheltz
I need to keep two of the files from each set of files and clip off anything older than the two newer ones.

-C

Tinkster 05-20-2010 03:23 PM

Heh ...

Same ... another stab in the dark:
Code:

#!/bin/bash
for i in $( ls | awk -F_ '{print $1}' | sort -u)
do
  count=$(ls ${i}*|wc -l)
  if [ $count -gt 2 ]; then
    ls ${i}*|sort -tr | head -n $(( count - 2 ))
  fi
done

If you tack an "xargs rm" onto the line with the "head" it
should delete the oldest files.



Cheers,
Tink

grail 05-20-2010 06:45 PM

Well I am going to go on a limb here and say that awk can probably do all of it if I am reading the file names correctly??

Apart from the Tomcat file the rest appear to have a date and time stamp after the initial name of the file so I am presuming
the ls will sort them logically (someone can shoot me down if I am wrong here), so I would use:

Code:

ls | awk -F_ '++_[$1] > 2{print | "rm "$0}'
Just remove the pipe before rm to check if the right names will be removed.

Tinkster 05-20-2010 08:34 PM

Quote:

Originally Posted by grail (Post 3975971)
Well I am going to go on a limb here and say that awk can probably do all of it if I am reading the file names correctly??

That would work if the files had iso-dates in the names, in which
case date == ASCII sort order.

With MMDDYYYY (who dreamt that up, anyway!? ;D) you could have
older files with a higher ASCII sort order than the date value last.



Cheers,
Tink

cheltz 05-21-2010 07:56 AM

thanks guys i appreciate the help

grail 05-21-2010 08:14 AM

@Tinkster
Quote:

With MMDDYYYY (who dreamt that up, anyway!? ;D) you could have
older files with a higher ASCII sort order than the date value last.
While I am the first to say I have never really understood this date ordering, although the US sure likes it,
I was wondering if you could provide an example of where it would not work?
Not doubting you just couldn't think of an example ;)

Tinkster 05-21-2010 03:55 PM

Quote:

Originally Posted by grail (Post 3976568)
@Tinkster


While I am the first to say I have never really understood this date ordering, although the US sure likes it,
I was wondering if you could provide an example of where it would not work?
Not doubting you just couldn't think of an example ;)

Code:

$ touch 04012010
$ touch 05012009
$ touch 05042010
$ touch 06012008
$ ls -1
04012010
05012009
05042010
06012008
$ ls -1r
06012008
05042010
05012009
04012010

Good enough?
No matter whether you sort ASCII or ASCII reverse, you
miss out on one of the 2010 dates.



Cheers,
Tink

Tinkster 05-21-2010 03:57 PM

Quote:

Originally Posted by cheltz (Post 3976544)
thanks guys i appreciate the help

If this is solved can you please mark it as such?



Cheers,
Tink


All times are GMT -5. The time now is 06:15 PM.