# spacereplace v1.0.1 by Richard van Kampen - july 2005
function helpme {
echo ""
echo "Help for spacereplace"
echo ""
echo "spacereplace version 1.0.1 - written by Richard van Kampen"
echo "Tool to substitute spaces in file and directory names with underscores."
echo ""
echo "Usage: spacereplace [option] [PATH]"
echo ""
echo "List of options:"
echo ""
echo "-h or --help : show help."
echo "-r : recursive. Also commit changes to all sub-directories."
echo ""
echo "You have to specify the path to the directory you want to process. Not specifying a path will just display help"
echo ""
echo "Example: 'spacereplace -r /home/richard/video' changes all spaces to underscores in /home/richard/mp3 and all directories below /home/richard/mp3."
echo ""
exit
}
for arg in "$@" # grab command line options
do
if [ "$arg" != "-r" ] && [ "$arg" != "-h" ]; then
dir="$arg"
orgdir="$dir"
sub=" and sub-directories"
fi
case "$arg" in
-h | --help ) help=1 ;; # help option true.
-r ) recursive=1 ;; # recursive is true
esac
done
if [ "$dir" == "" ];then
helpme
fi
if [ ! -d "$dir" ]; then
echo "Directory $dir does not exist. Please try again with full path"
exit
fi
if [ "$help" = "1" ]; then # do this if -h option is used
helpme
fi
function delspaces {
strips=1;
teller=1; # iterate through 'contentgrab' array
cd $dir;
# rename @ because we need it. Do it 5 times
until [ $strips = "5" ];do
rename @ - *
let strips+=1
done
for i in $(ls | sed 's/\n//g' | sed 's/\ /@/g'); do
i=${i//@/ }
contentgrab[$teller]=$i
replacespace=${contentgrab[$teller]// /_}
if [ "${contentgrab[$teller]}" != "$replacespace" ] && [ ! -f "$replacespace" ] && [ ! -d "$replacespace" ]; then
mv -- "${contentgrab[$teller]}" "$replacespace"
fi
let teller+=1;
done
}
teller3=1;
function getdirs {
for i in $(echo */); do
if [ "$i" != "*/" ];then
j[$teller3]=${i%/}
contentdirs[$teller3]=$dir/${j[$teller3]}
let teller3+=1
fi
done
}
delspaces
getdirs
counter4=1
if [ "$recursive" = "1" ]; then
until [ "${contentdirs[$counter4]}" == "" ]; do
dir=${contentdirs[$counter4]}
let counter4+=1
delspaces
getdirs
done
orgdir="$orgdir$sub"
fi
echo ""
echo "Changed all spaces to underscores in $orgdir"