How can I pipe files from grep to open all matching files?
I've tried grep -li "type" * | open
, but that does not work.
There is many ways to do this (using find/exec, xargs,...), one could be :
grep -li "type" * | while read file; do echo "processing $file"; done
IFS=$'\n'; for i in $(grep -rli "type" *); do open "$i"; done
The IFS is necessary, so that it won't split filenames containing whitespaces.
Use while read line loop:
| while read line
If
$ grep -l something *.py
file1.py
file2.py
Then
$ grep -l something *.py | while read line ; do open "$line" ; done
Is equivalent to
$ open "file1.py"
$ open "file2.py"
Using quotes like "$line"
instead of just $line
allows for it to match filenames that contain spaces, otherwise filenames with spaces will cause errors.
Notice that line
is just a commonly-used name for a variable in this usage. It could just as easily be
$ grep -l something *.py | while read potato ; do open "$potato" ; done
emacs
in sequence? Open them all inemacs
at the same time?