Move shared file automatically

Hi there!
I have a Nextcloud 15 installation with more than 1000 users and LDAP auth.
Each user receives some shared files every day, so very soon his root folder is becoming a mess.
Is there any way to automatically move some (not all with a config parameter) files to another folder?

I know it can be done via webdav, but you mus know the password of each user to do this.

Can I use cron for this task?
Or maybe it’s better to user PostLogin hook?
Please show me a way to control files that was shared to user without knowing user’s password.

At least you can set globally folder that will be used for shared files. Per default it is root, but you can set it to something via config.php: 'share_folder' => '/', more info under: https://docs.nextcloud.com/server/15/admin_manual/configuration_server/config_sample_php_parameters.html

You can try to use bunch of apps like: Automatically tagging such kind of files https://apps.nextcloud.com/apps/files_automatedtagging and then move them to somewhere script and app via https://apps.nextcloud.com/apps/workflow_script. Looks not really efficient :slight_smile:

I will try, thank you!
I will notify about the result

No, it did not work (
I need to move shared files using some logic, so I think that my way is to use Hooks…
If someone has ever done something like this, please suggest a direction)

1 Like

will implement my logic of sharing files via cron + sql script (oc_share table)

Just share your results when you are ready.

The results:

  • config.php:
    ‘skeletondirectory’ => ‘/var/www/nextcloud/data/skeleton/’,
    In /var/www/nextcloud/data/skeleton/ there is a SharedFiles folder

  • Then running shell script (to create SharedFiles folder for already active users):

    for d in */ ; do
    mkdir -p "$d"SharedFiles
    echo “$d”
    done
    echo “changing ownership”
    chown -R www-data:www-data /var/www/nextcloud/data/
    echo “ownership changed”
    echo “rescanning data folders”
    sudo -u www-data /usr/bin/php /var/www/nextcloud/./occ files:scan --all
    echo “folders rescanned”
    “Finished!”
    exit 0

  • Run sql script:
    UPDATE oc_share SET file_target = ‘/SharedFiles’ || file_target where item_type = ‘file’ and file_target not ilike ‘/SharedFiles/%’ and uid_owner in (select distinct uid FROM public.oc_group_admin as admin_list);

The SQL script will run by cron

UPD ‘skeletondirectory’ => ‘/var/www/nextcloud/data/skeleton/’, seems not working now for ldap and local users

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 https://docs.nextcloud.com/server/15/admin_manual/configuration_server/occ_command.html?highlight=occ#scan

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 {} \

Thank you very much for improvements!
scan --all is because the script will be run only once, I’ve fixed the skeleton folder (seems that the order of parameters in config.php matters)

for using scan --path the boolean variable ‘directory_created’ would be needed to determine the need to rescan the folder
And special thanks for find /var/www/nextcloud/data/ ! -user www-data -exec chown www-data:www-data {} \!