【问题标题】:How do I delete all the MP4 files with a file name not ending with -converted?如何删除文件名不以 -converted 结尾的所有 MP4 文件?
【发布时间】:2020-01-16 12:32:49
【问题描述】:

我使用 VLC 转换/压缩了几个文件夹中的几个 MP4 文件。 例如,转换/压缩文件的名称以 -converted 结尾。 2. bubble sort-converted.mp4.

进入每个文件夹并删除所有原始文件并留下转换后的文件确实很麻烦。

我想使用一些 zsh/bash 命令递归地删除所有原始文件并保留转换后的文件。 例如,我将删除3 - sorting/2. bubble sort.mp4 并留下3 - sorting/2. bubble sort-converted.mp4

TLDR; 简而言之,使用一些 zsh/bash 命令删除所有扩展名为 .mp4 的文件,其中文件名不以 -converted 结尾。

另外,如果在删除原始文件后有办法将转换后的文件重命名为原始名称,那将是一个加号。

谢谢!

【问题讨论】:

标签: bash zsh


【解决方案1】:

find 可以与逻辑表达式一起使用以匹配所需的文件并删除它们。

在您的情况下,可以使用以下内容来验证它是否与您要删除的文件匹配。它会查找名称中没有converted 但以.mp4 结尾的所有文件。

find . -type f -not \( -name '*converted*' \) -a -name "*.mp4"

一旦您对文件列表结果感到满意,然后添加 -delete 以进行实际删除。

find . -type f -not \( -name '*converted*' \) -a -name "*.mp4" -delete

【讨论】:

    【解决方案2】:

    试试这个:

    find . -name '*.mp4' | grep -v 'converted' | xargs rm -f
    

    【讨论】:

      【解决方案3】:

      zsh 纯解决方案:

      rm -f ^(*.mp4-converted)(.)
      
      ^ ................. negates
      *-converted ....... pattern
      (.) ............... regular files
      

      使用 gnu 并行(在许多文件的情况下)

      parallel --no-notice rm -rf ::: ^(*converted)(.)
      

      【讨论】:

        【解决方案4】:

        即使您的文件名包含 '、" 或空格,这也会起作用:

        find . -name '*.mp4' |
          grep -v 'converted' |
          parallel -X rm -f
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-05-03
          • 1970-01-01
          • 2019-07-21
          • 2015-06-20
          • 2020-08-31
          • 1970-01-01
          • 2022-12-28
          • 2017-03-07
          相关资源
          最近更新 更多