Search string in many files on HP-UX

23,764

Solution 1

You could use a variant of your find command like this:

find . -type f -exec grep -l word {} \; 

Solution 2

If your version of HP-UX is recent enough, you can call find with the -exec … + action. This action does the same job as xargs (call a command on multiple matching files at once, without overflowing the command line length limit), but in a reliable way for any file name.

find . -type f -exec grep -l word {} +

If your version of HP-UX is too old, you might have only -exec … \; and not -exec … +. The ; version calls the command on one file at a time, which is a bit slower.

find . -type f -exec grep -l word {} \;

If your file names don't contain \"' or whitespace, then you can use xargs without the -0 option.

find . -type f -print | xargs grep -l word
Share:
23,764

Related videos on Youtube

Nasser Ghazali
Author by

Nasser Ghazali

Updated on September 18, 2022

Comments

  • Nasser Ghazali
    Nasser Ghazali over 1 year

    I need to find which files (they can have space in the filename) of a directory contains a string using only sh and system's commands (Perl is not an option).

    For a few files, this command works fine:

    # grep -l word *
    file 1
    file1
    

    But if I have 270k file, I obtain the following error:

    #  grep -l word *
    sh: /usr/bin/grep: The parameter list is too long.
    

    In HP-UX, the xargs command doesn't have the -0 option, so I can't use this:

    # find . -print0 |xargs -0 grep -l
    xargs: unknown option: -0
    

    Do you know which command I can use?

  • Gabriel Hautclocq
    Gabriel Hautclocq over 6 years
    You forgot to write the word to search, so this does not answer the problem, so this should not be the best answer unless Mat correct it. @Gilles answer is better and more complete imho.
  • Gabriel Hautclocq
    Gabriel Hautclocq over 6 years
    That's better now :-) Still less complete than Gilles answer but works for everyone.