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

Find subdirectory and remove files not containing a specific string LINUX

给定主路径 ./class 并且在 class 中有子目录 student1student2student3...student100.子目录是其他子目录historygeographyMath。这些子目录中的每一个都有数百个文件。我想保留所有在 Math 中具有字符串 pass 的文件,而不影响其他主题中的文件。到目前为止,我可以 cdMath 并执行此操作:

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

但是在100 subdirectoriesrmunwanted文件中cdMath是无效的

一个grep如何只Math执行上面的代码?

下面应该可以解决问题

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