xargsfswatch

Changing the permissions and owner of the /var/www/html folder using fswatch and xargs


I want to change the owner and permission of the /var/www/html folder every time I add/create or update any new file or directory in this folder.

I thought of using fswatch for this to grab the events that happen in that directory(i.e /var/www/html), with now I am able to get the event of updated/removed every time any change like creating or deleting the file respectively in the directory by the command

fswatch -x /var/www/html

Now upon this event I piped the output to xargs and executed the chown and chmod command on the /var/www/html directory but now on file deletion fswatch & xargs still executes the chmod and chown command and throws an error no such file in the directory which is obvious as the file is deleted hence I want to know how can use if condition to check the event type (like only for updated event output of fswatch command) the chmod and chown commands are to be fired.


Solution

  • I'd filter-in for specific events, then pass it by one to xargs via e.g.:

    fswatch -x /tmp | egrep --line-buffered '(Created|Updated)$'| \
       xargs -l1 sh -c 'test -f "$1" && chown some_user:some_group "$1"' --