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: Chris Elvidge
Subject: Re: Speeding up a find and subsequent grep
Date: Sat, 19 Dec 2020 13:56:22 +0000
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1 Lightning/5.4

On 19/12/2020 12:04 pm, Jeffrey Walton wrote:
Hi Everyone,

I'm working on CentOS 7, x86_64, fully patched. The server has a
manual wiki installation. Ownership and permissions need to be set
after an update. I've got a script that does it. The slowest part of
the script is this:

# Make Python, PHP and friends executable
IFS= find "$WIKI_DIR" -type f -print | while read -r file
do
     if file -b "${file}" | grep -q -E 'executable|script';
     then
         chmod u=rwx,g=rx,o= "${file}"
     else
         chmod u=rw,g=r,o= "${file}"
     fi
done

Some of the files have whitespace in their names so I need something
like 'find -print | while read'.

When I added the 'file -b' piped to 'grep -E' the script slowed down
considerably.

Is there any way to speed up that portion of the script?

Thanks in advance.

Jeff




Probably better to look for strings starting PHP and/or Python (and/or Bourne, ELF for compiled programs. File also mistakes lua scripts for Ruby)

I don't know if it will help but: make a executable script like this

#!/bin/bash
## ./auto_chmod
## 750 is same as u=rwx,g=rx,o=
echo "$1"
file -b "$1" | grep -E "^PHP|^Python" && chmod -v 750 "$1" || chmod -v 640 "$1"
## or for a quiet time
#file -b "$1" | grep -q -E "^PHP|^Python" && chmod 750 "$1" || chmod 640 "$1"
#

then use:
find "$WIKI_DIR" -type f -exec ./auto_chmod {} \;

Gets rid of the while read. Works with filenames with spaces.


--
Chris Elvidge
England




reply via email to

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