List Files With Found Contents in Linux

Published by Torry Crass on

There are so many ways to slice, dice, parse, and of course search through files in Linux. This is but one of those ways. What makes this method fancy is that it we go from finding and printing contents of all the files found to listing the columns related to the files found to obtain the appropriate information.

Note: replace searchterm with the text you’re looking for.

Find and print the data in files as well as the file details:

find . -type f -exec grep -in searchterm {} \; -ls

Find data in files and suppress the output from grep (lists file details for files that contain the search term) (simply add q to -in)

find . -type f -exec grep -qin searchterm {} \; -ls

Find data in files, list applicable files but only the fields desired. Each numerical variable represents a single column of text (ignoring spaces/tabs/etc.)

In the example below we display the year, the month, the day, separated by spaces and then a colon separating the date fields from the file name.

find . -type f -exec grep -qin searchterm {} \; -ls |awk '{ print $10 " " $9 " " $8 " : " $11 }'

Leave a Comment

%d bloggers like this: