![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Rules & FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
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! |
| Forum Sponsor | ||
|
|
|
|||
|
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
|
|
|||
|
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. |