分页: 1 / 1

求一断shell程序,去掉文件名的空格。

发表于 : 2014-04-11 19:15
iamcook84
如何去掉文件名的空格。比如有文件"ba c.txt",支持中间空格变成"bac.txt"。
在终端中输入
chenwei@linux-bkga:~/program/shell> mv b\ ac.txt bac.txt
chenwei@linux-bkga:~/program/shell>
没有问题。

写了个shell程序如下:
#shell3.sh
#!/bin/bash
fr=ba\ c.txt
to=bac.txt
cp $fr $to

有问题了。
chenwei@linux-bkga:~/program/shell> sh shell3.sh
cp: 目标"bac.txt" 不是目录

请教高人一断shell程序,将目录中所有文件名中的空格去掉。

Re: 求一断shell程序,去掉文件名的空格。

发表于 : 2014-04-11 19:28
YeLee

代码: 全选

 mv "$fr" $to
要加引号,不然参数会被shell解析。
另,建议阁下最好能把第二行跟第一行换一下位置。

Re: 求一断shell程序,去掉文件名的空格。

发表于 : 2014-04-13 8:21
iamcook84
YeLee 写了:

代码: 全选

 mv "$fr" $to
要加引号,不然参数会被shell解析。
另,建议阁下最好能把第二行跟第一行换一下位置。
就是这样的。我试着写了一断较完整的代码:

#!/bin/bash
echo -e "请输入你要转化的目录。\n"
read filepath
find $filepath -name '*\ *.txt' -type f>fromfiles #需要转化的文件名列表
if [ ! -s fromfiles ];then
echo -e "本目录没有含空格的.txt文件。\n"
rm -r fromfiles
exit
fi
sed 's/ *//g' fromfiles >tofiles #去掉文件名中的空格后的文件名列表
gawk '{print NR}' fromfiles | sed -n '$p' >line_numfile
read line_num < line_numfile #总共多少行(多少要转化的文件名)。“wc -l”总是少算一行,弃用。
echo -e "go....\n"
time=1 #记录第几次转化
while [ $line_num -gt 0 ]
do
read frline < fromfiles
read toline < tofiles
echo -e "read to mv $time:\""$frline"\" to \"$toline\"\n"
mv "$frline" $toline
sed '1d' fromfiles >fromfiles_lin
sed '1d' tofiles >tofiles_lin
mv fromfiles_lin fromfiles
mv tofiles_lin tofiles
line_num=`expr $line_num - 1`
time=`expr $time + 1`
done
rm -r fromfiles
rm -r tofiles
rm -r line_numfile
echo -e "ok------------\n"

Re: 求一断shell程序,去掉文件名的空格。

发表于 : 2014-04-13 11:30
cao627

代码: 全选

find $filepath -name '* *' -exec rename 's/ //g' {} \;

Re: 求一断shell程序,去掉文件名的空格。

发表于 : 2014-04-13 16:31
iamcook84
cao627 写了:

代码: 全选

find $filepath -name '* *' -exec rename 's/ //g' {} \;
我试了,不行的。

Re: 求一断shell程序,去掉文件名的空格。

发表于 : 2014-04-13 18:53
cao627
iamcook84 写了:
cao627 写了:

代码: 全选

find $filepath -name '* *' -exec rename 's/ //g' {} \;
我试了,不行的。
x.png

Re: 求一断shell程序,去掉文件名的空格。

发表于 : 2014-04-14 9:07
iamcook84
chenwei@linux-bkga:~/program/shell> touch a\ a.txt b\ b.txt
chenwei@linux-bkga:~/program/shell> mkdir c
chenwei@linux-bkga:~/program/shell> ls
a a.txt b b b.txt c data.txt delet.sh hellohanshu.sh sewdd.txt shell.sh sorfile 按任意键.sh
chenwei@linux-bkga:~/program/shell> find . -name '* *' -exec rename 's/ //g' {} \;
rename: 参数不够

用法:
rename [选项] 表达式 替换文件...

选项:
-v, --verbose 解释正在进行的操作
-s, --symlink 在符号链接上执行

-h, --help 显示此帮助并退出
-V, --version 输出版本信息并退出

更多信息请参阅 rename(1)。
rename: 参数不够

用法:
rename [选项] 表达式 替换文件...

可能是我的rename 版本有问题。我看了man文档,也试了,只能改第一次出现的字符。rename rename " " "" *.txt 如果执行一次,只会把"a b c.txt",改成"ab c.txt"。

chenwei@linux-bkga:~/program/shell> ls
ab c c.txt b c data.txt delet.sh hellohanshu.sh sewdd.txt shell.sh sorfile 按任意键.sh
chenwei@linux-bkga:~/program/shell> rename *\ * 's/ //g' *.txt
rename: ab c c.txt:重命名为 b 失败: 是一个目录
chenwei@linux-bkga:~/program/shell>

Re: 求一断shell程序,去掉文件名的空格。

发表于 : 2014-04-14 9:51
eexpress
rename 's/\ //g' *.c

Re: 求一断shell程序,去掉文件名的空格。

发表于 : 2014-04-14 10:31
cao627

Re: 求一断shell程序,去掉文件名的空格。

发表于 : 2014-04-14 16:40
iamcook84
我的是C语言版本的rename,所以这样成功了。
#!/bin/bash
find . -type f -name "* *" |
while read name; do
na=$(echo $name | sed 's/\ //g')
mv "$name" "$na"
done

太感谢 cao627 了。