简体   繁体   English

删除所有包含特定字符串的文件 - Bash

[英]Remove all files contain specific string - Bash

I have these bad data我有这些坏数据

AWS-Console.pngcrop-AWS-Console.png                                                                                    
Alimofire.pngcrop-Alimofire.png                                                                                        
Amazon-ECR-.pngcrop-Amazon-ECR-.png                                                                                    
Amazon-ECS.pngcrop-Amazon-ECS.png                                                                                      
Amazon-RDS.pngcrop-Amazon-RDS.png                                                                                      
Angular.pngcrop-Angular.png                                                                                            
AngularJS.pngcrop-AngularJS.png 
.... 1000 more

I'm trying to delete them我正在尝试删除它们

I've tried我试过了

ls public/assets/fe/img/skill/ | grep crop | rm -rf *crop*
ls public/assets/fe/img/skill/ | grep crop | rm -rf
rm -rf $(ls public/assets/fe/img/skill/ | grep crop)

None of them work...他们都没有工作...

rm can handle the glob expressions that ls handles: rm可以处理ls处理的 glob 表达式:

rm public/assets/fe/img/skill/*crop*

Use the find command instead请改用find命令

find . -name "*crop*" -type f -exec rm -i {} \;
  • -type f will specify to search file only and avoid directories -type f将指定仅搜索文件并避免目录
  • -exec requires the command input to end with \; -exec要求命令输入以\; , the {} being substitute by the result of the command , {}被命令的结果替代
  • the -i will ask you to confirm; -i会要求您确认; remove it once sure what you do.确定您要做什么后将其删除。
  • advice display the result beforehand with -print in place of -exec...建议-print代替-exec...

     find. -name "*crop*" -type f -print

More here where your question would find more accurate answers更多在这里你的问题会找到更准确的答案

The main problem in your commands is the missing path in the output of the ls command.命令中的主要问题是ls命令的 output 中缺少路径。

ls public/assets/fe/img/skill/ | grep crop ls public/assets/fe/img/skill/ | grep crop will retur eg AWS-Console.pngcrop-AWS-Console.png which is passed to rm . ls public/assets/fe/img/skill/ | grep crop将返回例如AWS-Console.pngcrop-AWS-Console.png传递给rm But rm AWS-Console.pngcrop-AWS-Console.png fails because there is no such file in the current directory.但是rm AWS-Console.pngcrop-AWS-Console.png失败,因为当前目录中没有这样的文件。 It should be rm public/assets/fe/img/skill/AWS-Console.pngcrop-AWS-Console.png instead.应该改为rm public/assets/fe/img/skill/AWS-Console.pngcrop-AWS-Console.png

Adding -d to the ls command should do the trick:-d添加到ls命令应该可以解决问题:

ls -d public/assets/fe/img/skill/ | grep crop | rm -rf
rm -rf $(ls -d public/assets/fe/img/skill/ | grep crop)

As pointed out in other answers, other solutions exist, including:正如其他答案中指出的那样,存在其他解决方案,包括:

rm public/assets/fe/img/skill/*crop*
find public/assets/fe/img/skill/ -name "*crop*" -type f -exec rm -i {} \;

If it's a really large number of files (apparently wasn't in your case), xargs can speed up the process .如果它真的有大量文件(显然不是你的情况), xargs可以加快这个过程 This applies for a lot of things you might want to read from a pipe.这适用于您可能想从 pipe 中读取的许多内容。

 find . -name "*crop*" -type f | xargs rm

The main advantage of using find here is that it's an easy way to ignore directories.在这里使用find的主要优点是它是一种忽略目录的简单方法。 If that's not an issue, let the OS handle all that.如果这不是问题,让操作系统处理所有这些。

printf "%s\n" public/assets/fe/img/skill/*crop* | xargs rm

If you need to be able to pick up files in subdirectories -如果您需要能够子目录中获取文件 -

shopt -s globstar # double asterisks not include arbitrary preceding paths
printf "%s\n" public/assets/fe/img/skill/**crop* | xargs rm

You might want to look over the list first, though.不过,您可能想先查看一下列表。

printf "%s\n" public/assets/fe/img/skill/*crop* >crop.lst 
# check the list - vi, grep, whatever satisfies you.
xargs rm < crop.lst  # fast-delete them in bulk 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM