LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 09-16-2013, 04:46 PM   #1
SparceMatrix
Member
 
Registered: Aug 2002
Distribution: SME Server, CentOS
Posts: 212

Rep: Reputation: 30
How to compare only the file names between two directories? (diff?)


I have seen diff and this gives me a report on the contents of the files in two directories when they are the same.

I am not interested in the actual content of the files in two directories. I want to know the names only of files that are in one directory but not the other. I have tried,

Code:
diff -q /my/directory/ /my/other/directory
and this gives me a comparison between the file contents where the files are the same. I don't want this. All I want is a list of files in one directory that are not matched by file name in the other.
 
Old 09-16-2013, 05:22 PM   #2
suicidaleggroll
Senior Member
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 2,670

Rep: Reputation: 956Reputation: 956Reputation: 956Reputation: 956Reputation: 956Reputation: 956Reputation: 956Reputation: 956
I don't know of a command that will do this for you, but it's easy enough to do in a loop

Code:
for i in /my/directory/*; do
   name=$(basename "$i")
   if [[ ! -e "/my/other/directory/$name" ]]; then
      echo $name not found in other directory
   fi
done
You could turn it into your own program that pulls the two directories off of the command line arguments and put it in your own bin folder if this is something you'll be using often.

Last edited by suicidaleggroll; 09-16-2013 at 05:24 PM.
 
1 members found this post helpful.
Old 09-16-2013, 05:24 PM   #3
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,123

Rep: Reputation: 151Reputation: 151
I'm not on a box where I can check my syntax, but something like this should work:
Code:
diff <(ls /my/directory) <(ls /my/other/directory)
 
1 members found this post helpful.
Old 09-18-2013, 12:15 AM   #4
jonjgc
LQ Newbie
 
Registered: Nov 2009
Posts: 17

Rep: Reputation: 6
If you have a Unix system nearby, it includes a shell script called "dircmp".
The first part of its output is a list of files in one directory only.
The rest show if the common filenames have the same or different content.

I've seen the dircmp source on several websites; complete with the AT&T copyright.

For just what you are seeking, consider the "comm" command. Try:

$ comm <(ls dir1) <(ls dir2)

The output will be differentiated by 0, 1, or 2 leading tabs as:

files only in dir1
files only in dir2
files in both dirs

Options to comm allow selection of the columns you want.
 
1 members found this post helpful.
Old 09-20-2013, 05:58 PM   #5
SparceMatrix
Member
 
Registered: Aug 2002
Distribution: SME Server, CentOS
Posts: 212

Original Poster
Rep: Reputation: 30
Thanks all for replying. I am currently working on something and will have to be back to address this in the coming days.
 
Old 09-22-2013, 04:27 PM   #6
SparceMatrix
Member
 
Registered: Aug 2002
Distribution: SME Server, CentOS
Posts: 212

Original Poster
Rep: Reputation: 30
It works!

gilead and jonjgc, I tried

Code:
diff <(ls /my/directory) <(ls /my/other/directory)
and got the following information,

Code:
9d8
< 120223SC.htm
109,113d107
< 130211ED.htm
< 130212IT.htm
< 130213ED.htm
< 130214ED.htm
< 130215ED.htm
118,121d111
< 130226IT.htm
< 130227ED.htm
< 130228ED.htm
< 130301ED.htm
131,134d120
< 130423ED.htm
< 130424ED.htm
< 130425ED.htm
< 130426IT.htm
I think I understand that the "<" means that the file was in /my/directory, but not in /my/other/directory.

But what about the numbers "ABC, CDEdFGH" where the caps represent integers? What do they mean?

What I want to do is delete the files I found. Is there a quick fix for that? I have "Linux in a Nutshell" and I promise to look it up in the Bash shell section.
 
Old 09-22-2013, 10:13 PM   #7
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,123

Rep: Reputation: 151Reputation: 151
I'm using info from here. My comments are inline below. Also, diff -u provides some context about the diff which may help.

Quote:
Originally Posted by SparceMatrix View Post
gilead and jonjgc, I tried

Code:
diff <(ls /my/directory) <(ls /my/other/directory)
and got the following information,

Code:
9d8 <= Delete line 9 from source 1 (the info would have appeared at line 8 in source 2)
< 120223SC.htm
109,113d107 <= Delete lines 109 to 113 from source 1 (the info would have appeared at line 107 in source 2)
< 130211ED.htm
< 130212IT.htm
< 130213ED.htm
< 130214ED.htm
< 130215ED.htm
118,121d111 <= Delete lines 118 to 121 from source 1 (the info would have appeared at line 111 in source 2)
< 130226IT.htm
< 130227ED.htm
< 130228ED.htm
< 130301ED.htm
131,134d120 <= Delete lines 131 to 134 from source 1 (the info would have appeared at line 120 in source 2)
< 130423ED.htm
< 130424ED.htm
< 130425ED.htm
< 130426IT.htm
I think I understand that the "<" means that the file was in /my/directory, but not in /my/other/directory.

But what about the numbers "ABC, CDEdFGH" where the caps represent integers? What do they mean?

What I want to do is delete the files I found. Is there a quick fix for that? I have "Linux in a Nutshell" and I promise to look it up in the Bash shell section.
Once you're happy with your diff output, you can pipe it through sed to format it and use that output to loop through and delete the files. I'm at work at the moment so I can't provide an example yet

Last edited by gilead; 09-22-2013 at 10:15 PM.
 
1 members found this post helpful.
Old 09-23-2013, 01:38 AM   #8
jonjgc
LQ Newbie
 
Registered: Nov 2009
Posts: 17

Rep: Reputation: 6
gilead described your results very well. I'll just extend that reply.

diff outputs how to go from file1 to file2. Deletions (the "d" in 131,134d120)
are not the only possible change. You might also get "a" or "c" standing for
append (add) and change respectively.
 
1 members found this post helpful.
Old 09-23-2013, 01:44 PM   #9
SparceMatrix
Member
 
Registered: Aug 2002
Distribution: SME Server, CentOS
Posts: 212

Original Poster
Rep: Reputation: 30
Thanks gilead for that superb reference. I'll take it as far as I can from here. Thanks jonjgc for your reply.
 
Old 09-23-2013, 02:18 PM   #10
Pap
Member
 
Registered: May 2011
Distribution: Debian GNU/Linux, testing version, 64-bit
Posts: 66

Rep: Reputation: 19
Another way is to just pass the results of the ls command in two files, then compare them, something like:
Code:
ls -la ./dir1 > foo1
ls -la ./dir2 > foo2
diff foo1 foo2
But do yourself a favor and use a better tool for comparing files/directories. Emacs' ediff and vim's vimdiff are both excellent tools for comparing. At least ediff can also compare directories directly, without needing to use the above trick or scripts. Other open-source tools, such as Diffuse, can also be used for that purpose.
The following image shows Emacs comparing two directories. It highlights the differences and it is obviously much easier to read than diff.
Attached Images
File Type: png Emacs_compare.png (38.0 KB, 14 views)

Last edited by Pap; 09-23-2013 at 02:44 PM.
 
Old 09-23-2013, 02:41 PM   #11
SparceMatrix
Member
 
Registered: Aug 2002
Distribution: SME Server, CentOS
Posts: 212

Original Poster
Rep: Reputation: 30
More complications

I need to apply this comparison to a series of directories. I have matching directories like this example where the sequence of matching directories start with number "1":

Code:
/my/directory/1/Files1

/my/other/directory/1/another/directory/Files2
And so I have to work my way through a sequence of directories 1, 2, 3, ..., n in each of the two base directories /my/directory/ and /my/other/directory/ and compare the list of Files1 and Files2. In the Files2 directory, /another/directory/ is always the same directories between /1/ and /Files2.

I am adept at working through directories in perl and I wonder if at this point perl is my best solution. I was hoping a single command might do it, but we are getting a little deep into bash for my purposes. Perl is how I got these directories in the first place. It could be that suicidaleggroll is closer to what I need, but then I might as well go back to perl.

Ideas? Opinions?
 
Old 09-24-2013, 01:39 PM   #12
SparceMatrix
Member
 
Registered: Aug 2002
Distribution: SME Server, CentOS
Posts: 212

Original Poster
Rep: Reputation: 30
Problem Solved

I have found another solution that applies what I've learned here. I had a list of the files on a database and copied the files into a text file, then compared the directory with the text file. So it was something like,

Code:
diff <(ls /my/directory) /my/directory/ListOfOtherDirectory.txt
Many thanks to all who showed up.
 
  


Reply

Tags
diff


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Using diff to Compare Directories LXer Syndicated Linux News 0 12-02-2011 02:50 PM
How to compare timestamp of same file residing in two diff directories prasenjit.patra AIX 1 10-21-2009 04:45 PM
Recursive diff ( File compare ) script sharathkv25 Programming 5 02-22-2007 08:39 AM
mass file compare or diff mijohnst Linux - Software 11 01-27-2006 06:32 AM
Using diff to compare file with common lines, but at different line numbers jimieee Linux - Newbie 3 05-10-2004 07:26 AM


All times are GMT -5. The time now is 02:47 PM.

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