Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

zgrep and subfolders

  • 11-05-2007 1: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