help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Speeding up a find and subsequent grep


From: Eli Schwartz
Subject: Re: Speeding up a find and subsequent grep
Date: Sun, 20 Dec 2020 17:38:48 -0500

On 12/20/20 2:51 PM, Leonid Isaev (ifax) wrote:
The only external (to bash) tool you really need is file(1)... Try this
prototype script (also, see [1]):
You can remove the need for file(1) too.

shopt -s globstar
# shopt -s nullglob # not needed, [[ -f $f ]] will return false

knownscripts_re='^ *#! *.*/?(php|python[[:digit:]]*|bash|sh)( |$)'
shebang_re='^ *#!'

for f in ./**/*; do
    [[ -f $f ]] || continue

    read -r header < "$f"
    if [[ ${header:0:4} = $'\x7fELF' ]]; then
        echo "$f is an elf binary and should be chmodded"
    elif [[ $header =~ $knownscripts_re ]]; then
        echo "$f is a python/php/shell script and should be chmodded"
    elif [[ $header =~ $shebang_re ]]; then
        echo "$f is *a* script and should still probably be chmodded"
    fi
done

Still requires bash 4 due to globstar, but this could be removed if desperately needed, by manually globbing at every directory level.

for f in * */* */*/* */*/*/*; do
    ...
done


=~ needs bash 3

testing indicates bash 2's `read` mangled the input, so that needs bash 3 too (or drop the elf binary check)

--
Eli Schwartz
Arch Linux Bug Wrangler and Trusted User

Attachment: OpenPGP_signature
Description: OpenPGP digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]