【问题标题】:How to use 'mv' command to move files except those in a specific directory?如何使用“mv”命令移动特定目录中的文件除外?
【发布时间】:2011-06-04 11:20:09
【问题描述】:

我想知道 - 我如何移动目录中的所有文件,除了特定目录中的文件(因为 'mv' 没有 '--exclude' 选项)?

【问题讨论】:

标签: linux mv


【解决方案1】:

让我们假设目录结构是这样的,

|parent
    |--child1
    |--child2
    |--grandChild1
    |--grandChild2
    |--grandChild3
    |--grandChild4
    |--grandChild5
    |--grandChild6

我们需要移动文件,使其看起来像,

|parent
    |--child1
    |   |--grandChild1
    |   |--grandChild2
    |   |--grandChild3
    |   |--grandChild4
    |   |--grandChild5
    |   |--grandChild6
    |--child2

在这种情况下,您需要排除两个目录child1child2,并将其余目录移动到child1 目录中。

使用,

mv !(child1|child2) child1

这会将所有其余目录移动到child1 目录中。

【讨论】:

  • 提示:但是请注意,使用此模式依赖于extglob。您可以使用shopt -s extglob 启用它(如果您希望默认打开extended globs,您可以将shopt -s extglob 添加到.bashrc)
  • 我在尝试执行此命令时不断收到-bash: !: event not found
  • Derp,刚刚意识到上面的注释是 bash 所必需的。
  • 这怎么能强制?除了忽略的目录之外没有其他目录时? mv -f 似乎不起作用。我通过创建一个临时目录解决了这个问题,所以总共有三个目录,然后移动所有目录,然后删除临时目录。丑!
  • 小心这个你真正想要everything在那个目录的那个目录!
【解决方案2】:

由于 find 确实有排除选项,请使用 find + xargs + mv:

find /source/directory -name ignore-directory-name -prune -print0 | xargs -0 mv --target-directory=/target/directory

请注意,这几乎是从 find 手册页中复制的(我认为使用 mv --target-directory 比 cpio 更好)。

【讨论】:

    【解决方案3】:

    这并不完全符合您的要求,但它可能会完成这项工作:

    mv the-folder-you-want-to-exclude somewhere-outside-of-the-main-tree
    mv the-tree where-you-want-it
    mv the-excluded-folder original-location
    

    (本质上,将排除的文件夹移出要移动的较大树。)

    所以,如果我有 a/ 并且我想排除 a/b/c/*

    mv a/b/c ../c
    mv a final_destination
    mkdir -p a/b
    mv ../c a/b/c
    

    或者类似的东西。否则,您也许可以得到find 的帮助。

    【讨论】:

    • 如果你移动的文件在你的根目录下?例如,一些系统只允许用户访问一个目录(一些云 IDE 就是一个很好的例子)。也许有人想将所有内容移动到子目录。这行不通。
    • 正如我所说(六年前),答案并没有准确描述所要求的内容,它只有可能完成这项工作。在大多数情况下,它应该可以工作;当然,你的是个例外。
    • 你是对的,它没有。但我的意思不是这不是所要求的,而是在某些情况下它不起作用。我的例子只是一个假设来证明这一点。
    【解决方案4】:

    首先获取文件和文件夹的名称并排除您想要的:

    ls --ignore=file1 --ignore==folder1 --ignore==regular-expression1 ...
    

    然后将过滤后的名称作为第一个参数传递给mv,第二个参数将是目标:

    mv $(ls --ignore=file1 --ignore==folder1 --ignore==regular-expression1 ...) destination/
    

    【讨论】:

      【解决方案5】:

      这会将当前目录下的所有文件移动到/exclude/目录下的所有文件到/wherever...

      find -E . -not -type d -and -not -regex '\./exclude/.*' -exec echo mv {} /wherever \;
      

      【讨论】:

      • 这将忽略目录结构并将子目录中的所有文件复制到一个平面文件夹结构中。 a/b/c/ -> /wherever a/b.txt -> /wherever
      • 我收到了unknown predicate '-E'
      【解决方案6】:
      #!/bin/bash
      
      touch apple  banana  carrot  dog  cherry
      
      mkdir fruit
      
      F="apple  banana  carrot  dog cherry"
      
      mv ${F/dog/} fruit
      

      # 这会从列表 F 中删除 'dog',所以它仍保留在 当前目录而不是移动到'fruit'

      【讨论】:

        【解决方案7】:
        ls | grep -v exclude-dir | xargs -t -I '{}' mv {} exclude-dir
        

        【讨论】:

        • 您想为您的纯代码答案添加一些解释吗?这将有助于消除 StackOverflow 是免费代码编写服务的误解。另外,看看这里以改善外观:stackoverflow.com/editing-help
        【解决方案8】:
        mv * exclude-dir
        

        对我来说是完美的解决方案

        【讨论】:

          【解决方案9】:

          重命名您的目录以使其隐藏,这样通配符就看不到它:

          mv specific_dir .specific_dir 
          mv * ../other_dir
          

          【讨论】:

            猜你喜欢
            • 2020-04-22
            • 2021-01-24
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-08-26
            • 2015-03-21
            • 2015-02-20
            • 2022-04-28
            相关资源
            最近更新 更多