递归查找包含所有给定模式的文件

时间:2017-05-26 20:09:02

标签: bash grep find exec

我有一个目录 / Folder1 / Folder2 ,其中有多个项目包含多个文件。 某些文件的扩展名为“.ext”。

某些“.ext”文件包含“Word1”,另一个“.ext”文件包含“Word2”,其他文件包含“Word3”。

我想知道如何只找到包含“Word1” AND “Word2” AND “Word3”的“.ext”文件( 3 单词)递归...

使用查找命令可以过滤文件。

find /Folder1/Folder2  -iname "*.ext" -exec grep -i 'Word1' {} \;

使用 grep

grep -inR "Word1" --include \*.ext /Folder1/Folder2 
grep -inR "Word2" --include \*.ext /Folder1/Folder2
grep -inR "Word3" --include \*.ext /Folder1/Folder2

但是,如何使用组合 grep 命令?

下一个solution对我不起作用。

find /Folder1/Folder2 -iname "*.eXt" | xargs grep 'Word1' -sl | xargs grep 'Word2' -sl| xargs grep 'Word3' -sl

EDIT1

我找到了&我正在测试这个:

find /Folder1/Folder2 -iname "*.ext" -exec grep -qi 'Word1' {} \; -exec grep -qi 'Word2' {} \; -print
find /Users/joseluisbz/Google\ Drive/Developer/a\ TableCellRenderer  -iname "*.java" -exec grep -qi 'UI' {} \; -exec grep -qi 'Slider' {} \; -print

结果

joseluisbz-MacBook:~ joseluisbz$ find /Users/joseluisbz/Google\ Drive/Developer/a\ TableCellRenderer  -iname "*.java" -exec grep -qi 'UI' {} \; -exec grep -qi 'Slider' {} \; -print
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/ChangeLafAtRuntineDemo.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/ChangeLafAtRuntineDemo.new.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/ChangeLafAtRuntineDemoOld.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/ColorThumbJSlider.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/JFr_SplitPaneMotif.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/JLayeredPaneJSliderLAF.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/JSliderLAF.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/JSliderTableExample.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/RedGreenSliderUI.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/TestMultiSlider.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/TestSliderLayeredPane.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/UIManagerDefaults.java

检查另一种方法!

$ while IFS= read -d '' -r file; do     grep -q 'UI' "$file" &&      grep -q 'Slider' "$file";     echo "$file"; done < <(find /Users/joseluisbz/Google\ Drive/Developer/a\ TableCellRenderer -iname '*.java' -print0)
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/AnotherPanelInTable.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/AnotherSeparatorSample.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/AutoCompleteComboBox.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/ButtonGroupExample.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/ChangeLafAtRuntineDemo.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/ChangeLafAtRuntineDemo.new.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/ChangeLafAtRuntineDemo.oldWorking.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/ChangeLafAtRuntineDemoOld.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/ClientServerShort.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/ClientServerTCP.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/ColorComboBoxEditor.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/ColorComboBoxEditorRendererDemo.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/ColorEditor.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/ColorRenderer.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/ColorThumbJSlider.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/ComboBoxCustomScroller.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/CustomImageButton.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/CustomJComboBox/src/net/codejava/swing/combobox/CountryComboBox.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/CustomJComboBox/src/net/codejava/swing/combobox/CountryItemEditor.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/CustomJComboBox/src/net/codejava/swing/combobox/CountryItemRenderer.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/CustomJComboBox/src/net/codejava/swing/combobox/CustomComboBoxTester.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/CustomRendererEditorTableExample.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/DecimalSpinner.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/EjemploJLayeredPane.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/FloatSlider.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/JA_JButton/src/JF_Test.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/JButtonTableExample.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/JFr_SplitPaneMotif.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/JInteractiveTableExample.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/JLayeredPaneJSliderLAF.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/JPopupMenuTest.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/JRadioButtonTableExample.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/JSliderLAF.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/JSliderTableExample.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/JSpinnerTableExample.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/JTabbedText.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/LAFJButton.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/LayeredPaneDemo.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/LayeredPaneDemo2.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/LayeredPaneLayout.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/ListDemo.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/NullPointerExceptionSynthTableUI.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/OverlapLayout.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/PopupJOptionPane.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/RedGreenSliderUI.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/SeparatorExample.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/SimpleLayers.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/SliderSample.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/TableDialogEditDemo.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/TableJButtonSelfWork.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/TableTestCheckBox.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/TableWithPanelContainerOfSpinnerAndRadioButton.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/TableWithPanelContainerOfSpinnerAndRadioButton.new.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/TestComboScroll.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/TestMultiSlider.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/TestSliderLayeredPane.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/TransparentBackgroundSlider.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/TransparentSlider.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/UIManagerDefaults.java
/Users/joseluisbz/Google Drive/Developer/a TableCellRenderer/UsingJLayeredPane.java
joseluisbz-MacBook:~ joseluisbz$ 

2 个答案:

答案 0 :(得分:1)

您可以使用此find命令查找所有*.ext文件,然后使用grep命令搜索3个字:

while IFS= read -d '' -r file; do
    grep -q 'Word1' "$file" && 
    grep -q 'Word2' "$file" &&
    grep -q 'Word3' "$file" &&
    echo "$file"
done < <(find /Folder1/Folder2 -iname '*.ext' -print0)

答案 1 :(得分:0)

  

如何仅查找&#34; .ext&#34;包含&#34; Word1&#34;的文件AND&#34; Word2&#34; AND&#34; Word3&#34;   我的目录中的(三个字)递归地

查找 + awk &#34; hack&#34;:

find /Folder1/Folder2 -iname "*.ext" | xargs awk -v RS= '$0~/Word1/ && $0~/Word2/ && $0~/Word3/{print FILENAME}'
  • -v RS= - 将每个文件内容视为一行

  • '$0~/Word1/ && $0~/Word2/ && $0~/Word3/{print FILENAME} - 仅打印包含所有三个单词的文件名