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

sh/bash/dash/ksh/zsh等Shell脚本
回复
iamcook84
帖子: 41
注册时间: 2013-08-29 9:27

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

#1

帖子 iamcook84 » 2014-04-11 19:15

如何去掉文件名的空格。比如有文件"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程序,将目录中所有文件名中的空格去掉。
上次由 iamcook84 在 2014-04-12 8:30,总共编辑 1 次。
头像
YeLee
论坛版主
帖子: 26406
注册时间: 2008-08-13 8:48
系统: Fundu i64
来自: 东海硇州,一双管钥。
联系:

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

#2

帖子 YeLee » 2014-04-11 19:28

代码: 全选

 mv "$fr" $to
要加引号,不然参数会被shell解析。
另,建议阁下最好能把第二行跟第一行换一下位置。
◎当我站在道德的高度上俯视别人的时候,发现自己是多么渺小。
♥执着但不偏激,反对而不排斥,坚决捍卫矛盾体的存在方式。
★★★天气预报★★★
fcitx-yatable一个可以使用的码表输入法
[教程]几个实例攻克软件编译难关
Gentoo Development Guide
字体相关
iamcook84
帖子: 41
注册时间: 2013-08-29 9:27

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

#3

帖子 iamcook84 » 2014-04-13 8:21

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"
cao627
帖子: 992
注册时间: 2007-12-05 10:57
系统: ubuntu14.04
来自: 金山

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

#4

帖子 cao627 » 2014-04-13 11:30

代码: 全选

find $filepath -name '* *' -exec rename 's/ //g' {} \;
iamcook84
帖子: 41
注册时间: 2013-08-29 9:27

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

#5

帖子 iamcook84 » 2014-04-13 16:31

cao627 写了:

代码: 全选

find $filepath -name '* *' -exec rename 's/ //g' {} \;
我试了,不行的。
cao627
帖子: 992
注册时间: 2007-12-05 10:57
系统: ubuntu14.04
来自: 金山

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

#6

帖子 cao627 » 2014-04-13 18:53

iamcook84 写了:
cao627 写了:

代码: 全选

find $filepath -name '* *' -exec rename 's/ //g' {} \;
我试了,不行的。
x.png
iamcook84
帖子: 41
注册时间: 2013-08-29 9:27

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

#7

帖子 iamcook84 » 2014-04-14 9:07

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>
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

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

#8

帖子 eexpress » 2014-04-14 9:51

rename 's/\ //g' *.c
● 鸣学
cao627
帖子: 992
注册时间: 2007-12-05 10:57
系统: ubuntu14.04
来自: 金山

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

#9

帖子 cao627 » 2014-04-14 10:31

iamcook84
帖子: 41
注册时间: 2013-08-29 9:27

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

#10

帖子 iamcook84 » 2014-04-14 16:40

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

太感谢 cao627 了。
回复