renaming non-ascii file names
Mon, 08/14/2017 - 12:52 — sandipBelow script replaces non-ascii character with an underscore in file/folder names.
Before running the below script, cd to the folder with files that need renamed first.
#!/bin/bash
# rename2ascii.sh
# Replaces non-ascii character with an underscore.
function rename_to_ascii() {
echo -n "$1" | \
perl -ne '$new = $_; if($new =~ s/[^[:ascii:]]/_/g) {
print("Renaming $_ to $new\n");
rename($_, $new);
}'
}
export -f rename_to_ascii
find -depth -exec bash -c 'rename_to_ascii "$0"' {} \;- sandip's blog
- Login or register to post comments
Rename files and folders to lower case letters
Fri, 07/29/2005 - 09:58 — sandipHere's a simple bash script that will help in renaming files and folders with uppper-case to lower-case letters.
Make sure you have a backup and test it with a small batch. Also read the whole comments section below if you have spaces in your file names.
#!/bin/bash
#
# Filename: rename.sh
# Description: Renames files and folders to lowercase recursively
# from the current directory
# Variables: Source = x
# Destination = y
#
# Rename all directories. This will need to be done first.
#
# Process each directory’s contents before the directory itself
for x in `find * -depth -type d`;
do
# Translate Caps to Small letters
y=$(echo $x | tr '[A-Z]' '[a-z]');
# check if directory exits
if [ ! -d $y ]; then
mkdir -p $y;
fi
# check if the source and destination is the same
if [ "$x" != "$y" ]; then
# check if there are files in the directory
# before moving it
if [ $(ls "$x") ]; then
mv $x/* $y;
fi
rmdir $x;
fi
done
#
# Rename all files
#
for x in `find * -type f`;
do
# Translate Caps to Small letters
y=$(echo $x | tr '[A-Z]' '[a-z]');
if [ "$x" != "$y" ]; then
mv $x $y;
fi
done
exit 0