简体   繁体   English

查找子目录并删除不包含特定字符串LINUX的文件

[英]Find subdirectory and remove files not containing a specific string LINUX

Given a main path ./class and within class are subdirectories student1 , student2 , student3 ... student100. 给定一个主要路径./class并且在class内是子目录student1student2student3 ... student100. within these subdirectories are other sub-directories history , geography , Math . 在这些子目录中还有其他子目录historygeographyMath Each of these subdirectories have hundreds of files in them. 这些子目录中的每一个都有数百个文件。 I want to KEEP all files that have a string pass in Math ONLY without affecting files in other subjects. 我只想保留所有在Math pass字符串的文件,而不会影响其他主题中的文件。 So far I can cd to Math and do this: 到目前为止,我可以cdMath并执行以下操作:

find . -type f -print0 | xargs --null grep -Z -L 'pass' | xargs --null rm

But it is ineffective to cd to Math in each of the 100 subdirectories and rm unwanted files. 但它是无效的cdMath在各个的100 subdirectoriesrm unwanted文件。

How can one grep only Math and execute the code above? 一个grep如何仅执行Math并执行上面的代码?

以下应该可以解决问题

for i in `find . -type d -name *Math* -exec find {} -type f -not -name *pass* \;`; do rm $i ; done

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

相关问题 如何在 Linux 上查找包含特定文本(字符串)的所有文件? - How to find all files containing specific text (string) on Linux? 删除不包含特定字符串的文件 - Remove files not containing a specific string 如何在Linux中名称包含特定字符串的文件中搜索字符串? - How to search for a string in files with name containing a specific string in linux? 在Terminal / Linux上查找包含特定日期范围文件名的所有文件 - Find all files containing the filename of specific date range on Terminal/Linux Linux脚本查找包含特定格式的字符串并处理数据 - Linux Script to find string containing specific formatting & manipulate the data 如何更改 linux 上包含特定字符串的所有目录/文件的名称 - how to change names of all directories / files containing a specific string on linux 仅打开包含特定字符串的文件,然后在Linux命令行上进行替换 - Open only files containing a specific string and then replace on Linux command line Linux find 将所有文件重新编码到子目录 - Linux find reencode all files to subdirectory 使用 Linux 查找包含特定字符串的文件并复制到目录 - Find Files Containing Certain String and Copy To Directory Using Linux Linux Glob Pattern删除所有包含'?'的文件 - Linux Glob Pattern to remove all files containing a '?'?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM