Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

linux diff tool output with relative prefix

Tags:

linux

diff

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
like image 299
Robber Pen Avatar asked Sep 18 '26 07:09

Robber Pen


1 Answers

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.

like image 99
randomir Avatar answered Sep 20 '26 21:09

randomir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!