Special Characters in File Names....


Mar 2, 2024
We're having the hardest time with what should be a small issue. Maybe it's not.

We have about 190 TiB of data on two pools on TrueNAS 12.x. We are trying to sync the files over to another NAS (not TrueNAS) and the other NAS refuses to accept special characters in file names. Because of poor naming conventions over two decades, easily about 40 TB of that 192 TiB is going to have dirty file names. I was like, ok... and I got a utility to handle batch name changes from a workstation in the shop with the NAS mounted. Well, that utility was issuing name changes MUCH faster than the NAS could digest and it crashed SMB. The makers of the utility then said, "Oh, yeah. Don't use this with a NAS because, well, you've seen why." So, I figure the only way to do the task of batch scrubbing 192 TiB worth of filenames and directory names is to do it from within the NAS itself.

I've been playing with a script today. I'm not at all good at that kind of thing. And I wish to all things there were a GUI way to do it, as I'm dyslexic. But, here we are. Any ideas?

#!/bin/bash # Set the path to the directory directory="/path/to/your/files" # Navigate to the directory cd "$directory" # Rule 1: Replace specific characters with an underscore rename 's/[#$%^[]()<>"\/|\\]/_/g' * # Rule 2: Replace instances of the character "&" with "A" rename 's/&/A/g' * # Rule 3: Remove instances of the character "." rename 's/\./ /g' * # Rule 4: Preserve all file extensions rename 's/\.$/_/' * # Rule 5: Apply the script to folder names as well find . -type d \ -exec rename 's/[#$%^[]()<>.”\/|\\]/_/g’ {} \; \ -exec rename 's/&/A/g' {} \; \ # Rule 6: Remove spaces in filenames and folder names find . -depth -name "* *" -execdir rename 's/ /_/g' "{}" \; # Rule 7: Append a number to prevent duplicate names i=1 for file in *; do newfile=$(echo "$file" | sed 's/\.[^.]*$//') # Remove file extension while [[ -e "$newfile.$i.${file##*.}" ]]; do ((i++)) done mv "$file" "$newfile.$i.${file##*.}" i=1 done