Move shared file automatically

Could be not most efficient way to do this (for me it takes too long), you can do rescan of exact folder for exact user. This will reduce time to rescan, e.g.

... --path "user_id/files/path"
  or
... --path "user_id/files/mount_name"
  or
... --path "user_id/files/mount_name/path"

from Using the occ command — Nextcloud 15 Administration Manual 15 documentation

You can added before done something like this (but I’m not sure):

sudo -u www-data /usr/bin/php /var/www/nextcloud/occ files:scan --path "$d/files/$dSharedFiles"

I wrote short script to rescan external shares only and run it via cron like few times per day, it is few times faster as rescan --all.

Also make chown of whole data folder on a bigger installations will be inefficient. Try to find “wrong” users and then execute chown, e.g. (took it from here, explanation):

find /var/www/nextcloud/data/ ! -user www-data -exec chown www-data:www-data {} \