简体   繁体   English

递归查找和打开文件

[英]Recursively find and open files

I want to search through all subdirectories and files to find files with a specific extension. 我想搜索所有子目录和文件,以查找具有特定扩展名的文件。 When I find a file with the extension, I need to open it, find a specific string from within the file and store it within a txt file. 当我找到带有扩展名的文件时,需要打开它,在文件中找到一个特定的字符串,并将其存储在txt文件中。

This is what I have so far for finding all of the correct files: 到目前为止,这是我找到所有正确文件的方法:

find . -name ".ext" ! -path './ExcludeThis*'

This is what I have for opening the file and getting the part of the file I want and storing it: 这是我打开文件并获取想要的文件部分并将其存储的功能:

LINE=$(head .ext | grep search_string)
SUBSTR=$(echo $LINE | cut -f2 -d '"')
echo $SUBSTR >> results.txt

I am struggling for how to combine the 2 together, I have looked at 'for f in **/*' and then run an if statement in there to see if it matches the .ext and remove the need for find all together but **/* seems to work on directories only and not files. 我为如何将2组合在一起而苦苦挣扎,我研究了'for in in ** / *',然后在其中运行if语句以查看它是否与.ext匹配,并消除了除*以外全部查找的需要。 * / *似乎仅适用于目录,而不适用于文件。

A break down of any solutions would be very much appreciated too, I am new to shell scripting. 任何解决方案的分解也将不胜感激,我是Shell脚本的新手。 Thanks. 谢谢。

find -name "*.ext" \! -path './ExcludeThis*' -exec head -q '{}' \+ | 
    grep search_string | cut -f2 -d'"' >> results.txt

find explanation 找到解释

find -name "*.ext" \! -path './ExcludeThis*' -exec head -q '{}' \+

For each file name matched, executes head (with \\+ , the command line is built by appending each selected file name at the end, so the total number of invocations of the command will be much less than the number of matched files ). 对于匹配的每个文件名,执行head (使用\\+ ,通过在每个末尾附加每个选定的文件名来构建命令行,因此命令的调用总数将远小于匹配的文件数 )。

Notice I replaced .ext with *.ext (the first way just math a file named exactly .ext ), and ! 请注意,我换成.ext*.ext (第一种方式只是数学名为恰好文件.ext ),以及! with \\! \\! (protection from interpretation by the shell). (防止外壳解释)。

The head option -q is necessary because that command prints headers when used with multiple files (due to \\+ in this case). head选项-q是必需的,因为该命令与多个文件一起使用时会打印头(在这种情况下,由于使用\\+ )。

In addition, if no path is given, the default is taken ( . ). 另外,如果未给出路径,则采用默认值( . )。 ie: find . -name 即: find . -name find . -name = find -name . find . -name = find -name


pipeline explanation 管道说明

<find ... -exec head> | grep search_string | cut -f2 -d'"' >> results.txt
  • While head write the lines (10 by default) for every file in the pipe, grep read them. head编写管道中每个文件的行(默认情况下为10行),而grep读取它们。
  • If grep matches search_string in some of them, write those lines in the next pipe. 如果grep在其中某些匹配search_string情况下,请将这些行写在下一个管道中。
  • At the same time, cut take the second fields (delimited by " ) of every line and appends them in results.txt 同时, cut采用每行的第二个字段(由"分隔)并将它们附加在results.txt

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

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