简体   繁体   中英

How to get `diff -qr` to display all files under subdirs?

Let's say this is my file tree:

a
a/file1
a/subdir1
a/subdir1/file1
a/subdir1/file2
b
b/file1

When I run diff -qr ab , it returns the below. It is doing a recursive diff, but it doesn't list the files under the subdir.

Files a/file1 and b/file1 differ
Only in a: subdir1

What I would like to see is the below. Are there any diff options to do this?

Files a/file1 and b/file1 differ
Only in a: subdir1
Only in a: subdir1/file1
Only in a: subdir1/file2

(Note: I am using Ubuntu, GNU diffutils 3.2)

see Given two directory trees, how can I find out which files differ?

It does not appear that diff provides a comparison or output regarding files that exist in one directory and not the other as you are trying to do. As Matt has pointed out below, you must parse the file lists below both a and b outside of diff and call diff to compare those files that exist in both directories and output the names of those that only appear in one directory. It would be nice if there was something like that in diff, but it is essentially asking diff to compare the differences between file_a and nonexistent_file_b. Sorry for the initial misreading.

The reason why you get that output when running diff -qr ab is because the subdir1 is only in a . Because of that, everything under a must be different than in b so it apparently doesn't go through and list all the files. I haven't found a way for it to list all the files regardless. I put together another command that will hopefully help!

My file structure:

Matt@MattPC ~/perl/testing/10
$ find .
.
./a
./a/file1
./a/subdir1
./a/subdir1/file1
./a/subdir1/file2
./b
./b/file1

The diff command you ran (same output):

Matt@MattPC ~/perl/testing/10
$ diff -q -r a b
Only in a: subdir1

Here's a command I wrote that will hopefully get you what you need. Basically, it calls find a and find b and pipes the output to diff to compare them. However, we have to pipe that to sed 's/^[^/]\\+//' to remove the name of the first directory, in this case a and b .

Matt@MattPC ~/perl/testing/10
$ diff <(find a | sed 's/^[^/]\+//') <(find b | sed 's/^[^/]\+//')
3,5d2
< /subdir1
< /subdir1/file1
< /subdir1/file2

Hope this helps! Let me know if the command needs tweeking.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM