当前位置: 技术问答>linux和unix
批量修改特殊符号文件夹及文件SHELL
来源: 互联网 发布时间:2016-09-21
本文导语: 目录结构 root ddd | -----roo(rr) | ----roo..ff( 要求需要把特殊体都替换成_,重复多次的变成单个。。 | #!/bin/bash #下面这句的值改成你实际要修改的主目录 TARGETPATH=/...
目录结构
root ddd
|
-----roo(rr)
|
----roo..ff(
要求需要把特殊体都替换成_,重复多次的变成单个。。
root ddd
|
-----roo(rr)
|
----roo..ff(
要求需要把特殊体都替换成_,重复多次的变成单个。。
|
#!/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
#change directory name改目录名
files=`find $TARGETPATH -type d>/tmp/tmp.txt`
change
#下面这句的值改成你实际要修改的主目录
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
#change directory name改目录名
files=`find $TARGETPATH -type d>/tmp/tmp.txt`
change
|
try:
find $(pwd) -type d |
awk 'NR>1{printf "mv 42"$0"42 "}{gsub(/[(|) ]/,"_");gsub(/([_])+/,"_");print}'|sh
find $(pwd) -type d |
awk 'NR>1{printf "mv 42"$0"42 "}{gsub(/[(|) ]/,"_");gsub(/([_])+/,"_");print}'|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
#下面这句的值改成你实际要修改的主目录
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
#change directory name改目录名
files=`find $TARGETPATH -type d>/tmp/tmp.txt`
change