Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grep returns "Too many argument specified on command" [duplicate]

Tags:

file

shell

unix

I am trying to list all files we received in one month

The filename pattern will be

20110101000000.txt

YYYYMMDDHHIISS.txt

The entire directory is having millions of files. For one month there can be minimum 50000 files. Idea of sub directory is still pending. Is there any way to list huge number of files with file name almost similar.

grep -l 20110101*    

Am trying this and returning error. I try php it took a huge time , thats why i use shell script . I dont understand why shell also not giving a result Any suggestion please!!

like image 308
zod Avatar asked Apr 25 '26 19:04

zod


2 Answers

$ find ./ -name '20110101*' -print0 -type f | xargs -0 grep -l "search_pattern"

you can use find and xargs. xargs will run grep for each file found by find. You can use -P to run multiple grep's parallely and -n for multiple files per grep command invocation. The print0 argument in find separates each filename with a null character to avoid confusion caused by any spaces in the file name. If you are sure there will not be any spaces you can remove -print0 and -0 args.

like image 161
Damodharan R Avatar answered Apr 28 '26 12:04

Damodharan R


This should be the faster way:

find . -name "20110101*" -exec grep -l "search_pattern" {} +

Should you want to avoid the leading dot:

find . -name "20110101*" -exec grep -l "search_pattern" {} + | sed 's/^.\///'

or better thanks to adl:

find . -name "20110101*" -exec grep -l "search_pattern" {} + | cut -c3-

like image 37
jlliagre Avatar answered Apr 28 '26 12:04

jlliagre



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!