Shell 问题,当文件名中包含空格无法移动的问题

2014-10-20 19:12:41 +08:00
 andybest
命令为:
mv $(ls /mnt/sda1/t1/*.mp4 | head -n -100) /mnt/sda1/t2

其中 /mnt/sda1/t1/*.mp4 的文件中包含空格:
/mnt/sda1/t1/2014-10-17 05.28.20.000.mp4
/mnt/sda1/t1/2014-10-17 05.38.37.120.mp4
/mnt/sda1/t1/2014-10-17 05.41.07.774.mp4

执行后出现错误:
mv: can't rename '/mnt/sda1/t1/2014-10-17': No such file or directory
mv: can't rename '06.21.58.497.mp4': No such file or directory

将命令改为:
mv "$(ls /mnt/sda1/t1/*.mp4 | head -n -100)" /mnt/sda1/t2
将移动目标用""括起来,结果出现错误:
...
/mnt/sda1/t1/2014-10-17 21.27.43.700.mp4': File name too long

如何解决这个问题?谢谢!
3405 次点击
所在节点    问与答
8 条回复
11138
2014-10-20 19:27:23 +08:00
一次mv的文件太多了吧,可以试一下减少数量或考虑用find来代替。
randal
2014-10-20 19:28:30 +08:00
find /mnt/sda/t1 -type f -name "*.mp4" |head -n 100|xargs -I {} mv {} /mnt/sda1/t2
andybest
2014-10-20 19:47:01 +08:00
@randal 我的 xargs 没有 -I 参数,有什么可以替代的吗?

root@OpenWrt:~# xargs --help
BusyBox v1.22.1 (2014-08-04 22:39:32 CEST) multi-call binary.

Usage: xargs [OPTIONS] [PROG ARGS]

Run PROG on every item given by stdin

-p Ask user whether to run each command
-r Don't run command if input is empty
-0 Input is separated by NUL characters
-t Print the command on stderr before execution
-e[STR] STR stops input processing
-n N Pass no more than N args to PROG
-s N Pass command line of no more than N bytes
-x Exit if size is exceeded
randal
2014-10-20 20:32:05 +08:00
@andytest 应该可以换成 |while read line;do mv "$line" /mnt/sda1/t2/;done
randal
2014-10-20 20:32:47 +08:00
andybest
2014-10-20 20:38:59 +08:00
@randal 成功!!!多谢!给你 100 个赞! +10086 :)))
msg7086
2014-10-20 21:10:42 +08:00
rsync应该内置通配符解析吧,这样就不用在命令行上展开了
msg7086
2014-10-20 21:13:03 +08:00
@msg7086 哦你要前100个。那for或者while循环是最好的了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/140297

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX