Using grep to locate which file contains a string: Argument list too long

Just on a whim, I needed to pull data from an archive folder that creates filenames based on the current timestamp.  The string to be found was “L46890″.  Turns out that there are 11,939 files in that directory, and that is over the limit of arguments that grep or cat can process.

List number of files
# ls -la | wc -l
11939

Trying to use ‘grep -l’ to return a filename list that contains “L46890″.  The * tells grep to check every file in the current directory.  As you can see, it fails.
# grep -l L46890 *
-bash: /bin/grep: Argument list too long

This runs along the same lines as my previous post of using rm (rm `find /test/ -name “asdf*” | grep subfolder`).  Only difference here is that I am not trying to remove the files.  Inside the “`” ticks, it is doing a directory listing with ls, then passing those results over to grep.  Next, grep parses the file listing from ls and picks out all the files that were created in October 2008 per the timestamped filename.

The completed command
# grep -l L1046890 `ls | grep 200810`
20081020105011

The below shows that a total of 1,953 files were processed
# ls | grep 200810 | wc -l
1953

Advertisement

~ by Kevin Goodman on November 5, 2008.

Leave a Reply

Fill in your details below or click an icon to log in:

Gravatar
WordPress.com Logo

You are commenting using your WordPress.com account. Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 1,031 other followers