【问题标题】:move files that no not contain specific string移动不包含特定字符串的文件
【发布时间】:2019-10-13 14:53:56
【问题描述】:

我想使用mv 移动文件名中不包含字母S 的文件。在 mv 手册中找不到任何内容。也许与findgrep 结合使用?它必须区分大小写。

输入:

  file1
  fileS1
  file2
  fileS2

要移动的文件:

  file1
  file2

【问题讨论】:

    标签: bash find mv


    【解决方案1】:

    如果您启用 extended globbing,您可以在纯 Bash 中进行选择,而无需任何额外的软件,默认情况下它是关闭的:

    shopt -s extglob
    mv !(*S*) /target/dir
    

    如需更多信息,请在bash(1) 手册页中搜索extglob(信息在第二个匹配项中)。

    【讨论】:

      【解决方案2】:

      您也可以使用ls 中的忽略模式开关,例如:

      mv $(ls -I '*S*') /target/dir
      

      【讨论】:

        【解决方案3】:

        GREP-v 标志也可以在这里使用。根据文档,

        -v, --invert-match 反转匹配感,选择不匹配的行。

        随便用

        ls | grep -v '*S*' | xargs mv -t target_dir/
        

        另外,请参阅this 帖子。

        【讨论】:

          【解决方案4】:

          例如,您可以将find-not 标志一起使用。

          find /path/to/source/dir -type f -not -name '*S*' \
              | xargs mv -t /path/to/target/dir
          

          【讨论】:

            猜你喜欢
            • 2012-07-02
            • 1970-01-01
            • 2022-11-10
            • 1970-01-01
            • 2021-12-10
            • 1970-01-01
            • 2019-07-28
            • 1970-01-01
            • 2010-11-22
            相关资源
            最近更新 更多