函数一、
# replace space with underline batch under a directory
function replace_space_with_underline()
{
dir=$1
cd $dir
find $dir -name "* *" |
while read name; do
na=$(echo $name | tr ' ' '_')
mv "$name" $na
done
}
函数二、
# replace the space under a directory and count
function replace_space_under_directory()
{
handle_file=0
found_mark=0
IFS=$(echo -en "\n\b") # to cope with the space, TAB in name
path=$1
cd $path
for file_name in `ls -1`; do
# 通过grep的返回值来判断
echo "$file_name" | grep -q " "
if [ $? -eq $found_mark ];then
fileName=$file_name
newName=`echo $fileName | sed -e 's/ /_/g'`
mv "$fileName" $newName
let "handle_file=$handle_file+1"
fi
done
echo "$handle_file file renamed."
exit 0
}