john
john

Reputation: 11709

find the folder along with the file names which has my string in it?

I need to find the folder names along with the file names which has my string in their contents. I am in this directory "/data/queue/data" and I have lot of folders in the same directory and inside those each folders, I have various files.

I want to run a command which can find me folder name and the file names which has a particular string like this "badezimmer" in their contents. What is the fastest way to do this?

Below are the folders in this directory "/data/queue/data" and each of those folders has various files in it.

david@machineA:/data/queue/data$ ls -lrth
drwxr-xr-x 2 david david  12K Apr 11 18:58 1428800400
drwxr-xr-x 2 david david  12K Apr 11 19:58 1428804000
drwxr-xr-x 2 david david  12K Apr 11 20:58 1428807600

I want to run a command from this directory only - "/data/queue/data" which can print me the folder names and the files names for that folder if they have my string in their contents.

david@machineA:/data/queue/data$ some command to find the folder and files which has my string.

Upvotes: 2

Views: 99

Answers (1)

Phylogenesis
Phylogenesis

Reputation: 7880

As discussed, the following will do what you want:

grep -rl 'badezimmer' /data/queue/data

From the man page for grep(1):

-l, --files-with-matches
       Suppress  normal  output;  instead  print  the  name of each input file from which output would normally have been
       printed.  The scanning will stop on the first match.

-r, --recursive
       Read all files under each directory, recursively, following symbolic links only if they are on the  command  line.
       This is equivalent to the -d recurse option.

Upvotes: 2

Related Questions