Robber Pen
Robber Pen

Reputation: 1093

linux diff tool output with relative prefix

The diff output with file path is too long. I will like to remove the path for readable with diff built-in parameter/option without via shell script ?

$ diff -u /share/a/b/c/d /share/1/2/3/4/             <== path is too long
diff -u /share/a/b/c/d/dirsave /share/1/2/3/4/dirsave
--- /share/a/b/c/d/dirsave      2017-08-11 16:21:50.100547799 +0800
+++ /share/1/2/3/4/dirsave      2017-08-11 16:22:21.612546684 +0800
@@ -1,2 +1,3 @@
 pushd /share/qca6174/qca6174a-5-pci/QCA6174A-5-PCI/fixce/AIO/rootfs-ra-r105.build/lib/firmware
 pushd /share/qca6174/qca6174a-5-pci/QCA6174A-5-PCI
+libc

However, The path is too long for me. I want to get shortly path like as

diff -u dirsave dirsave                                <== enhance shortly path
--- dirsave     2017-08-11 16:21:50.100547799 +0800
+++ dirsave     2017-08-11 16:22:21.612546684 +0800
@@ -1,2 +1,3 @@
 pushd /share/qca6174/qca6174a-5-pci/QCA6174A-5-PCI/fixce/AIO/rootfs-ra-r105.build/lib/firmware
 pushd /share/qca6174/qca6174a-5-pci/QCA6174A-5-PCI
+libc

Upvotes: 3

Views: 1050

Answers (1)

randomir
randomir

Reputation: 18697

It's hard to answer your question because diff utility does not exist in Linux (the kernel). It exists in GNU, and it exists in Unix, because those are operating systems. Linux is a kernel, usually packaged with GNU and distributed as GNU/Linux.

With this being said, if you are using GNU diff (from GNU diffutils), then you can use the --label LABEL option (twice) to specify the alternate names you wish to see printed in place of the file names.

In your example:

$ diff -u --label file1 --label file2 /share/a/b/c/d /share/1/2/3/4/             
--- file1
+++ file2
@@ -1,2 +1,3 @@
 pushd /share/qca6174/qca6174a-5-pci/QCA6174A-5-PCI/fixce/AIO/rootfs-ra-r105.build/lib/firmware
 pushd /share/qca6174/qca6174a-5-pci/QCA6174A-5-PCI
+libc

If you're stuck with POSIX diff, then I'd say your best option is to rewrite that header with sed or awk.

Upvotes: 1

Related Questions