Search code examples
bashcronchown

cron chmod script does not change owner


i need to run cron job that changes owner and group for selected files.

i have a script for this:

#!/bin/bash
filez=`ls -la /tmp | grep -v zend | grep -v textfile | awk '$3 == "www-data" {print $8}'`
for ff in $filez; do
        /bin/chown -R tm:tm /tmp/$ff
done

if i run it manually - it works perfectly. if i add this to root's cron

* * * * *               /home/scripts/do_script

it does not change owner/group. file has permissions "-rwsr-xr-x".

any idea how this might be solved?


Solution

  • On my system, field $8 is the hour/year, not the filename. Maybe that's the case for your root user as well. This is why you should never try to parse ls. Even if you fix this issue, half a dozen more will remain to break the system in the future.

    Use find instead:

    find /tmp ! -name '*zend*' ! -name '*textfile*' -user www-data \
        -exec chown -R tm:tm {} \;