Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Am using the following "find" command to extract some files,

find /lag/cnnf/ -maxdepth 3 -newer ./start ! -newer ./end | grep -nri abc | egrep '([^0-9]45[^0-9])' | grep -nri "db.tar.gz" >> sample.txt

My output in sample.txt is


what should i do to get only,


without the random numbers in front of it and what those numbers actually mean.

share|improve this question
up vote 3 down vote accepted

The numbers come from the -n options you are passing to grep. However, the pipe as you have it is a bit too long for my taste.

From your example it seems you have a reasonably simple directory structure. If you have the GNU find, use -regex (I'm not sure this is mandated by POSIX):

find /lag/cnnf/ \
    -maxdepth 3 \
    -regex "abc.*[^0-9]45[^0-9].*db.tar.gz" \
    -newer ./start ! -newer ./end >> sample.txt

Otherwise, assuming a little bit stricter requirements on the directory structure (would still fit your example):

find /lag/cnnf/ \
    -maxdepth 3 \
    -path "*abc*/45/*db.tar.gz" \
    -newer ./start ! -newer ./end >> sample.txt

You might also want to consider using shell expansion - for example in bash you would need to set the shell option globstar and then play with matching using the ** wildcard.

share|improve this answer
Neither -regex nor -maxdepth are POSIX. They're both GNU, but are now also supported by some BSDs. -maxdepth can be replaced by a combination of -path (recent POSIX addition) and -prune. – Stéphane Chazelas Dec 21 '13 at 21:09

The numbers are actually produced by (e)greps and its -n option which means:

Prefix each line of output with the 1-based line number within its input file.

If you insist on keeping that option, remove first two fields separated by a colon with the cut command:

find /lag/cnnf/ -maxdepth 3 -newer ./start ! -newer ./end \
    | grep -nri abc \
    | egrep '([^0-9]45[^0-9])' \
    | grep -nri "db.tar.gz" \
    | cut -d':' -f3- >> sample.txt

-d is character used as field separator, -f says which fields to print, in this case the 3rd one and all following.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.