MacOS – How to quickly rename files on NAS

macosnasrenameterminal

I have recently added a Qnap (Linux) NAS to my Mac OS environment, as the main file server.

I realized many of the Mac OS files have / characters, that do not behave well in Linux, as they disappear when seen from Macs (they actually are being renamed in Linux and becoming invisible from the Macs… dunno why).

Example: a file named image/b.jpg should be renamed as image_b.jpg to avoid problems on the Qnap Linux NAS.

Anyway, I'd like to build a script from Mac OS Terminal to access the Linux NAS and perform a "search and change" of the / character to something else like: _.

I have begun by writing this:

ssh admin@192.168.2.2
for f in $(find /share/Public/ -name "*:*"); do mv $f ${f/:/_}; done

I can log on the remote NAS, but seems not to work… I am not a tech guy, and tried to collect some code samples from the internet.

What are options to automate or script file renames for a NAS share?

Best Answer

Running for over the results of a find is not recommended, it will break on filenames with spaces, newlines etc.

You can use the following instead once you are logged into your NAS:

find /share/Public/ -depth -name '*:*' -exec sh -c 'mv -- "$1" "${1//:/_}"' _ {} \;

To be on the save side, run

find /share/Public/ -depth -name '*:*' -exec sh -c 'echo mv -- "$1" "${1//:/_}"' _ {} \;

and check the output for anything unexpected.

PS: The duplicated // in ${1//:/_} is intentional to ensure that all occurrences of : are replaced.