Fixing trailing spaces in directory names

We had a situation where trailing spaces in directory names on a Ubuntu server caused issues with a Nextcloud instance. As a temporary fix I found a script here to deploy.

https://github.com/nextcloud/server/issues/5843#issuecomment-493822639

Here is a slightly modified version we run daily using cron.

#!/bin/bash

NEXTCLOUD_DATA_DIR=/nc/data/directory
NEXTCLOUD_INSTALL=/nc/web/root

find_cmd=(
  find                 
  $NEXTCLOUD_DATA_DIR  
  -depth               
  -type d              
  -name '*[[:space:]]' 
  -print0              
)

shopt -s extglob                            
while IFS= read -r -d '' source_name; do    
  dest_name=${source_name%%+([[:space:]])}  
  mv -v "$source_name" "$dest_name"         
done < <("${find_cmd[@]}")                  

cd $NEXTCLOUD_INSTALL
php occ files:scan --all --quiet

exit 0