bashgit

How to append a pattern to each list member from back quote output?


For example if I do git diff --name-only under a git local directory, the command returns the list of files seen from the git root directory like this.

ab21/ab21_universe.sv
ab21cv/apzeus/aarch64/cvp_earth/hello_world/Makefile.inc
ab21cv/apzeus/aarch64/shared/stackheap.s
ab21cv/apzeus/cvp_category/cvp_earth/hello_world/hello_world.c
ab21cv/apzeus/cvp_category/shared/ab21x_zeus_uart.h

But since I am now in simab21/sim_rtl directory from the git root position, to do something to those files, I have to append ../.. to each members of the output above. For example for the question, if I want to do cat for those files, how should I do it? I can do things like

for i in `git diff --name-only`
do
cat ../../$i
done

But I cannot do ls -ltrd git diff --name-only (characters in the shade is inside back quotes) because the git diff --name-only outputs files lists seen from git root directory. Hope my explanation comes through. How can I do that?


Solution

  • You can use sed to modify the file names:

    ls -ltrd $(git diff --name-only | sed 's=^=../../=')
    

    (Note that I used $(...) instead of back quotes, as they nest cleanly).

    This only works if the file names don't contain spaces.