Need script to output difference in folders


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need script to output difference in folders
# 1  
Old 02-03-2012
Need script to output difference in folders

Hey everyone,

I have two folders (folder1, folder2). Folder2 is a compiled version of a bunch of other folders including folder1. I want to compare folder1 to folder2 to make sure that folder2 contains all of the contents of folder1. If it does not, I would like the script to output what folders are missing from folder1 in folder2 so I may add them to folder2.

I need to do this with several folders so a script to make this happen will save a me a LOT of time. Someone please help me out here.

Thank you!
# 2  
Old 02-03-2012
One way: You can list contents of each of the dir in a temp file using find and then diff the temp files.
Suppose there are two dir test1 and test2, that you want to compare.

test1 contains
Code:
user@ubuntu:~/programs/test1$ ls -l
total 16
drwxrwxr-x 4 user user 4096 2012-02-04 05:54 ./
drwxrwxr-x 4 user user 4096 2012-02-04 06:00 ../
drwxrwxr-x 2 user user 4096 2012-02-04 05:52 t1/
drwxrwxr-x 2 user user 4096 2012-02-04 05:54 t2/
-rw-rw-r-- 1 user user    0 2012-02-04 05:54 test2_input
-rwxr--r-- 1 user user    0 2012-01-30 05:28 test_input

test2 contains
Code:
user@ubuntu:~/programs/test2$ ls -l
total 12
drwxrwxr-x 3 user user 4096 2012-02-04 05:53 ./
drwxrwxr-x 4 user user 4096 2012-02-04 06:00 ../
drwxrwxr-x 2 user user 4096 2012-02-04 05:53 t1/
-rwxr--r-- 1 user user    0 2012-02-04 05:53 test_input

Code:
user@ubuntu:~/programs$ find ./test1/ | cut -d'/' -f3- >> tmp1
user@ubuntu:~/programs$ find ./test2/ | cut -d'/' -f3- >> tmp2
user@ubuntu:~/programs$ diff tmp1 tmp2
2d1
< t2
5d3
< test2_input

# 3  
Old 02-03-2012
A better solution: process substitution
Code:
diff <(ls -1p /path/to/dir1) <(ls -1p /path/to/dir2)

note: 1 is number 1, not letter l
# 4  
Old 02-04-2012
This works and it doesn't at the same time. It does display the difference between the two folders; however, I only need it to display the what folder2 does Not contain from folder1.

Since folder2 contains a compilation of different folders, there will surely be a difference of a lot of items. I am looking for only what folder2 doesn't have from folder1. So I will need something that will display the difference only of what folder1 has that folder2 doesn't; not the other way around.

Any ideas?
# 5  
Old 02-04-2012
List the directories to tmp files and use comm to compare the tmp files and print what is unique to the first file:

Code:
ls folder1 >/tmp/t1
ls folder2 >/tmp/t2
comm -23 /tmp/t[12]
rm /tmp/t[12]

---------- Post updated at 12:04 ---------- Previous update was at 12:00 ----------

You could also use the more terse syntax:

Code:
comm -23 <(ls folder1) <(ls folder2)

# 6  
Old 02-04-2012
Only directories in bash/ksh93:
Code:
comm -23 <(cd folder1;printf "%s\n"  */) <(cd folder2;printf "%s\n"  */)

-or-
Code:
cd folder1
for i in */; do 
  [ -d "/pathto/folder2/$i" ] || printf "%s\n" "$i"
done


Last edited by Scrutinizer; 02-04-2012 at 02:02 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Difference in awk output and while

so, im going over one of my scripts and trying to optimize it. i have a code like this: cksum sjreas.py | awk '{prinnt $1$2}' This does what I need. However, i dont want to call the external command awk. so im doing this: cksum sjreas.py | while OFS=' ' read v1 v2 ; do printf... (4 Replies)
Discussion started by: SkySmart
4 Replies

2. Shell Programming and Scripting

How do you compare two folders and copy the difference to a third folder in a remote server?

How do you compare one local folder and a remote folder and copy the difference to a third folder in a remote folder.e.g. Folder A -- Is in a remote server and it has the following files TEST1.OUT TEST2.OUT TEST3.OUT Folder B --Is in a local server and it has the following files ... (5 Replies)
Discussion started by: cumeh1624
5 Replies

3. Shell Programming and Scripting

Difference output of files

Need help on below req Compare two files and send difference of file to other file File2 is static which never changes ex: File1 A.txt B.ttx C.txt E.txt File2 A.txt (6 Replies)
Discussion started by: satish1222
6 Replies

4. Shell Programming and Scripting

Gawk output difference

Why the below option2 doesn't fetch similar output as option1 ? I am on linux. $cat test 2013-01-01-00.25.43.643845 Option1: cat test | gawk -F"-" ' {print $2 " " $3 " " $1}' 01 01 2013 Option2: cat test | gawk '{FS="-"} {print $2 " " $3 " " $1}' 2013-01-01-00.25.43.643845 (5 Replies)
Discussion started by: Shivdatta
5 Replies

5. Shell Programming and Scripting

Script to compare 2 files and prints difference as output sidebyside

Hi All, Am trying script to compare 2 files and print the difference found from old file to new file on line by line basis on side by side display. Basically line by line comparision and files may contain blank line as well I know we have compare/diff commands but i don't how to make... (10 Replies)
Discussion started by: Optimus81
10 Replies

6. Solaris

Difference in date output

HiCan anyone tell me why I am getting a difference in the date format on 2 different Solaris servers?On one I get: -Monday, 9 November 2009 09:02:45 GMTand the other: -Monday November 9 09:03:05 GMT 2009Both servers are running OS Version M-11/16/88iCan anyone tell me why one uses a "," and the... (5 Replies)
Discussion started by: steadyonabix
5 Replies

7. HP-UX

Difference in netstat -a and -an output.

Hi, Does anyone know why I get a different output when using "netstat -a" or "netstat -an" ?? # netstat -a | grep ts15r135 tcp 0 0 nbsol152.62736 ts15r135.23211 ESTABLISHED # netstat -an | grep 172.23.160.78 tcp 0 0 135.246.39.152.51954 ... (4 Replies)
Discussion started by: ejdv
4 Replies

8. UNIX for Dummies Questions & Answers

Compare Files and Output Difference

I have to compare two files for any differences, then output the lab and question number for any differences. This is what I currently have: diff lab2.txt lab2answer.txt > lab2compare.txt Though the output doesn't have to be sent to a .txt (or any sort of log), I found that easier, at least... (2 Replies)
Discussion started by: Joesgrrrl
2 Replies

9. UNIX for Dummies Questions & Answers

output difference in two files

Hi, Please help me, I have two files. I need to output the difference of contents of each file in another file. For example, I need to know the content of the file1 that does not exist on file2 and vice versa. Please take note that the size of the files are large. How can I do it using unix... (4 Replies)
Discussion started by: ayhanne
4 Replies

10. UNIX for Dummies Questions & Answers

How to output the difference of two files?

Hi, I had two data file (File1, File2), each one just have one column, but two file were very big. File2 is smaller, all its data included in File1. I want to ouput the result which don't have any data in File2. Could any one give me a help on how to do that? Thanks in advance! Yun ... (4 Replies)
Discussion started by: yxiao
4 Replies
Login or Register to Ask a Question