简体   繁体   English

如何删除具有特定名称的所有子目录中的所有文件?

[英]How can I delete all files in all subdirectories with a certain name?

I have been trying to use the command line to delete all files in all subdirectories with the name s_1_1102_c.jpg.我一直在尝试使用命令行来删除所有子目录中名为 s_1_1102_c.jpg 的所有文件。

This question is similar to what I need How to remove folders with a certain name but it is removing directories and I only want to delete the files with the name s_1_1102_c.jpg.这个问题类似于我需要如何删除具有特定名称的文件夹,但它正在删除目录,我只想删除名称为 s_1_1102_c.jpg 的文件。

I will need to remove this file from 260 subdirectories under the L001 directory.我需要从 L001 目录下的 260 个子目录中删除该文件。 My directory structure is like this:我的目录结构是这样的:

L001
    C5.1
        s_1_1101_a.jpg
        s_1_1101_c.jpg
        s_1_1101_g.jpg
        s_1_1101_t.jpg
        s_1_1102_a.jpg
        s_1_1102_c.jpg
        s_1_1102_g.jpg
        s_1_1102_t.jpg
        s_1_1103_a.jpg
        s_1_1103_c.jpg
        s_1_1103_g.jpg
        s_1_1103_t.jpg
    C6.1
        s_1_1101_a.jpg
        s_1_1101_c.jpg
        s_1_1101_g.jpg
        s_1_1101_t.jpg
        s_1_1102_a.jpg
        s_1_1102_c.jpg
        s_1_1102_g.jpg
        s_1_1102_t.jpg
        s_1_1103_a.jpg
        s_1_1103_c.jpg
        s_1_1103_g.jpg
        s_1_1103_t.jpg

Ultimately I need to remove several files from all subdirectories (s_1_1101_g.jpg, s_1_1101_t.jpg, s_1_1102_a.jpg, s_1_1102_c.jpg, s_1_1102_g.jpg, s_1_1102_t.jpg).最终我需要从所有子目录中删除几个文件(s_1_1101_g.jpg、s_1_1101_t.jpg、s_1_1102_a.jpg、s_1_1102_c.jpg、s_1_1102_g.jpg、s_1_1102_t.jpg)。 So maybe there is a way to provide a list of the file names I need to delete.所以也许有一种方法可以提供我需要删除的文件名列表。

How can I delete these files?如何删除这些文件?

find . -name "s_1_1102_c.jpg" -exec rm -f {} \;

Note: This will find and delete the file in any subdirectory of the current one.注意:这将在当前文件的任何子目录中查找并删除该文件。 So you could execute it in L001 or wherever else you want to do this.因此,您可以在L001或其他任何您想要执行此操作的地方执行它。

for i in s_1_1101_g.jpg s_1_1101_t.jpg s_1_1102_a.jpg s_1_1102_c.jpg s_1_1102_g.jpg s_1_1102_t.jpg; do
  echo rm L001/*/"$i";
done

If output looks fine, remove echo .如果 output 看起来不错,请删除echo

The final method I used to delete my files was given by @Peter - Reinstate Monica我用来删除文件的最后一种方法是由@Peter - Reinstate Monica提供的

for f in s_1_1101_t.jpg s_1_1102_a.jpg s_1_1102_c.jpg s_1_1102_g.jpg s_1_1102_t.jpg s_1_1103_a.jpg s_1_1103_c.jpg s_1_1103_g.jpg s_1_1103_t.jpg s_1_1104_a.jpg s_1_1104_c.jpg s_1_1104_g.jpg s_1_1104_t.jpg s_1_2101_g.jpg s_1_2101_t.jpg s_1_2102_a.jpg s_1_2102_c.jpg s_1_2102_g.jpg s_1_2102_t.jpg s_1_2103_a.jpg s_1_2103_c.jpg s_1_2103_g.jpg s_1_2103_t.jpg s_1_2104_g.jpg s_1_2104_t.jpg; do find /hpc/home/L001 -name $f -delete; done

I was concerned that my file list would be too long but it worked in this situation.我担心我的文件列表会太长,但它在这种情况下有效。

With bash :使用bash

echo rm L001/*/{s_1_1101_g.jpg,s_1_1101_t.jpg,s_1_1102_a.jpg,s_1_1102_c.jpg,s_1_1102_g.jpg,s_1_1102_t.jpg}

or或者

shopt -s globstar
echo rm **/{s_1_1101_g.jpg,s_1_1101_t.jpg,s_1_1102_a.jpg,s_1_1102_c.jpg,s_1_1102_g.jpg,s_1_1102_t.jpg}

If output looks fine, remove echo .如果 output 看起来不错,请删除echo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在所有子目录中找到所有具有相同名称的文件 - how do i find all files with the same name in all subdirectories 如何删除具有特定名称的所有子目录 - How to delete all subdirectories with a specific name 如何删除目录中的所有文件,但名称中包含特定模式的文件除外? - How to delete all files in a dir except ones with a certain pattern in their name? 如何删除Bash中的所有文件和子目录(包括边缘情况) - How to delete all files and subdirectories in Bash including edge cases 如何删除所有子目录,但将这些文件保存在父目录中? - How to delete all the subdirectories but keep those files in the parent directory? 如何从所有子目录中删除文件,而不是从隐藏目录中删除文件? - How do I delete files from all subdirectories but not from hidden directories? 如何向所有子目录中的所有 PHP 文件添加文本块? - How can I add a block of text to all the PHP files in all subdirectories? 使用 find 删除所有子目录(及其文件) - Using find to delete all subdirectories (and their files) 如何将所有文件按扩展名移动到指定DIR目录中的子目录? - How to move all files to subdirectories by extension name in the specified DIR directory? 从所有子目录复制所有具有特定扩展名的文件 - Copy all files with a certain extension from all subdirectories
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM