grep -l will get you just the filenames
tree --fromfile will read from stdin just fine
So: grep -ril "foo bar" . | tree --fromfile -a should do the trick.
Edit: removed -n from the grep incantation per below conversation.
grep -l will get you just the filenames
tree --fromfile will read from stdin just fine
So: grep -ril "foo bar" . | tree --fromfile -a should do the trick.
Edit: removed -n from the grep incantation per below conversation.
Combine both grep and tree commands:
grep -rl "find" | tree --fromfile -F
Edit: The rg example didn't work correclty. Not sure what I was testing. But looks like the grep variant works as intended. Please report if its not.
Another Edit, because this is fun: A simple function for your .bashrc (functions are like alias, but allow more complex code, such as arguments):
treegrep() {
# grep:
# --recursive like --directories=recurse
# --files-with-match print only names of FILEs with selected lines
# tree:
# --fromfile Reads paths from files (.=stdin)
# -F Appends '/', '=', '*', '@', '|' or '>' as per ls -F.
grep --recursive --files-with-match "${@}" |
tree --fromfile -F
}
I don't understand what you're asking: you want some kind of graphical display of the file structure? Grep per se doesn't do that, but maybe you could match the output against "tree" output? I generally just use M-x grep in Emacs which doesn't make a tree-like display, but lets me navigate to matched lines by clicking on them.
top 6 comments