MiG
MiG

Reputation: 43

How to use "find" and "grep" to get the file size too?

I have this script:

find test -type f \( -iname \*.html -o -iname \*.htm -o -iname \*.xhtml \) -exec grep -il ".swf" {} \; -printf '%k KB - \t %p\n' > result-swf-files.csv

This will search the directory "test" (and its subdirectories) for all HTML files which contains the word ".swf" in them. ANd will write a CSV file with the results.

But I want to get the file size too in the same line (now, the script outputs on one line the grep result - which doesn't have the file size - and in another line the printf result - which includes the file size). How do I add an option to grep to get the file size?

Upvotes: 0

Views: 11744

Answers (2)

Zhenya
Zhenya

Reputation: 79

find . -name *PATTERN*.gz -print0 | xargs -0 ls -lh

So you get ls for all files that you want.

Upvotes: 0

Thyag
Thyag

Reputation: 1315

A less verbose way is to use recursive grep (if your system supports it):

grep -rl --include="*.htm*" ".swf" test|xargs ls -l|awk '{ print $9 "," $5 }'

Explanation :

  • Grep recursively using the "rl" flag
  • include file pattern "*.htm"
  • search for the string ".swf" in each htm* file
  • search only under the "test" directory
  • pipe the result to xargs where each filename becomes argument to "ls -l" command
  • Then use awk to get to only filename and file size. Use comma "," in between 9th and 5th columns in awk print to get the csv output.

Feel free to replace "ls -l" with human readable variants such as "ls -lk" or "ls -lh"

Alternatively, in your script, you can just print only the 2nd line of each file (the one that contains the size). You can just pipe and use grep like this : grep "[0-9] [KB]"

Below is the complete command:

find . -type f \( -iname \*.html -o -iname \*.htm -o -iname \*.xhtml \) -exec grep -il ".swf" {} \; -printf '%k KB - \t %p\n'| grep "[0-9] [KB]"

Upvotes: 4

Related Questions