LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Shell Script Compare Folders (https://www.linuxquestions.org/questions/programming-9/shell-script-compare-folders-693165/)

corteplaneta 12-26-2008 07:05 PM

Shell Script Compare Folders
 
Hey,

I've attempted to learn shell scripting but haven't found any good resources..does anyone know of a way to compare two folders to see what folders one contains and the other does not?

Any help would be greatly appreciated!!

Thanks :)

-corte

billymayday 12-26-2008 07:15 PM

You could do something very simple like

ls -1 dir_1 > dir_1.txt
ls -1 dir_2 > dir_2.txt
diff dir_1.txt dir_2.txt

corteplaneta 12-26-2008 07:36 PM

Great solution..but how do I exclude all files with .mp3 extension?

Nevermind....

figured this out by...

diff dir_1.txt dir_2.txt > differences

cat differences | grep -v .mp3 > removed_mp3

gave the result I was searching for!

ghostdog74 12-26-2008 07:37 PM

include sorting would be good.

colucix 12-26-2008 07:43 PM

Or eventually
Code:

diff -rq dir_1 dir_2 | grep Only

billymayday 12-26-2008 07:43 PM

ls -1 dir_1 | sed -e '/.*\.mp3/d' > dir_1.txt

etc.

ls sorts by default - from man ls

Code:

List  information about the FILEs (the current directory by default).  Sort entries alphabetically if none of -cftuvSUX
      nor --sort.


DrLove73 09-17-2010 10:35 AM

Quote:

Originally Posted by billymayday (Post 3388092)
ls -1 dir_1 | sed -e '/.*\.mp3/d' > dir_1.txt

no need for sed:
Quote:

ls -1 --hide=*.mp3 dir_1 > dir_1.txt

konsolebox 09-18-2010 01:16 AM

You can also try this one:
Code:

find <first dir> <second dir> -maxdepth 1 -type f -printf "%p\\t%f\\n" | sort | uniq -f 1 -u [| cut -f 1]

konsolebox 09-18-2010 01:32 AM

My mistake. The above is really not working.
Code:

find <first dir> <second dir> -maxdepth 1 -type f -printf "%f\\t%p\\t%f\\n" | sort | uniq -f 2 -u | cut -f 2


All times are GMT -5. The time now is 08:10 PM.