V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
server
V2EX  ›  Linux

求教下 Linux mv a/* b/* ,文件恢复 和 mv 执行过程

  •  
  •   server ·
    exuan · 2020-04-27 17:22:47 +08:00 · 3966 次点击
    这是一个创建于 1453 天前的主题,其中的信息可能已经有所发展或是发生改变。

    RT.

    mv a/* b/*
    

    今天试了一个 mv 操作,将 a 的内容迁移到空目录 b 下,执行了上面的命令。结果 a 空了, b 目录下有个了*

    total 8
    ~/space/workspace/t/b ls -an   
    -rw-r--r-- 1 1000 1000    0  4 月 27 15:44 '*'
    drwxr-xr-x 2 1000 1000 4096  4 月 27 15:44  .
    drwxr-xr-x 4 1000 1000 4096  4 月 27 15:44  ..
    
    
    11 条回复    2020-05-06 15:09:37 +08:00
    taolu
        1
    taolu  
       2020-04-27 17:53:20 +08:00   ❤️ 1
    如果 a 下有多个文件,这样操作是不允许的,因此 a 下原本就一个文件。把 b/* 这个文件重命名即可,`mv \* new_name`
    julyclyde
        2
    julyclyde  
       2020-04-27 18:13:45 +08:00
    这里主要的问题是,你误以为
    b/*
    是目标

    但其实 b/*展开之后,最后一个元素才是目标
    ddzzhen
        3
    ddzzhen  
       2020-04-27 18:24:39 +08:00 via Android
    mv a/* b/
    superrichman
        4
    superrichman  
       2020-04-27 18:29:12 +08:00 via iPhone
    可能文件已经回不来了? 跟移动到 /dev/null 差不多?
    iFollow
        5
    iFollow  
       2020-04-27 19:24:44 +08:00
    @taolu
    可是 b 目录下新出来的 * 文件大小是 0 ?

    @julyclyde
    题主说 b 是空目录。

    我在 Ubuntu 16.04 上的测试结果:
    :$ ls a
    a1 a2 a3
    :$
    :$ ls b
    :$
    :$ mv a/* b/*
    mv: target 'b/*' is not a directory
    :$
    :$ ls a
    a1 a2 a3
    :$ ls b
    :$

    直接报错了,没有执行操作。
    server
        6
    server  
    OP
       2020-04-28 10:39:55 +08:00
    @iFollow zsh 下会报错, 做了 alias
    https://github.com/ohmyzsh/ohmyzsh/blob/9817e1e7ff62494abf9d7c78870e92908567e8f4/plugins/common-aliases/README.md#file-handling

    ```zsh
    mv mv -i Move a file
    ```

    ```bash
    -i, --interactive
    prompt before overwrite

    ```
    taolu
        7
    taolu  
       2020-04-28 13:07:25 +08:00
    @server
    ```bash
    root@ubuntu:~# docker run -it --rm debian bash
    root@fa9a52396d6f:/# cd
    root@fa9a52396d6f:~# mkdir a b
    root@fa9a52396d6f:~# touch a/{1..10}.txt
    root@fa9a52396d6f:~# ls -R a
    a:
    1.txt 10.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt 9.txt
    root@fa9a52396d6f:~# /bin/mv a/* b/*
    /bin/mv: target 'b/*' is not a directory
    root@fa9a52396d6f:~# /bin/mv -f a/* b/*
    /bin/mv: target 'b/*' is not a directory
    root@fa9a52396d6f:~# rm a/{2..10}.txt
    root@fa9a52396d6f:~# ls -R a
    a:
    1.txt
    root@fa9a52396d6f:~# /bin/mv a/* b/*
    root@fa9a52396d6f:~# ls b/
    '*'
    ```
    server
        8
    server  
    OP
       2020-04-28 14:35:52 +08:00
    @taolu 确实,试了几个发行版,b/* 是 a/1.txt

    说下事情经过
    昨天 有个小伙伴 x51 云一个盘满了,开了一个新盘,就搞了个 mv a/* b/*,后面就是一顿猛虎操作(重启大法),重启后 原目录正常。
    总结下:
    1. 跨盘 mv 没有创建多文件(夹),多版本测试,给 v2 友造成困扰
    2. 很有可能小伙伴 乌龙了
    3. 感谢 @taolu
    c7in7
        9
    c7in7  
       2020-04-28 15:32:43 +08:00
    我试一下去
    c7in7
        10
    c7in7  
       2020-04-28 15:34:54 +08:00
    [root@c7in7 ~]# mkdir a
    [root@c7in7 ~]# mkdir b
    [root@c7in7 ~]# cd a
    [root@c7in7 a]# touch 1.txt 2.txt
    [root@c7in7 a]# ls
    1.txt 2.txt
    [root@c7in7 a]# cd ..
    [root@c7in7 ~]# mv a/* b/*
    mv: 目标"b/*" 不是目录
    aloxaf
        11
    aloxaf  
       2020-05-06 15:09:37 +08:00
    这种问题牢记一点就可以了——通配符是由 shell 展开的(这点和 Windows 不同),而且不同 shell 下的行为可能有细微的差别

    所以 `mv a/* b/*` 就相当于 `mv a 下的文件 1 a 下的文件 2 ... b 下的文件 1 b 下的文件 2 ...`
    而当通配符展开失败时(比如 b 目录下没有文件),bash 的默认行为是原样输出,而 zsh 默认行为则是直接报错(赞一波 zsh )
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3101 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 10:57 · PVG 18:57 · LAX 03:57 · JFK 06:57
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.