Find only files/directories with different permissions/owners

My git post-update has the following lines in it to make sure the permissions are set right:

find /usr/local/apache/htdocs -type d -print0 | xargs -0 chmod 755
find /usr/local/apache/htdocs -type f -print0 | xargs -0 chmod 644
chown -R apache:apache /usr/local/apache/htdocs

The only problem is that each git update takes some time as it chmod's each and every file on the web server. So I'm assuming the fastest thing would be to only chmod only the files that have different permissions and only chown the files that aren't apache:apache.

How would I rewrite the three lines above to make that change? Many thanks.

for first two lines try below to chnage only permissions of the other than 644(for files) and 755 for directories

find /usr/local/apache/htdocs \! -perm 644 -type f  -print0 | xargs -0 chmod 644
find /usr/local/apache/htdocs \! -perm 755 -type d -print0 | xargs -0 chmod 755

I'll give that a try. Digging further, will this work for the chown?

find /usr/local/apache/htdocs \! -user apache -print0 | xargs -0 chown apache:apache

---------- Post updated at 03:10 PM ---------- Previous update was at 03:08 PM ----------

Getting an error on the command:

 find /usr/local/apache/htdocs \! -perm 644 -type f  -print0 | xargs -0 chmod 644
chmod: missing operand after `644'

try this

find /usr/local/apache/htdocs \! -perm 644 -type f | xargs chmod 644

it worked for me in sloaris , which os are you using

In one pass without xargs (tested with an old GNU find):

find /usr/local/apache/htdocs    \
\( -user apache -group apache -o -exec chown apache:apache {} + \)    \
\( -type d ! -perm 755 -exec chmod 755 {} + -o -type f ! -perm 644 -exec chmod 644 {} + \)

Regards,
Alister

2 Likes

CentOS