I wanted a script to help me rename files that Handbrake creates.
Issues
There are mainly 2 issues that I come across:
- Spaces that I want to replace with underscores
- Remove Unprintable characters
- I determined that these were ASCII characters 001-037 (in octal)
- ls -b directory_with_octal_chars_in_filename
Solution
Script (rename.sh)
#!/usr/bin/bash
directory=${1}
if [ -z ${directory} ] ; then
echo "Requires a path"
exit 1
fi
# replace spaces with underscore
find ${directory} -type f -name "* *" |
while read file; do
mv "${file}" "${file// /_}"
done
# remove unprintable ASCII chars
find ${directory} -type f -name "*"[$'\001'-$'\037']"*" |
while read file; do
new_file=$(echo "$file" | sed -e 's/[\o001-\o037]//g')
mv "$file" "$new_file"
done
Permissions
chmod +x rename.sh
Usage
./rename.sh Directory_To_Rename_Files_In
Appendix
Sources
- https://stackoverflow.com/questions/71629204/how-to-find-and-rename-file-and-folder-names-with-octl-characters
- https://stackoverflow.com/questions/15974506/use-sed-to-replace-a-range-of-hex-values
No comments:
Post a Comment