简体   繁体   English

如何使用bash或Perl脚本遍历目录树?

[英]How can I traverse a directory tree using a bash or Perl script?

I am interested into getting into bash scripting and would like to know how you can traverse a unix directory and log the path to the file you are currently looking at if it matches a regex criteria. 我有兴趣进入bash脚本,并想知道如何遍历unix目录并记录当前正在查看的文件的路径,如果它符合正则表达式标准。

It would go like this: 它会是这样的:

  • Traverse a large unix directory path file/folder structure. 遍历大型unix目录路径文件/文件夹结构。
  • If the current file's contents contained a string that matched one or more regex expressions, 如果当前文件的内容包含与一个或多个正则表达式匹配的字符串,
  • Then append the file's full path to a results text file. 然后将文件的完整路径附加到结果文本文件。

Bash or Perl scripts are fine, although I would prefer how you would do this using a bash script with grep , awk , etc commands. Bash或Perl脚本很好,虽然我更喜欢你如何使用带有grepawk等命令的bash脚本来实现这一点。

find . -type f -print0 | xargs -0 grep -l -E 'some_regexp' > /tmp/list.of.files

Important parts: 重要部分:

  • -type f makes the find list only files -type f使查找列表只有文件
  • -print0 prints the files separated not by \\n but by \\0 - it is here to make sure it will work in case you have files with spaces in their names -print0打印的文件不是由\\ n分隔,而是由\\ 0打印 - 这是为了确保它有效,以防你的名字中有空格的文件
  • xargs -0 - splits input on \\0, and passes each element as argument to the command you provided (grep in this example) xargs -0 - 在\\ 0上拆分输入,并将每个元素作为参数传递给您提供的命令(在本例中为grep)

The cool thing with using xargs is, that if your directory contains really a lot of files, you can speed up the process by paralleling it: 使用xargs的好处是,如果你的目录包含很多文件,你可以通过并行来加速这个过程:

find . -type f -print0 | xargs -0 -P 5 -L 100 grep -l -E 'some_regexp' > /tmp/list.of.files

This will run the grep command in 5 separate copies, each scanning another set of up to 100 files 这将以5个单独的副本运行grep命令,每个副本扫描另一组最多100个文件

use find and grep 使用find和grep

find . -exec grep -l -e 'myregex' {} \; >> outfile.txt

-l on the grep gets just the file name grep上的-l只获取文件名

-e on the grep specifies a regex -e on the grep指定正则表达式

{} places each file found by the find command on the end of the grep command {}将find命令找到的每个文件放在grep命令的末尾

>> outfile.txt appends to the text file >> outfile.txt附加到文本文件

grep -l -R <regex> <location>应该完成这项工作。

If you wanted to do this from within Perl, you can take the find commands that people suggested and turn them into a Perl script with find2perl : 如果你想在Perl中执行此操作,可以使用人们建议的find命令并将它们转换为带有find2perl的Perl脚本:

If you have: 如果你有:

$ find ...

make that 做那个

$ find2perl ...

That outputs a Perl program that does the same thing. 这会输出一个执行相同操作的Perl程序。 From there, if you need to do something that easy in Perl but hard in shell, you just extend the Perl program. 从那里开始,如果你需要在Perl中做一些简单但在shell中很难的东西,你只需要扩展Perl程序。

find /path -type f -name "*.txt" | awk '
{
    while((getline line<$0)>0){
        if(line ~ /pattern/){
            print $0":"line
            #do some other things here
        }
    }    
}'

similar thread 类似的线程

find /path -type f -name "outfile.txt" | awk '
{
    while((getline line<$0)>0){
        if(line ~ /pattern/){
            print $0":"line
        }
    }    
}'

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

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