[英]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.