segunda-feira, 9 de novembro de 2009

nospace.sh

#!/bin/bash
# Rename files on the current directory, changing spaces to underscore.. recursive
# Author: Daniel Hilst

HELPMSG="Options:\v-h\tGive this help message\n\t-v\tPrint errors on stderr"
TURN=1
DIRS=$(find . -maxdepth ${TURN} -type d -name "* *")
DBUG="/dev/null"
IFS="
"
if [ "$1" == "-h" ]; then
 echo -e ${HELPMSG}
 exit 1
fi
if [ "$1" == "-v" ]; then
 DBUG="/dev/stderr"
fi




mvFiles()
{
 FILES=$(find . -type f -name "* *")
 for FILE in ${FILES}; do
  echo -n "Renaming ${FILE} ..."
  mv ${FILE} $(echo ${FILE} | tr -s " " "_") && { echo "done"; } || { echo "fail"; }
 done
}



mvDir()
{
 let TURN++;
 for DIR in ${DIRS}; do
  echo -n "Renaming ${DIR} ..."
  mv ${DIR} $(echo ${DIR} | tr -s " " "_") 2> ${DBUG} && {
  echo "done"; } || { echo "fail"; }
 done
 DIRS=$(find . -maxdepth ${TURN} -type d -name "* *")
 if [ ! -z "${DIRS}" ]; then
  mvDir
 fi
}

mvDir
mvFiles

#EOF
Rename files changing spaces to underscore.. recursive
(e.g)

user@PC:$ ./nospace.sh
Renaming ./with space ...done
Renaming ./with_space/other with space ...done
Renaming ./with_space/File with space ...done
Renaming ./with_space/other_with_space/Other file with space ...done


Options:
-h Give this help message
-v Print errors on stderr

Nenhum comentário:

Postar um comentário