LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-20-2010, 01:48 PM   #1
cheltz
Member
 
Registered: Apr 2010
Posts: 62

Rep: Reputation: 2
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?
 
Old 05-20-2010, 01:58 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
What do you mean by pick-up?
I think you need to clarify your requirements.
 
Old 05-20-2010, 02:30 PM   #3
cheltz
Member
 
Registered: Apr 2010
Posts: 62

Original Poster
Rep: Reputation: 2
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.
 
Old 05-20-2010, 03:01 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

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


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

Cheers,
Tink

Last edited by Tinkster; 05-20-2010 at 03:02 PM.
 
Old 05-20-2010, 03:43 PM   #5
cheltz
Member
 
Registered: Apr 2010
Posts: 62

Original Poster
Rep: Reputation: 2
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
 
Old 05-20-2010, 04:07 PM   #6
custangro
Senior Member
 
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , RHEL
Posts: 1,971
Blog Entries: 1

Rep: Reputation: 208Reputation: 208Reputation: 208
Quote:
Originally Posted by cheltz View Post
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
 
Old 05-20-2010, 04:23 PM   #7
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
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
 
Old 05-20-2010, 07:45 PM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,423

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
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.
 
Old 05-20-2010, 09:34 PM   #9
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by grail View Post
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

Last edited by Tinkster; 05-20-2010 at 09:37 PM.
 
Old 05-21-2010, 08:56 AM   #10
cheltz
Member
 
Registered: Apr 2010
Posts: 62

Original Poster
Rep: Reputation: 2
thanks guys i appreciate the help
 
Old 05-21-2010, 09:14 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,423

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
@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
 
Old 05-21-2010, 04:55 PM   #12
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by grail View Post
@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
 
Old 05-21-2010, 04:57 PM   #13
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by cheltz View Post
thanks guys i appreciate the help
If this is solved can you please mark it as such?



Cheers,
Tink
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Ksh Script Help sharathkv25 Programming 10 02-02-2007 11:28 AM
help w/ksh script ShaqDiesel Programming 2 01-12-2007 03:04 PM
ksh math script SeT Programming 10 10-13-2004 09:06 AM
Can somebody help me with a ksh script? twentymil AIX 7 01-21-2004 10:55 AM
KSH script AquamaN Programming 2 12-08-2003 12:34 PM


All times are GMT -5. The time now is 10:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration