Compare the file names from a file to a directory


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Compare the file names from a file to a directory
# 1  
Old 12-22-2011
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 different file names seperated by comma and each file names should be compared with the file names available in a unix directory for its presence. if the file is present with this name, script will be success or it should fail. eg., based on the above mentioned data, a file called ABC should be in a directory and similarly DEF, RTY and URE.
and there can be one file or can be more than one. eg., it can only be ABC or ABC and RTY or ABC, DEF, RTY,URE.

Can someone please help me with this?

Thanks
Vijay
# 2  
Old 12-22-2011
Code:
#!/bin/sh

if ! [ -f "File1" ]
then
        echo "Data file doesn't exist" >&2
        exit 1
fi

IFS=" ," # Split awk's output on spaces and commas.
# in awk, match line beginning with 'File Names', print stuff after :, then quit
for FILE in $(awk -F: '/^File Names/ { print $2 ; exit }' File1)
do
        if ! [ -f "$FILE" ]
        then
                echo "$FILE missing" >&2
                exit 1
        fi
done

exit 0

# 3  
Old 12-22-2011
thank you very much Corona688.

this works perfect.. can you please tell me how to write the error message to a file? i want to capture the $FILE missing message to a file...

---------- Post updated at 07:46 PM ---------- Previous update was at 07:21 PM ----------

I figured it out.. i have created a file and appended error message to that file.

thanks!
# 4  
Old 12-23-2011
One more way ..
Code:
nawk -F: '/File Names:/{gsub(/,/," ",$2);print "ls -ltr "$2}' infile | sh
[ $? -eq 0 ] && echo "Success" || echo "Failure"

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Renaming the file names in a directory

Hi, I have about 60 files in a directory and need to rename those files. For example the file names are i_can_phone_yymmdd.txt (where yymmdd is the date. i.e 170420 etc) i_usa_phone_1_yymmdd.txt i_eng_phone_4_yymmdd.txt The new file names should be phone.txt phone_1.txt phone_4.txt I am... (4 Replies)
Discussion started by: naveed
4 Replies

2. 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

3. 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

4. UNIX for Dummies Questions & Answers

List Directory names which have the file

Hi All, Can any one help me to list out the directory names which contain the specified file. See for example File name : file.201307014.LKT Have the directory structure as below. /app/work/data/INDIA/file.201307014.LKT /app/work/data/AMERICA/file.201307014.KTP... (5 Replies)
Discussion started by: Balasankar
5 Replies

5. 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

6. Shell Programming and Scripting

Grepping file names, comparing them to a directory of files, and moving them into a new directory

got it figured out :) (1 Reply)
Discussion started by: sHockz
1 Replies

7. Shell Programming and Scripting

Searching for file names in a directory while ignoring certain file names

Sun Solaris Unix Question Haven't been able to find any solution for this situation. Let's just say the file names listed below exist in a directory. I want the find command to find all files in this directory but at the same time I want to eliminate certain file names or files with certain... (2 Replies)
Discussion started by: 2reperry
2 Replies

8. AIX

find for specific content in file in the directory and list only file names

Hi, I am trying to find the content of file using grep and find command and list only the file names but i am getting entire file list of files in the directory find . -exec grep "test" {} \; -ls Can anyone of you correct this (2 Replies)
Discussion started by: madhu_Jagarapu
2 Replies

9. Shell Programming and Scripting

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... (4 Replies)
Discussion started by: stky13
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