Need Help with Bash - comparing directory contents with list of files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need Help with Bash - comparing directory contents with list of files
# 1  
Old 01-07-2013
Hammer & Screwdriver Need Help with Bash - comparing directory contents with list of files

Hey guys,

need help with a script I'm trying to write.

Basically I need to compare the contents of a folder called "profiles"
with a list of files called "template".

when the file matches the contents of the folder it needs to set a variable called "checked" to "1"


Cookies to anyone who can help Smilie

thanks in advance
# 2  
Old 01-07-2013
Code:
#!/bin/bash
ls -ltr folder1 > list-of-files-1.txt
ls -ltr folder2 > list-of-files-2.txt
i=1
while read line; do
  if [ "cat list-of-files-1.txt | head -$i | tail -1" = "$line" ]; then
    put-1-wherever-you-want
    i=$i+1
  fi
done < list-of-files-2.txt

Explanation: You should do 2 lists of files. After that, you have to compare those lists by line. If both lists have the same element in the same position, you can put a number 1 wherever you want.

Last edited by Scott; 01-07-2013 at 04:41 PM.. Reason: Code tags
# 3  
Old 01-07-2013
That is Useless Use of Cat. You can simply code:
Code:
checked=1
while read file
do
        [[ ! -f profiles/$file ]] && checked=0; [[ "$checked" -eq 0 ]] && break;
done < template

Note: Replace highlighted with absolute path of dir: profiles
# 4  
Old 01-07-2013
Wow, I didn't knew that. Thanks!
# 5  
Old 01-07-2013
anyway i could work in something to echo an error if the check fails?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Comparing two files and list the difference with common first line content of both files

I have two file as given below which shows the ACL permissions of each file. I need to compare the source file with target file and list down the difference as specified below in required output. Can someone help me on this ? Source File ************* # file: /local/test_1 # owner: own #... (4 Replies)
Discussion started by: sarathy_a35
4 Replies

2. Shell Programming and Scripting

Reading contents of files from a directory

I have a directory with the files, 1st: I want to Diplay each filename, underline it 2nd: Display the contents under the filename and 3rd: Redirect the above content to other file 4th: Remove the files from the directory #!/bin/ksh for i in $( cat $a/b/c.txt ) do echo "... (1 Reply)
Discussion started by: Aditya_001
1 Replies

3. UNIX for Advanced & Expert Users

How to find duplicates contents in a files by comparing other files?

Hi Guys , we have one directory ...in that directory all files will be set on each day.. files must have header ,contents ,footer.. i wants to compare the header,contents,footer ..if its same means display an error message as 'files contents same' (7 Replies)
Discussion started by: Venkatesh1
7 Replies

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

5. Shell Programming and Scripting

Create a list of directory contents

Well I did a search and didn't anything for my specific case. I got a directory with a bunch of text file. All of them have the following pattern on the filename "ABCD_<As of Date>.txt" Example: ABCD_20110301.txt ABCD_20110302.txt ABCD_20110303.txt All I want to accomplish is a Korn... (3 Replies)
Discussion started by: Shark Tek
3 Replies

6. UNIX for Dummies Questions & Answers

Best way to list a directory's contents?

Hey guys! I'm so glad I found this site, I've had so many questions and have been left alone for roughly a year scanning man pages but It's just not quite cutting it for some things. So, I often like to list directories when browsing around my local machine, a friend's machine, or my web... (6 Replies)
Discussion started by: bbilheimer
6 Replies

7. Shell Programming and Scripting

Comparing contents of files

Hi, I hav two files a1.txt and a2.txt, a1.txt contains: --------------- asdev ebcdev .... a2.txt contains: --------------- asdev ebcdev prod .... a1.txt will be updated by a process,.. now i want to compare two files and i want to see data which is not in a1.txt am i clear....?? ... (3 Replies)
Discussion started by: rrs
3 Replies

8. UNIX for Dummies Questions & Answers

list contents of directory

I want to list the contents of a directory, but I do not want to use the ls, is there another way?? (3 Replies)
Discussion started by: carl_vieyra
3 Replies

9. Shell Programming and Scripting

comparing files to contents of a file

Hi I have a problem trying to run a while statement. I have files under one directory that i need to compare to a value in filex and update that file with the result files in the directory are DFC1. DFC5. DFC345. DFC344. DFC9. The program i am trying to run will take the number... (3 Replies)
Discussion started by: SummitElse
3 Replies

10. UNIX for Dummies Questions & Answers

Message trying to list contents of directory

I'm getting this return whenever I try to do anything on a directory root# ls -al /directory ls: .: Value too large to be stored in data type. total 0 I can change directory down two levels but can not list contents of the root of this directory. ANy one seen this? (1 Reply)
Discussion started by: sallender
1 Replies
Login or Register to Ask a Question