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

发布于 2024-07-26 01:34:36 字数 320 浏览 3 评论 0原文

我对 bash 脚本很感兴趣,并且想知道如何遍历 unix 目录并记录当前正在查看的文件的路径(如果它与正则表达式条件匹配)。

它会像这样:

  • 遍历一个大的unix目录路径文件/文件夹结构。
  • 如果当前文件的内容包含与一个或多个正则表达式匹配的字符串,
  • 则将文件的完整路径附加到结果文本文件。

Bash 或 Perl 脚本都可以,但我更喜欢使用带有 grepawk 等命令的 bash 脚本来执行此操作。

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.

It would go like this:

  • Traverse a large unix directory path file/folder structure.
  • 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

我不在是我 2024-08-02 01:34:36
find . -type f -print0 | xargs -0 grep -l -E 'some_regexp' > /tmp/list.of.files

重要部分:

  • -type f 使查找列表仅包含文件
  • -print0 打印的文件不是用 \n 而是用 \0 分隔 - 它在这里是为了确保它可以工作,以防你的文件名称中包含空格
  • xargs -0 -将输入拆分为 \0,并将每个元素作为参数传递给您提供的命令(本例中为 grep)

使用 xargs 的一个很酷的事情是,如果您的目录确实包含很多文件,您可以通过并行来加速该过程it:

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

这将在 5 个单独的副本中运行 grep 命令,每个副本扫描另一组最多 100 个文件

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
  • -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
  • xargs -0 - splits input on \0, and passes each element as argument to the command you provided (grep in this example)

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:

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

﹏雨一样淡蓝的深情 2024-08-02 01:34:36

使用 find 和 grep

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

-l 在 grep 上仅获取文件名

-e 在 grep 上指定一个正则表达式

{} 将找到的每个文件放置在grep 命令末尾的 find 命令

>> outfile.txt 附加到文本文件

use find and grep

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

-l on the grep gets just the file name

-e on the grep specifies a regex

{} places each file found by the find command on the end of the grep command

>> outfile.txt appends to the text file

咋地 2024-08-02 01:34:36

grep -l -R <正则表达式>应该可以完成这项工作。

grep -l -R <regex> <location> should do the job.

嗳卜坏 2024-08-02 01:34:36

,您可以采用人们建议的 find 命令,并使用 find2perl 将它们转换为 Perl 脚本:

如果您有:

$ find ...

如果您想在 Perl 中执行此

$ find2perl ...

操作 输出一个执行相同操作的 Perl 程序。 从那时起,如果您需要在 Perl 中做一些简单但在 shell 中却很难的事情,您只需扩展 Perl 程序即可。

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:

If you have:

$ find ...

make that

$ find2perl ...

That outputs a Perl program that does the same thing. From there, if you need to do something that easy in Perl but hard in shell, you just extend the Perl program.

空气里的味道 2024-08-02 01:34:36
find /path -type f -name "*.txt" | awk '
{
    while((getline line<$0)>0){
        if(line ~ /pattern/){
            print $0":"line
            #do some other things here
        }
    }    
}'

类似线程

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

天荒地未老 2024-08-02 01:34:36
find /path -type f -name "outfile.txt" | awk '
{
    while((getline line<$0)>0){
        if(line ~ /pattern/){
            print $0":"line
        }
    }    
}'
find /path -type f -name "outfile.txt" | awk '
{
    while((getline line<$0)>0){
        if(line ~ /pattern/){
            print $0":"line
        }
    }    
}'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文