[Q] find command & spaces in path - Unix

This is a discussion on [Q] find command & spaces in path - Unix ; I am currently using the following string of commands to find every file with a specific extension in a folder hierarchy (the find command), output the result of these text files (cat) and then just show me the line with ...

+ Reply to Thread
Results 1 to 7 of 7

Thread: [Q] find command & spaces in path

  1. [Q] find command & spaces in path

    I am currently using the following string of commands to find every
    file with a specific extension in a folder hierarchy (the find
    command), output the result of these text files (cat) and then just
    show me the line with some specific text (grep)

    find . -iname "*.out" | xargs cat | grep -i "hello"

    Now, this all works, except when the file path contains a space.
    For example, it cannot search: ./a dir/afile.out

    Any suggestions on what I can do to fix this?

    Thank you.


  2. Re: [Q] find command & spaces in path

    On 28 Sep 2006 08:19:38 -0700, ericgorr@gmail.com wrote:

    > Now, this all works, except when the file path contains a space.
    > For example, it cannot search: ./a dir/afile.out
    >
    > Any suggestions on what I can do to fix this?



    man xargs

  3. Re: [Q] find command & spaces in path

    In article <1159456778.815183.266920@k70g2000cwa.googlegroups. com>,
    ericgorr@gmail.com wrote:

    > I am currently using the following string of commands to find every
    > file with a specific extension in a folder hierarchy (the find
    > command), output the result of these text files (cat) and then just
    > show me the line with some specific text (grep)
    >
    > find . -iname "*.out" | xargs cat | grep -i "hello"
    >
    > Now, this all works, except when the file path contains a space.
    > For example, it cannot search: ./a dir/afile.out
    >
    > Any suggestions on what I can do to fix this?
    >
    > Thank you.


    If you're using GNU find and xargs, use the -print0 option to find and
    the -0 option to xargs. These make them use a null byte as the filename
    delimiter rather than whitespace.

    If not, you need a while loop:

    find . -name "*.out" | while IFS= read filename
    do
    grep -i "hello" "$filename"
    done

    The "IFS=" is necessary in case any filenames begin or end with spaces.
    This still won't handle filenames containing newlines, though.

    --
    Barry Margolin, barmar@alum.mit.edu
    Arlington, MA
    *** PLEASE post questions in newsgroups, not directly to me ***
    *** PLEASE don't copy me on replies, I'll read them in the group ***

  4. Re: find command & spaces in path


    Bit Twister wrote:
    > On 28 Sep 2006 08:19:38 -0700, ericgorr@gmail.com wrote:
    >
    > > Now, this all works, except when the file path contains a space.
    > > For example, it cannot search: ./a dir/afile.out
    > >
    > > Any suggestions on what I can do to fix this?

    >
    >
    > man xargs


    I have. Sorry, I am still not certain what I should do.
    Any help you can provide would be appreciated.


  5. Re: find command & spaces in path

    On 28 Sep 2006 14:44:32 -0700, ericgorr@gmail.com wrote:
    >
    >>
    >> man xargs

    >
    > I have. Sorry, I am still not certain what I should do.


    Well looking at man xargs under Examples: we see

    find /tmp -name core -type f -print0 | xargs -0 /bin/rm -f

    Find files named core in or below the directory /tmp and delete them,
    processing filenames in such a way that file or directory
    names containing spaces or newlines are correctly handled.

    So, what arguments are used in the above find and xargs example
    that you are not using?


  6. Re: find command & spaces in path


    Barry Margolin wrote:
    > In article <1159456778.815183.266920@k70g2000cwa.googlegroups. com>,
    > ericgorr@gmail.com wrote:
    >
    > > I am currently using the following string of commands to find every
    > > file with a specific extension in a folder hierarchy (the find
    > > command), output the result of these text files (cat) and then just
    > > show me the line with some specific text (grep)
    > >
    > > find . -iname "*.out" | xargs cat | grep -i "hello"
    > >
    > > Now, this all works, except when the file path contains a space.
    > > For example, it cannot search: ./a dir/afile.out
    > >
    > > Any suggestions on what I can do to fix this?
    > >
    > > Thank you.

    >
    > If you're using GNU find and xargs, use the -print0 option to find and
    > the -0 option to xargs. These make them use a null byte as the filename
    > delimiter rather than whitespace.


    Thank you.

    I thought I had to somehow get tick marks around the paths to the files.


  7. Re: [Q] find command & spaces in path

    ericgorr@gmail.com wrote, on Thu, 28 Sep 2006 08:19:38 -0700:

    > I am currently using the following string of commands to find every
    > file with a specific extension in a folder hierarchy (the find
    > command), output the result of these text files (cat) and then just
    > show me the line with some specific text (grep)
    >
    > find . -iname "*.out" | xargs cat | grep -i "hello"
    >
    > Now, this all works, except when the file path contains a space.
    > For example, it cannot search: ./a dir/afile.out
    >
    > Any suggestions on what I can do to fix this?


    Others have mentioned the -print0 method. It's non-standard, but
    that's okay since you are using -iname.

    Standard (POSIX) solutions are:

    find . -name "*.[oO][uU][tT]" -exec cat {} + | grep -i "hello"

    (note the use of "+" instead of ";" with -exec), or

    find . -name "*.[oO][uU][tT]" | sed "s/.*/'&'/" | xargs cat | grep ...

    which adds single quotes to every line, and thus won't work for
    file names containing single quote characters or newline characters.

    --
    Geoff Clare


+ Reply to Thread