[SOLVED] How to compare only the file names between two directories? (diff?)
Linux - GeneralThis 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.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
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.
9d8 <= Delete line 9 from source 1 (the info would have appeared at line 8 in source 2)
109,113d107 <= Delete lines 109 to 113 from source 1 (the info would have appeared at line 107 in source 2)
118,121d111 <= Delete lines 118 to 121 from source 1 (the info would have appeared at line 111 in source 2)
131,134d120 <= Delete lines 131 to 134 from source 1 (the info would have appeared at line 120 in source 2)
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
Another way is to just pass the results of the ls command in two files, then compare them, something like:
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.
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.
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,