The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Compare 2 list and delete certain names eltinator Shell Programming and Scripting 12 08-22-2007 09:45 PM
Compare file names charbel Shell Programming and Scripting 4 01-31-2007 09:47 AM
Compare 2 directoriesand it's sub directories anikanch UNIX for Dummies Questions & Answers 0 10-28-2005 07:24 AM
Compare 2 Directories seongyin UNIX for Dummies Questions & Answers 2 06-01-2005 09:28 AM
compare directories on two servers MizzGail Shell Programming and Scripting 1 03-24-2003 09:41 PM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-08-2008
Registered User
 

Join Date: Feb 2008
Posts: 8
Question Compare File Names in Different Directories...

I do not know much about shell scripting and need to create a script and I am at a loss. If someone can help me, that would be great!!

I have two directories:

/dir1

/dir2


I need to get the sequence number which is part of the filename in /dir1 and delete all files in /dir2 that are less than that sequence number and have the same ending number extension. Example:


/dir1/1_400_123456


/dir2/1_400_123456
/dir2/1_399_123456
/dir2/1_398_123456
/dir2/1_300_999999




So, I would need to look in the /dir1 directory and see the sequence number is 400. Then, in the dir2 directory, I need to delete any files that are less than 400 and have the same '123456' ending number. So the only files in /dir2 left would be:


/dir2/1_400_123456
/dir2/1_300_999999



Thanks in advance if you can help in anyway!
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 05-08-2008
Moderator
 

Join Date: Feb 2007
Posts: 1,662
Try this script, if you get the expected output you can pipe the output to sh to remove the files.

Code:
#!/bin/sh

cd /dir2

ls /dir1 > tmp1
ls /dir2 > tmp2

awk 'BEGIN {FS=OFS="_"}
NR==FNR{a[$3]=$2;next}
$3 in a{if(a[$3]<=$2){next}}
{print "rm "$0}
' tmp1 tmp2

rm tmp1 tmp2
Regards
Reply With Quote
  #3 (permalink)  
Old 05-09-2008
Registered User
 

Join Date: Feb 2008
Posts: 8
Thanks for your help! It looks like this is almost there. It is listing all of the files that have a sequence number lower than what is found in the first directory. But, it looks like it is not comparing the next node of the file:

/dir1/1_400_123456


/dir2/1_400_123456
/dir2/1_399_123456
/dir2/1_398_123456
/dir2/1_300_999999


The script is listing:

/dir2/1_399_123456
/dir2/1_398_123456
/dir2/1_300_999999


Even though the second node of the file is lower in the /dir2/1_300_999999 file, it should not delete this file because the next node is different.
Reply With Quote
  #4 (permalink)  
Old 05-09-2008
Moderator
 

Join Date: Feb 2007
Posts: 1,662
I looked over something, try this:

Code:
#!/bin/sh

cd /dir2

ls /dir1 > tmp1
ls /dir2 > tmp2

awk 'BEGIN {FS=OFS="_"}
NR==FNR{a[$3]=$2;next}
$3 in a{if(a[$3]>$2){print "rm "$0}
'  tmp1 tmp2

rm tmp1 tmp2
Regards
Reply With Quote
  #5 (permalink)  
Old 05-09-2008
Registered User
 

Join Date: Feb 2008
Posts: 8
Smile

It works! Thank you!!!
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 08:34 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0