Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Find file that contains "....." (command in Unix)

by Anonymous Monk
on Mar 28, 2003 at 07:44 UTC ( [id://246421]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have some folders and subfolders in them... each subfolders contains *.txt files in them...
|-test |-sub_test1 |-1.txt |-2.txt |-3.txt |-sub_test2 |-1.txt |-2.txt |-3.txt
what's the unix command/or perl codes to find files that contains a string, "....." in each folders and its subfolders?

Replies are listed 'Best First'.
Re: Find file that contains "....." (command in Unix)
by Abigail-II (Bishop) on Mar 28, 2003 at 08:15 UTC
    find . -type f -print0 | xargs -0 grep -l

    The advantage of using grep -l is that the -l option makes grep print the file name of the file that matches (instead of the matching line), and grep will stop looking in the file after the first match.

    Abigail

      Good solution, but you could run into trouble if the resulting command line is bigger than the maximum size supported by the OS.

      If there are a lot of files to be checked, then it could be the case to check them one by one. So I'd slightly modify your command line this way:

      find . -type f -exec grep -Hl {} \;

      Update: grep here is the GNU grep

      Ciao!
      --bronto


      The very nature of Perl to be like natural language--inconsistant and full of dwim and special cases--makes it impossible to know it all without simply memorizing the documentation (which is not complete or totally correct anyway).
      --John M. Dlugosz
        Good solution, but you could run into trouble if the resulting command line is bigger than the maximum size supported by the OS.

        Of course, you will only run into trouble if your xargs is broken. The point of using xargs is to avoid the problem you are describing.

        The disadvantage of using -exec is that find will spawn a grep process for each file found, while with the use of xargs, far less processes will be spawned.

        Abigail

Re: Find file that contains "....." (command in Unix)
by Corion (Patriarch) on Mar 28, 2003 at 07:54 UTC

    The module File::Find::Rule has a nice and easy interface to do this (as seen in the documentation) :

    my @files = File::Find::Rule->grep( qr/^whatever is in the file$/ ) ->in('test');

    perl -MHTTP::Daemon -MHTTP::Response -MLWP::Simple -e ' ; # The $d = new HTTP::Daemon and fork and getprint $d->url and exit;#spider ($c = $d->accept())->get_request(); $c->send_response( new #in the HTTP::Response(200,$_,$_,qq(Just another Perl hacker\n))); ' # web
Re: Find file that contains "....." (command in Unix)
by robartes (Priest) on Mar 28, 2003 at 07:50 UTC
    No Perl needed solution:
    find . -type f -exec grep "\.\.\.\.\." /dev/null {} \;
    Or if you have a modernish grep:
    grep -r "\.\.\.\.\." .

    Update: The OP requested the file name, the solutions print all lines containin the pattern. This does what the OP wants:

    find . -type f -exec grep -q "\.\.\.\.\." {} \; -exec echo 'Found {}' + \;

    CU
    Robartes-

      Well, here is yet another solution assuming gnu grep...

      grep -F -r -I -l '.....' . | grep '.txt$'

      ...first part of the pipeline recursively, -r, (downword only) searches text files, -I, for ..... in the current directory, ., listing only the file names found. Second part filters the file names which lists only those ending in '.txt'.

      Here is one more and bit more robust (to be run in "test" directory; see OP)...

      find . -type f -name '*.txt' -print0 | xargs -0 grep -F -l '.....'
      or just find . -type f -exec grep -lq "\.\.\.\.\." {} \;

      -Waswas
Re: Find file that contains "....." (command in Unix)
by nite_man (Deacon) on Mar 28, 2003 at 08:05 UTC
    Use grep:
    grep -rE "\.{5}" `ls /my_folders`
    --------> SV* sv_bless(SV* sv, HV* stash);
      That does the same as grep -rE "\.{5}" /my_folders/* so it's useless use of ls.

      Makeshifts last the longest.

Re: Find file that contains "....." (command in Unix)
by blakem (Monsignor) on Mar 28, 2003 at 10:02 UTC
    None of the solutions posted so far are exactly what I would do... How about a simple:
    grep -lr yourstring /path/to/your/dir

    -Blake

      The -r option on grep is not uniformely supported. It's not in the POSIX standard. The question was about a Unix command; and GNU stands for "GNU is Not Unix". ;-)

      Abigail

        And if you're going to depend on non-standard grep options why not just depend on using zsh and use grep '\.\.\.\.\.' ./**/*.txt to let the shell do the find for you.

        (Aside: zsh rules.)

      Thanks....
Re: Find file that contains "....." (command in Unix)
by Anonymous Monk on Jul 31, 2009 at 04:19 UTC
    find some more information about unix find command at http://scripterworld.blogspot.com/2009/07/unix-find-command-with-examples-and.html

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://246421]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-03-29 07:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found