This should be one of the most common scripts available!
Yet I'm going crazy trying to remove special characters and am
very sad that I can't find any good existing script by googling.
Here's the best I have and it doesn't work (only partially).
Do you have a script that just converts all files in a directory
that have spaces and parentheses in the name (and possibly other
special characters if necessary) to underscores?
Here's what I'm using but it is driving me crazy.
#!/bin/bash
#
https://md314159265.wordpress.com/2006/08/01/a-script-to-remove-spaces-from-a-file-name/
# Writen by Mayuresh Phadke (mayuresh at
gmail.com)
# To change the names of all files in a directory including directory names
# run the command
#
# find . -depth -exec ~/rename.sh {} \;
#
# This command is pretty useful if you have a collection of songs or pictures transferred
# from your windows machine and you are finding it difficult to handle the
# spaces in the filenames on UNIX
#
# set -x
progname=`basename $0`
# if [ $# != 1 ]
# then
# echo "Usage: $progname \"file name with spaces\""
# echo
# echo "This utility is useful for renaming files with spaces in the filename. Spaces in the filename are replaced with _"
# echo "\"file name with spaces\" will be renamed to \"file_name_with_spaces\""
# echo
# exit 1
# fi
# Replace all spaces with underscores
for i in *\ *
do
old_name=$i
echo "old_name = $old_name"
dir=`dirname "$i"`
echo "dir = $dir"
file=`basename "$i"`
echo "file = $file"
done
new_file=`echo $file|sed "s/ /_/g"`
new_name=$dir"/"$new_file
echo "new_file = $new_file"
if [ "$old_name" != "$new_name" ]
then
mv "$old_name" "$new_name"
fi
# Replace all open parenthesis with underscores
for i in *\(*
do
old_name=$i
echo "old_name = $old_name"
dir=`dirname "$i"`
echo "dir = $dir"
file=`basename "$i"`
echo "file = $file"
done
new_file=`echo $file|sed "s/(/_/g"`
new_name=$dir"/"$new_file
echo "new_file = $new_file"
if [ "$old_name" != "$new_name" ]
then
mv "$old_name" "$new_name"
fi
# Replace all closed parenthesis with underscores
for i in *\)*
do
old_name=$i
echo "old_name = $old_name"
dir=`dirname "$i"`
echo "dir = $dir"
file=`basename "$i"`
echo "file = $file"
done
new_file=`echo $file|sed "s/)/_/g"`
new_name=$dir"/"$new_file
echo "new_file = $new_file"
if [ "$old_name" != "$new_name" ]
then
mv "$old_name" "$new_name"
fi
exit 0