LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-26-2008, 07:05 PM   #1
corteplaneta
Member
 
Registered: Jul 2007
Distribution: archlinux, ubuntu, slackware
Posts: 58

Rep: Reputation: 15
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
 
Old 12-26-2008, 07:15 PM   #2
billymayday
LQ Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
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
 
Old 12-26-2008, 07:36 PM   #3
corteplaneta
Member
 
Registered: Jul 2007
Distribution: archlinux, ubuntu, slackware
Posts: 58

Original Poster
Rep: Reputation: 15
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!

Last edited by corteplaneta; 12-26-2008 at 07:41 PM.
 
Old 12-26-2008, 07:37 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
include sorting would be good.
 
Old 12-26-2008, 07:43 PM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Or eventually
Code:
diff -rq dir_1 dir_2 | grep Only
 
Old 12-26-2008, 07:43 PM   #6
billymayday
LQ Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
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.
 
Old 09-17-2010, 10:35 AM   #7
DrLove73
Senior Member
 
Registered: Sep 2009
Location: Srbobran, Serbia
Distribution: CentOS 5.5 i386 & x86_64
Posts: 1,118
Blog Entries: 1

Rep: Reputation: 129Reputation: 129
Quote:
Originally Posted by billymayday View Post
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
 
Old 09-18-2010, 01:16 AM   #8
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
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]

Last edited by konsolebox; 09-18-2010 at 01:21 AM. Reason: forgot '-u' after adding cut; changed single to double quotes to make it compatible with all shells
 
Old 09-18-2010, 01:32 AM   #9
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I compare floating number in a shell script? abefroman Programming 34 10-12-2016 08:09 AM
How to Compare Two files using shell script pooppp Linux - Networking 14 08-05-2008 03:35 AM
shell script: compare 2 files anhtt Programming 6 08-29-2007 02:39 AM
Shell script to compare blocks of strings? bruno buys Programming 10 04-15-2006 02:16 PM
How to compare records in two tables in seperate My Sql database using shell script sumitarun Programming 5 04-14-2005 09:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:00 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration