2

I want to create a file with the output of a grep search but in a folder included in the search. If I do:

grep -rnw . -e "text" > test/files.txt

It never ends because it always add one more files.txt. How can I do it after it finishes?

grep -rnw . -e "text" >| test/files.txt

This gives the same output than the previous one. Any idea?

llrs
  • 3,308
  • 35
  • 68

2 Answers2

1

Use parent directory:

grep -rnw . -e "text" > ../files.txt && mv ../files.txt test/files.txt

This, however, relies on relative path which may fail in some contexts. Use static independent path (for example, /tmp) to avoid that.

Alma Do
  • 37,009
  • 9
  • 76
  • 105
  • Your first answer worked fine: `grep -rnw . -e "text" > test/files.tmp && mv test/files.tmp test/files.txt`. I didn't think about the temporal files. – llrs Feb 25 '14 at 13:21
  • 1
    @Llopis it will fail if you need to parse all files (i.e. without some mask). That's why I've updated – Alma Do Feb 25 '14 at 13:25
0

Also you can try this:

cat <<< "$(grep -rnw . -e "text")" > test/files.txt
Малъ Скрылевъ
  • 16,187
  • 5
  • 56
  • 69