0

So am setting a code that will change group owner of the file to a specific group.

PS there are more than 10 users, with same folder and file structure, and i want change the ownership of a specific file in each folder.

So that if anyone edits the file, within 10 second (using cron, and sleep function) it will change the group owner to a specific group.

here is my code i used, and created a file named owner.sh

chgrp www-data /home/*/private/FILE.TXT
sleep 10
chgrp www-data /home/*/private/FILE.TXT
sleep 10
chgrp www-data /home/*/private/FILE.TXT
sleep 10
chgrp www-data /home/*/private/FILE.TXT
sleep 10
chgrp www-data /home/*/private/FILE.TXT
sleep 10
chgrp www-data /home/*/private/FILE.TXT
sleep 10

And it should work, but when I try to bash it,

bash owner.sh 

it says that /home/*/private/FILE.TXT does not exist, but if I run it manually that is

chgrp www-data /home/*/private/FILE.TXT

, it goes in each and every user's folder and change the group of that specific file.

So how to make it work the way I want?

  • Ah. Where did you create this file? On a Windows PC? – roaima Apr 26 '16 at 16:05
  • I have updated. Please see now. – Harsh Apr 26 '16 at 16:05
  • Ubuntu 14.04, using nano. – Harsh Apr 26 '16 at 16:09
  • What's the output of running the command file owner.sh please? It should give one of two results. Either owner.sh: ASCII text or owner.sh: ASCII text, with CRLF line terminators. – roaima Apr 26 '16 at 16:24
  • Also, please provide the exact and complete error message you get from running your script with bash owner.sh. – roaima Apr 26 '16 at 16:25
0

try:

#!/bin/bash
while true; do
    for user in $(ls /home); do
        chgrp www-data /home/${user}/private/FILE.TXT
    done
    sleep 10
done

The infinite loop is for bypassing the cron limitation of 1 minute to repeat a job. make it executable:

chmod +x /PATH/TO/owner.sh

And just run it without a cronjob.

Also if you just want to react to the modification of a file and modifying directly the ownership of this file, you can use the inotify-tools:

#!/bin/bash
for user in $(ls /home); do
    inotifywait -mqe close_write /home/${user}/private/FILE.TXT --format %f | while IFS= read -r file; do chgrp www-data $file; done
done

Just run it once

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.