-   Programming (
-   -   Shell Script Compare Folders (

corteplaneta 12-26-2008 07:05 PM

Shell Script Compare Folders

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 :)


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?


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

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


ls sorts by default - from man ls


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


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

no need for sed:

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

konsolebox 09-18-2010 01:16 AM

You can also try this one:

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.

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.