Transferring ownership of files in ownCloud to another user automatically

One of the biggest issues when using ownCloud in a corporate environment is managing ownCloud files ownership especially when people leave the company. ownCloud automatically deletes the files a person has created when his/her account is closed or disabled. We have kept a bunch of AD accounts open because of this issue. We tried a workaround the problem by creating a standalone account for all departments and manage the permissions to all team members from there, but because users can share files among each other from their own account the mess always build up.

Recently we upgraded to ownCloud 10, and one of the new features in this version is the ability to transfer files ownership from one account to another, and even though the feature is only available from the command line as of now, meaning you have to have access to your ownCloud server as the root user and be comfortable running Linux commands is a very welcome feature.

OK. enough talking and let’s get some action going.

Login to your ownCloud server with the root account or with a ROOT account that has write permissions to your ownCloud root folder. In my case, that is “/var/www/owncloud” once your are the root of your ownCloud folder, list your users by running this command:

sudo -u www-data php occ user:list

That will show you all the list all of your users in your ownCloud instance. Get the username of the Source and destination users you are dealing with. Once you have that info, use this command to transfer all files from one user to another:

sudo -u www-data php occ occ files:transfer-ownership <source-username> <destination-username>

If you only want to transfer a folder to another user, use this command:

sudo -u www-data php occ files:transfer-ownership --path="folder/to/move" <source-username> <destination-username>

This command transfers shares and metadata info associated with those files (shares, tags, and comments, etc) also note that the directory (and its contents) won’t be moved as is between the users. It’ll be moved inside the destination user’s files directory, and placed in a directory which follows the format: transferred from <source-username> on <timestamp>. Using the example above, it will be stored under: data/<destination-user>/files/transferred from <source-user> on 20170426_124510/

Now you can delete the source account if you need to without deleting the ownCloud this user owns.