V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
andybest
V2EX  ›  问与答

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

  •  
  •   andybest · 2014-10-20 19:12:41 +08:00 · 3368 次点击
    这是一个创建于 3484 天前的主题,其中的信息可能已经有所发展或是发生改变。
    命令为:
    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

    如何解决这个问题?谢谢!
    8 条回复    2014-10-20 21:13:03 +08:00
    11138
        1
    11138  
       2014-10-20 19:27:23 +08:00   ❤️ 1
    一次mv的文件太多了吧,可以试一下减少数量或考虑用find来代替。
    randal
        2
    randal  
       2014-10-20 19:28:30 +08:00   ❤️ 1
    find /mnt/sda/t1 -type f -name "*.mp4" |head -n 100|xargs -I {} mv {} /mnt/sda1/t2
    andybest
        3
    andybest  
    OP
       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
        4
    randal  
       2014-10-20 20:32:05 +08:00   ❤️ 1
    @andytest 应该可以换成 |while read line;do mv "$line" /mnt/sda1/t2/;done
    randal
        5
    randal  
       2014-10-20 20:32:47 +08:00   ❤️ 1
    andybest
        6
    andybest  
    OP
       2014-10-20 20:38:59 +08:00
    @randal 成功!!!多谢!给你 100 个赞! +10086 :)))
    msg7086
        7
    msg7086  
       2014-10-20 21:10:42 +08:00 via iPhone
    rsync应该内置通配符解析吧,这样就不用在命令行上展开了
    msg7086
        8
    msg7086  
       2014-10-20 21:13:03 +08:00 via iPhone
    @msg7086 哦你要前100个。那for或者while循环是最好的了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   982 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 18:36 · PVG 02:36 · LAX 11:36 · JFK 14:36
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.