llrs
llrs

Reputation: 3397

Create a file with the grep search in a folder where the search is done

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?

Upvotes: 2

Views: 148

Answers (2)

Малъ Скрылевъ
Малъ Скрылевъ

Reputation: 16507

Also you can try this:

cat <<< "$(grep -rnw . -e "text")" > test/files.txt

Upvotes: 0

Alma Do
Alma Do

Reputation: 37365

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.

Upvotes: 1

Related Questions