Changing the permissions and owner of the /var/www/html folder using fswatch and xargs - 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.

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"' --

Related

Script to check if files in $HOME are owned by $USER and -if not- change them

Need to create a script at startup that checks if files in $HOME are owned by $USER and -if not- change them.
Would start with
find $HOME ! -user $USER
and later
chown -R $USER:$USER $HOME
but can't write it properly to be started automatically as a service at startup in Debian.
This should work.You will probably need to run as sudo.
find $HOME ! -user $USER -exec chown -R $USER:$USER {} \;
To execute a command with find you can use -exec with the command you want to run.
The filename/directory found is inserted into statement as {}. And when using -exec you have to include a semicolon at the end.
If you execute this from a shell script and use sudo be sure to use $SUDO_USER and /home/$SUDO_USER since $USER evaluates to root when running a shell script using sudo

How to solve no such file or directory while using xargs

I am trying to copy a file(s) to the same directory but with a prefix. I am using xargs to do that. NOTE: I can't use cd as it will break build in my pipeline.
This is what I have
root#gotoadmins-OU:~# ls
snap test
root#gotoadmins-OU:~# ls test/
me.war
root#gotoadmins-OU:~# ls test | xargs -I {} cp {} latest-{} test/
cp: cannot stat 'me.war': No such file or directory
cp: cannot stat 'latest-me.war': No such file or directory
If I understand the question correctly, you simply want to copy all of the files in a subdirectory to the same subdirectory, but with the prefix "latest-".
find test -type f -execdir bash -c 'cp "$1" latest-"$(basename "$1")"' "$(which bash)" "{}" \;
$(which bash) can be replaced with the word bash or anything, really. It's just a placeholder.
As #KamilCuk commented, a subshell might also be an option. If you put commands inside parentheses (e.g. (cd test; for FILE in *; do cp "$FILE" latest-"$FILE"; done)), those commands are run in a subshell, so the environment, including your working directory, is unaffected.
can you just use the cp command to do this?
cp test/me.war test/latest-me.war

Move specific folders to a new location and leave a symlink at the old location to the new folder

I have a lot of folders named Archive.folder in /var/CommuniGate/Accounts/*/. How can I move them to a new location and leave a symlink at the old folder to the new location?
Maybe use find and cp to move them?
find /var/CommuniGate/Accounts/ -name 'Archive.folder' -exec cp -r --parents {} . \;
This works fine for moving them and keeping the folder structure. But how can leave a link at the same time with ln? Also the link should replace the old folder.
I've tried executing the following after, it works if there is only one folder.
find /var/CommuniGate/Accounts/ -name 'Archive.folder' -delete
find * -name 'Archive.folder' | xargs -0 -iDIR sh -c 'mkdir -p /$(dirname DIR); ln -s -t /$(dirname DIR) $PWD/DIR'
I get permission denied for the sh -c for everything after the first directory.
You can juste move your file to the desired location and then create the symlink :
mv /var/CommuniGate/Accounts/Archive.folder* new_location/
ln -s new_location/Archive.folder* .

fswatch not running script, reports as not found

I have a script in ~/bin called go_refresh. It's definitely there. I run fswatch like so:
fswatch -o /usr/local/var/www/wp_sites/test/wp-content/themes/test/style.css | xargs -n1 -I{} ~/bin/go_refresh
When I change the file, I get this error once:
xargs: /Users/my_dir/bin/go_refresh: No such file or directory
And the script doesn't run.

Script in Bash to delete

I am trying to delete all files containing the name TRAR in the filename. This is for a Linux system and this is my first time doing such a script, below is what I have tried, but it does not work
cd /appl/virtuo/gways/input_d
rm -rf TRAR*
When I manually enter the directory and run rm -rf TRAR* , all the files are removed, I need this script to work so that it can be added to run via a cronjob..
VENDOR=ericsson-msc
RELEASE=R13.2
BASE_DIR=/appl/virtuo/gways
RAW_DIR=${BASE_DIR}/config/${VENDOR}/${RELEASE}/trdipfile_raw_landing_area
#rm -rf $RAW_DIR/*
cd ${RAW_DIR}
ssh netperf#10.76.26.1 "cd /var/opt/ericsson/sgw/outputfiles/apgfiles/oms ; find . -newer ~/msc- trdif-timestamp -type f | egrep TRDIP | cpio -oc ; touch ~/msc-trdif-timestamp" 2>/dev/null | cpio - icdu 2>/dev/null
If you run this script by crontab then you should add
!#/bin/sh to first line of file. Anc change permissions for this file. For example
chmod 755 script.sh
Or you can add to crontab command as /bin/sh /<folder with scripts>/script.sh

Resources