0

I am running a file search command with ag -g foo and I'd like to see the contents of the files I find.

If I do ag -g foo | xargs more then the more command makes me press the spacebar to advance between each file, even though they are small enough that I could see more than one at a time in my terminal window.

If I do ag -g foo | xargs cat then I see everything at once, but the files are mushed together so it's hard to see where a new file starts.

Is there a way to see the files all in one scroll, but with a heading for each one?

joachim
  • 7,687

1 Answers1

2

You could tell xargs to add a header:

ag -g foo | xargs sh -c '
    for file do
        printf "====== %s =====\n" "$file"
        cat -- "$file"
    done' sh

Or, you could use a loop:

ag -g foo | 
    while IFS= read -r file; do 
        printf '======== %s =======\n' "$file" 
        cat -- "$file"
    done | less

(here assuming ag outputs one filename per line as opposed to in the very specific format expected by xargs).

terdon
  • 242,166