Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

zgrep and subfolders

  • 11-05-2007 02:55PM
    #1
    Registered Users, Registered Users 2 Posts: 2,078 ✭✭✭


    I have a directory containing some 40 subfolders, each containing about a hundred files, mostly compressed. I need to search the whole lot for a word pattern.

    I can use grep -r to search the subfolders, but it won't look in the .z files.
    I can use zgrep to search the .z files, but it won't look in subfolders, (it says -r option is not supported)

    Any suggestions?

    PS I know next to nothing about unix beyond basic file manipulation, so be nice!


Comments

  • Registered Users, Registered Users 2 Posts: 568 ✭✭✭phil


    find /path -type f -name \*.z -print0 | xargs -0 grep -H <term>
    

    -print0 argument to find separates files by the null character which is used in conjunction with xargs -0 to prevent xargs going barmy if it's passed a file with spaces in it

    zgrep -H makes sure the filename is specified in the output.

    Phil.


  • Registered Users, Registered Users 2 Posts: 2,078 ✭✭✭theCzar


    Thanks Phil,

    That's pretty nifty. It does the job nicely. Now I just need to break it and fix a few times until I understand it perfectly!

    :D


  • Registered Users, Registered Users 2 Posts: 2,078 ✭✭✭theCzar


    I was wondering:

    All the files I want to search are text files with extension '.list' and most are compressed to '.list.Z'

    I tried to have the find command find all files with 'list' in the extension, compressed or otherwise using
    find /path -type f -name \*.list* -print0 | xargs -0 zgrep -H <term>

    but this returned 'no match'. I can get it to return ALL files by \*, but this isn't perfect, since I've been outputting the zgrep results to a file in the same directory, so find finds the previous results and outputs them again in duplicate. :rolleyes:

    Of course, I could move the results file, but surely there's a stupid mistake I'm making that is preventing the above line from working properly?


  • Registered Users, Registered Users 2 Posts: 568 ✭✭✭phil


    The trailing * is stopping it from working.

    Everything you type at the command line is parsed by the shell first. The shell has certain characters that are basically special characters that it uses to do different things. & for example signifies that the process preceding it should go into the background and * denotes a glob that the shell should expand.

    e.g.

    if you run 'ls -l l*' that will print out a list of files all beginning with 'l' because the shell takes the * as a wildcard function.

    In the case above, you don't want the shell to expand the * because it only looks in the current directory. You want find to parse the * so you need to escape it or surround it in quotes (i.e. like the previous * in the statement).

    e.g.
    find /path -type f -name \*.list\* -print0 | xargs -0 zgrep -H <term>
    

    or
    find /path -type f -name "*.list*" -print0 | xargs -0 zgrep -H <term>
    

    Phil.


Advertisement