H M Kunzmann wrote:
I have an old script that replaces spaces with underscores.I've used wget to mirror a site with many subdirectories. Unfortunately this site had many spaces in the urls and now I have a stack of directories with %20 instead of spaces.
Can I write a script to get all directory names with %20 in the name and replace the %20 with spaces ?
How would I do this ? Pointers would be much appreciated.
Also, could this script be made to drill down into subdirectories and files ?
Thanks for the help
Ciao Herbert
I took the "lowercase" script that came with bash and made a very simple modification to it.
You could use something like this to replace %20 with whatever you want.
Well, I should have tried it before posting. I tried it and it ended up with big spaces but this works.
#!/bin/bash Usage="Usage: $name file ..." phelp() { echo "$name: replace spaces in filenames with underscore. $Usage Each file is moved to a name with the same directory component, if any, and with a filename component that is the same as the original but with any spaces changed to underscore." }
name=${0##*/}
while getopts "h" opt; do case "$opt" in h) phelp; exit 0;; *) echo "$Usage" 1>&2; exit 2;; esac done
shift $((OPTIND - 1))
for file; do
filename=${file##*/}
case "$file" in
*/*) dirname=${file%/*} ;;
*) dirname=. ;;
esac
nf=$(echo $filename | tr "%20" "\ " |tr -s "\ ")
## nf=$(echo $filename | tr "\ " _) newname="${dirname}/${nf}"
if [ "$nf" != "$filename" ]; then
mv "$file" "$newname"
echo "$0: $file -> $newname"
else
echo "$0: $file not changed."
fi
done