Skip to content
Snippets Groups Projects
Verified Commit 1e3005f6 authored by David Beniamine's avatar David Beniamine
Browse files

Script to remove trailing space in filenames

Nextcloud does not handle well trailing space in filenames
parent ed038ffa
No related branches found
No related tags found
No related merge requests found
#!/bin/env python3
import click
from subprocess import run
import os
def add_common_parent(d, dirs):
for sub in dirs:
if d in sub:
print(' {} is a subdir of {}, replacin'.format(sub, d))
dirs.remove(sub)
elif sub in d:
print(' {} is a subdir of {}, ignoring'.format(d, sub))
return
dirs.append(d)
@click.command()
@click.argument('base', type=click.Path('r'))
@click.argument('path', type=str)
@click.option('--dry_run/--no-dry-run', default=False, help='Only list files to rename')
@click.option('--occ_path', help='occ command path', default='/var/www/nextcloud/occ') # , type=click.File('r'))
@click.option('--user', help='User for running occ command', default='www-data')
@click.option('--php', help='Php command', default='php')
def rename(base, path, dry_run, occ_path, user, php):
files = []
dirs = []
# Retrieve files with trailing space
cmd = ['find', '{}/{}'.format(base, path), '-regex', '.*/.*\s$']
r = run(cmd, capture_output=True)
files = r.stdout.decode('utf-8').split('\n')[:-1]
# Rename files and collect directories to scan
for f in files:
new_name = f.strip()
print("'{}' >>> '{}'".format(f, new_name))
if (not dry_run):
os.rename(f, new_name)
add_common_parent(os.path.dirname(f), dirs)
# Scanning
for d in dirs:
print('Scanning {} via occ'.format(d))
p = d.replace(base, '')
cmd = ['sudo', '-u', user, php, occ_path, 'files:scan', '--path', p]
print(cmd)
if not dry_run:
run(cmd)
if __name__ == "__main__":
rename()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment