2

I'm trying to find out which files are present in two compared folders. I'm only interested in comparing filenames.

Adding -s to the command does not do the trick for some reason. I want to reuse the list to delete duplicates and deleting the lines saying "Only in" is a waste of time.

Command:

diff -s /folder1/folder1 /folder2/folder2

Output:

Only in /folder1/folder1: sth.pdf
Only in /folder1/folder1: sth.png
Only in /folder1/folder1: sth.txt
Files /folder1/folder1/sthe.txt and /folder2/folder2/sthe.txt are identical
Only in /folder1/folder1: sth.pdf
Only in /folder1/folder1: sth.png
Only in /folder1/folder1: sth.txt
.
.
.

Any suggestions?

4
  • Use fdupes. – muru Mar 18 '16 at 10:53
  • Compare file names in the directories recursively or only in the given directory? And compare directory names as well or ignore them? – Byte Commander Mar 18 '16 at 11:10
  • doesn't fdupes only compare files in folders within a parent folder? the two folders I'm comparing are in completely different locations. @ByteCommander: directory names don't matter. in this particular case I don't need to go recursive. – treakec Mar 18 '16 at 11:19
  • Does that mean it must not go recursive or it could go recursive but that is not necessary? – Byte Commander Mar 18 '16 at 11:26
2

I'm only interested in comparing filenames.

Then avoid any tool that will compare the content of the files, as it will just slow down the process.

What's occuring to me right now to compare the filenames in the two directories and to output only the common ones is:

comm -12 <(cd dir1; stat -c '%n' *) <(cd dir2; stat -c '%n' *)
  • comm -12 <([...]) <([...]): will compare the output of [...] and [...] output only the lines present in both files;
  • cd dirN; stat -c '%n' * will change the current working directory to dirN and output an ordered list of the filenames of the files in it.
% tree
.
├── dir1
│   ├── file1
│   ├── file2
│   └── file3
└── dir2
    └── file1

2 directories, 4 files
user@user-X550CL ~/tmp % comm -12 <(cd dir1; stat -c '%n' *) <(cd dir2; stat -c '%n' *)
file1

If you need to deal with newlines in filenames, use this instead:

sort -z <(cd dir1; stat --printf '%n\0' *) <(cd dir2; stat --printf '%n\0' *) | uniq -zd | tr '\0' '\n'
  • sort -z <([...]) <([...]): will join and sort the output of [...] and [...];
  • cd dirN; stat -c '%n' * will change the current working directory to dirN and output an ordered list of the filenames of the files in it;
  • uniq -zd: will print only duplicate lines;
  • tr '\0' '\n' will replace NUL characters with newlines.
% tree 
.
├── dir1
│   ├── file1
│   ├── file2
│   └── file3
└── dir2
    └── file1

2 directories, 4 files
% sort -z <(cd dir1; stat --printf '%n\0' *) <(cd dir2; stat --printf '%n\0' *) | uniq -zd | tr '\0' '\n'
file1
4
  • Why cd dir1; stat -c '%n' * and not e.g. ls -A1 dir1? – Byte Commander Mar 18 '16 at 11:07
  • will it print the whole path or just the filenames? – treakec Mar 18 '16 at 11:21
  • @ByteCommander Not sure, I just avoid ls as a rule, though in this case it probably fits better. Thanks, I'll update. – kos Mar 18 '16 at 11:42
  • @treakec As in the output, it will print only the filenames. Though it should be possible to adjust this. – kos Mar 18 '16 at 11:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.