19,612
社区成员
发帖
与我相关
我的任务
分享
#!/bin/bash
#下面这句的值改成你实际要修改的主目录
TARGETPATH=/tmp/test
change(){
while read line
do
if echo "$line" | grep -q '[^[:alnum:].\/_]'
then
path=`dirname "$line"`
name=`basename "$line"`
newname=`echo "$name" | sed -e "s/[^a-zA-Z0-9]\{1,\}/-/g"`
if [[ "$name" != "$newname" ]];then
mv "$line" "$path/$newname"
fi
fi
done</tmp/tmp.txt
}
#change file name改文件名
files=`find $TARGETPATH -type f>/tmp/tmp.txt`
change
#change directory name改目录名
files=`find $TARGETPATH -type d>/tmp/tmp.txt`
change
[root@CentOS test]# ls -l
总计 4
-rw-r--r-- 1 root root 0 08-18 16:53 aa bb
-rw-r--r-- 1 root root 0 08-18 16:56 aa___bb
-rw-r--r-- 1 root root 0 08-18 17:22 aa())bb
-rw-r--r-- 1 root root 0 08-18 16:53 aa\bb cc
-rwxr-xr-x 1 root root 187 08-18 17:18 test.sh
[root@CentOS test]# ./test.sh
./aa())bb
[root@CentOS test]# ls -l
总计 4
-rw-r--r-- 1 root root 0 08-18 16:53 aa bb
-rw-r--r-- 1 root root 0 08-18 17:22 aa___bb
-rw-r--r-- 1 root root 0 08-18 16:53 aa\bb cc
-rwxr-xr-x 1 root root 187 08-18 17:18 test.sh
javer@javer-laptop:~/MyProjects/cakes/mvtest$ ls -l
总用量 4
-rw-r--r-- 1 javer javer 0 2010-08-18 16:18 ^&*(
-rw-r--r-- 1 javer javer 0 2010-08-18 16:18 a()
-rw-r--r-- 1 javer javer 0 2010-08-18 16:18 aa++==
-rwxr-xr-x 1 javer javer 181 2010-08-18 16:17 rnm.sh
javer@javer-laptop:~/MyProjects/cakes/mvtest$ ./rnm.sh
./^&*(
./aa++==
./a()
javer@javer-laptop:~/MyProjects/cakes/mvtest$ ls
____ a__ aa____ rnm.sh
#!/bin/bash
files=`find .`
for i in $files
do
if echo $i | grep '[^[:alnum:].\/_]'
then
newname=`echo $i | sed -n 's/[^[:alnum:].\/]/_/g;p'`
mv $i $newname
fi
done
[root@CentOS test]# ls -l
总计 4
-rw-r--r-- 1 root root 0 08-18 16:53 aa bb
-rw-r--r-- 1 root root 0 08-18 16:53 aa___bb
-rw-r--r-- 1 root root 0 08-18 16:54 aa())bb
-rw-r--r-- 1 root root 0 08-18 16:53 aa\bb cc
-rwxr-xr-x 1 root root 186 08-18 16:55 test.sh
[root@CentOS test]# ./test.sh
./aa())bb
[root@CentOS test]# ls -l
总计 4
-rw-r--r-- 1 root root 0 08-18 16:53 aa bb
-rw-r--r-- 1 root root 0 08-18 16:54 aa___bb
-rw-r--r-- 1 root root 0 08-18 16:53 aa\bb cc
-rwxr-xr-x 1 root root 186 08-18 16:55 test.sh
#!/bin/bash
files=`find .`
for i in $files
do
if echo $i | grep '[^[:alnum:].\/_]'
then
newname=`echo $i | sed -n 's/[^[:alnum:].\/]/_/g;p'`
mv $i $newname
fi
done