0

I have this find command to get all the files modified in the last 50 seconds which matchs with the following regex hell\d in the last 1000 characters. I use tail to get the last 1000 chars in order to speed up the search becouse the files to check are huge (3gb on average).

find /home/ouhma -newermt '50 seconds' -type f |
while read fic; do
    if tail -c 1000 "${fic}" | LANG=C LC_ALL=C grep -Pq 'hell\d'; then
        echo "${fic}"
    fi
done

It is posible to use -exec parameter to replace that ugly loop and to retrieve the result even faster?

This works, but I dont know if its the best way to do it:

find /home/ouhma -newermt '50 seconds' -type f -exec bash -c 'LANG=C LC_ALL=C grep -Pq "hell\d" <(tail -c 1000 "{}") && echo "{}"' \;
0

1 Answer 1

3

Multiple -exec actions can follow one another, one -exec will be run if the previous -exec succeeds i.e. the command run by -exec returns exit status 0.

Do:

find /home/ouhma -type f -newermt '50 seconds' -exec env LC_ALL=C \
       bash -c 'grep -Pq "hell\d" <(tail -c 1000 "$1")' _ {} \; -exec echo {} \;

As you are just printing the filename, this would suffice though:

find /home/ouhma -type f -newermt '50 seconds' -exec env LC_ALL=C \
       bash -c 'grep -Pq "hell\d" <(tail -c 1000 "$1") && echo "$1"' _ {} \;
Sign up to request clarification or add additional context in comments.

9 Comments

If I test your command by this way: find /home/ouhma/hello.txt -exec LC_ALL=C grep -Pq 'hell\d' <(tail -c 1000 {}) \; -exec echo {} \; It returns the following error: tail: cannot open '{}' for reading: No such file or directory find: 'LC_ALL=C': No such file or directory
Spawning a bash for every file won't be faster than OP's own attempt.
@anubhava True, check now.
Good but now it will scan full 3GB data not just last 1000 characters.
@anubhava Hmmm..chicken-egg problem i see :/
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.