I was struggling to use Nextcloud as I found some of the documentation to be out of date. I kept finding references to using occ to run commands, but couldn’t work out how to use it. I finally found this page https://docs.nextcloud.com/server/latest/admin_manual/configuration_server/occ_command.html which outlined the full command to use. This needs to enter the full path to the occ file.
sudo -u www-data php /var/www/nextcloud/occ
(You can open the occ file in nano, it is simply a php file.