Re: Want to find duplicate file entries in two directories - by filename only



On 2007-05-10, Robert Glueck wrote:
Vilmos Soti wrote:
Robert Glueck <rglk@xxxxxx> writes:

I have two directories, dir A (6000 entries) and dir B (1000 entries).
Some of the entries in dir B are duplicates of entries in dir A (by
name only, creation dates and sizes differ). I want to find out which
of the entries in dir B are duplicated in dir A and then selectively
delete the duplicates in dir B. How would I do this?

ls /dir1 /dir2 | sort | uniq -d

And now you have your list. You might want to ensure that each file
is really a file.

Vilmos

Thanks, Vilmos. This works as well as and is more compact
than Joe Beanfish's suggestion. I amended it slightly and ran

ls /dir1 /dir2 | sort | uniq -d >/tmp/dups.lst

I wind up with a text file dups.lst that lists all of the
duplicate files in dir1. I now want to move all of these
files to a directory (e.g. /tmp/dups) from which I can
delete them all.

Hence, I need a shell script that opens the file dups.lst
(which contains nothing but filenames, separated by carriage
returns) and parses the list line by line, executing
something like

mv filename /tmp/dup

for every item in the list. That should be very simple.
Unfortunately, I'm totally ignorant of bash shell scripting.

while IFS= read file
do
: do whatever with "$file"
done < /tmp/dups.lst

--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
.



Relevant Pages