79411190

Date: 2025-02-04 09:42:38
Score: 1
Natty:
Report link

Offered solutions could be better quality. There are a number of unsolved issues with the given answers:

  1. Various special characters in bash have to be escaped.

  2. Grep searches for a regex which has to be escaped.

  3. If commands are put into other commands, that leads to more things being escaped.

All three combined when searching code is a literal nightmare to figure out: the user should have an input for what to search for that does not require any escaping.

The following should be a big improvement:

# Note: Change search string below here. 
nCores=$(nproc --all)
read -r -d '' sSearch <<'EOF'
echo $locale["timezone"]." ".$locale['offset'].PHP_EOL;
EOF
find -print0 \(  -type f \) -and \( -not \( -type l \) \) -and \( -not \( -path "./proc/*" -o -path "./sys/*" -o -path "./tmp/*"  -o -path "./dev/*" \) \) | xargs -P $nCores -0 grep -Fs "$sSearch" | tee /home/npr/results.txt &

If you do not want to suppress grep errors, use this:

# Note: Change search string below here. 
nCores=$(nproc --all)
read -r -d '' sSearch <<'EOF'
echo $locale["timezone"]." ".$locale['offset'].PHP_EOL;
EOF
find -print0 \(  -type f \) -and \( -not \( -type l \) \) -and \( -not \( -path "./proc/*" -o -path "./sys/*" -o -path "./tmp/*"  -o -path "./dev/*" \) \) | xargs -P $nCores -0 grep -F "$sSearch" | tee /home/npr/results.txt &

Change EOF to any other A-Za-z variable if it's desired to search for the literal text EOF.

With this, I reduced a day-long search that had thousands of errors resulting from several of the top answers here into an easy sub 1-minute command.


Reference:

Also see these answers:

https://unix.stackexchange.com/questions/394983/nested-quotes-nightmare-sending-an-e-mail-from-a-remote-host

running bash pipe commands in background with & ampersand

How do I exclude a directory when using `find`? (most answers were wrong and I had to fix it for modern find).

https://unix.stackexchange.com/questions/172481/how-to-quote-arguments-with-xargs

https://unix.stackexchange.com/questions/538631/multi-threaded-find-exec

Reasons:
  • Blacklisted phrase (1): How do I
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Low reputation (1):
Posted by: npr_se