LinuxQuestions.org
Help answer threads with 0 replies.
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
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,695
Blog Entries: 5

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

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
Or eventually
Code:
diff -rq dir_1 dir_2 | grep Only
 
Old 12-26-2008, 07:43 PM   #6
billymayday
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,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
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,245
Blog Entries: 15

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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 29 09-10-2012 11:29 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


All times are GMT -5. The time now is 05:23 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration