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

89 Views Asked by At

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.

1

There are 1 best solutions below

0
Maximilian Ballard On

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