[英]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
内是子目录student1
, student2
, student3
... student100.
within these subdirectories are other sub-directories history
, geography
, Math
. 在这些子目录中还有其他子目录
history
, geography
和Math
。 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: 到目前为止,我可以
cd
到Math
并执行以下操作:
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. 但它是无效的
cd
到Math
在各个的100 subdirectories
和rm
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.