Compare File Names in Different Directories...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare File Names in Different Directories...
# 1  
Old 05-08-2008
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!
# 2  
Old 05-08-2008
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
# 3  
Old 05-09-2008
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.
# 4  
Old 05-09-2008
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
# 5  
Old 05-09-2008
Bug

It works! Thank you!!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare Only "File Names" in 2 Files with file lists having different directory structure

I have a tar arcive arch_all.tar.gz and 4 batched tar archive . These batches are supposed to have all the files form arch1.all.tar.gz arch1_batch1.tar.gz arch1_batch2.tar.gz arch1_batch3.tar.gz arch1_batch4.tar.gz my issue is that the directory structure in "arch_all.tar.gz" is... (6 Replies)
Discussion started by: sumang24
6 Replies

2. Shell Programming and Scripting

Compare files with different names in different directories

Hi, I have a requirement to compare files in different directories with different names. The files have a datestamp in their name (It might not be a sequential datetimestamp). This is for Redhat Linux. I have more than 5 directories and more than 10 file in each directory to be compared. ... (4 Replies)
Discussion started by: GosarJunk
4 Replies

3. Shell Programming and Scripting

Comparing the pattern of the file names in 2 different directories

Hi, I have got a requirement for which i need your help. The following problem is required to get solved in PERL SCRIPT. Here is the requirement. There are 4 folders say SRC_DIR1, SRC_DIR2 and TGT_DIR_1,TGT_DIR_2 (Note: both path of SRC_DIR1 & SRC_DIR2 are different but both path of... (4 Replies)
Discussion started by: shadow_fawkes
4 Replies

4. Shell Programming and Scripting

**URGENT ** : Comparing the pattern of the file names in 2 different directories

Hi, I have got a requirement for which i need your help. The following problem is required to get solved in PERL SCRIPT. Here is the requirement. There are 4 folders say SRC_DIR1, SRC_DIR2 and TGT_DIR_1,TGT_DIR_2 (Note: both path of SRC_DIR1 & SRC_DIR2 are different but both path of... (1 Reply)
Discussion started by: shadow_fawkes
1 Replies

5. Shell Programming and Scripting

Compare file names on directory

Dears, Would you please help on following bash script: I want to get the most recent file named alfaYYYYMMDD.gz in one directory: for example: in directory /tmp/ ls -ltr alfa20130715.gz holding.gz alfa20130705.gz sart.txt merge.txt.gz alfa20130802.gz my result shoud be... (1 Reply)
Discussion started by: maxsub
1 Replies

6. Shell Programming and Scripting

Check for particular files and compare the file names

Hi, Below are the 2 files in directory /tmp: masterCSF242323.img indexCSF242323.img 1) I want to compare if both the number (242323) are same in both the files. If they are same print - Files matching, else print files do not match. 2) Also if only index file is present in that... (7 Replies)
Discussion started by: apatil65
7 Replies

7. UNIX for Dummies Questions & Answers

Compare the file names from a file to a directory

Hi, I have to acheive the below logic in the script. I have a file which has many columns and one of them is "File Names" and there will be only one such column name. Eg., cat File1 Number of files:10 File Names: ABC, DEF, RTY,URE .... ... ... The column will contain the... (3 Replies)
Discussion started by: Vijay81
3 Replies

8. Shell Programming and Scripting

Compare file names and select correct elements to include in "for each loop"

Hi everyone, I`ll try to be most clear I can explaining my help request. I have 2 folders Folder A-->This folder receives files through FTP constantly Folder B-->The files from Folder A are unzipped and then processed in Folder B Sometimes Folder A doesn`t contain all... (2 Replies)
Discussion started by: cgkmal
2 Replies

9. Shell Programming and Scripting

Compare File Differences in different directories

Hello, I am new to scripting and have been trying to compare two different directories, but with all the same file names in each directory for file changes. I have been doing it in baby steps and have been doing pretty good, but I have hit a few snags. Test 1 and Test 2 work great, but my... (4 Replies)
Discussion started by: dmaday
4 Replies

10. Shell Programming and Scripting

Compare file names

Hi everyone, How to compare between two filenames, in case the current filename is the same as the last one an alarm to be sent, in case the current filename is different from the last filename, no alarm to be sent. Is there a way to do this? Thanks in advance. (4 Replies)
Discussion started by: charbel
4 Replies
Login or Register to Ask a Question