tyrande
tyrande

Reputation: 27

Display folder that is being searched

Is there any way to display a current folder/file that is being searched while grep -r is performing? That would give me a sense of how much work is already done.

Upvotes: 1

Views: 54

Answers (2)

Diomidis Spinellis
Diomidis Spinellis

Reputation: 19345

You can run grep under strace to obtain all open system calls. An invocation of sed can then select the calls that open directories and print the directory name. I've redirected the printing of the directory names to the standard error, so that it will not interfere with grep's normal output.

strace -o >(sed -n 's/^openat([^,]*, "\([^"]*\)".*O_DIRECTORY.*/\1/p' 1>&2) -s 1024 -e trace=openat grep -r foo .

Upvotes: 0

hek2mgl
hek2mgl

Reputation: 158060

You can use find:

find -type f -print -exec grep foo {} +

PS: To separate search results from normal output, you may use this:

find -type f -print -exec bash -c 'grep class "${@}" | tee -a result.txt' -- {} +

In addition to get printed on screen - along with the filenames, the search results will be stored in result.txt

Upvotes: 1

Related Questions