1

Like in the title, and also I want to print the number of the line in each (text) file where the substring occurs.

So far I have:

find /sys | grep "filesystem"

But it appears to only look at the names and directories of files, and not their content.

And how to search for several strings simultaneously? Because I think I should search additionally for "*filesystem", "filesystem*" and "*filesystem*" to include all possible occurances of the string.

Thank you in advance.

  • You don't have to do find and grep. You can pass the parameter to find. find /sys -name "*filesystem*" . Also find searches the filenames. Do you also want to search for content? You should use grep for that. Maybe if you posted your code and the details of your expected output, it would help – rahul Apr 12 '15 at 17:01
  • @rahul Expected output: names of regular files in a given directory (and its subdirectories) which (either in name, directory, or content) have a (sub)string "filesystem", and if they are regular text files, then print the number of the line in them in which said (sub)string occurs. The code I posted was what I had, but you said it was wrong so I guess I'll have to re-do it from a scratch. – dziadek1990 Apr 12 '15 at 17:11
  • I'm still a bit confused as to the requirement. Since you mentioned name, directory, or content . What if only the filename matches, what do you want to print then? Does the requirement have to be covered in the same command? Would you mind using find for the file/directory and grep for the content? – rahul Apr 12 '15 at 17:26
  • @rahul I'm okay with using find and grep seperately. The requirement was ambiguous enough and didn't specifically say I have to do it in one command. – dziadek1990 Apr 12 '15 at 17:39
2

If you don't mind using two different commands for the file names and content, the below commands will help you.

find /sys -name "*filesystem*"

The above command will find all the files/directories with "filesystem" as part of the filename/directoryname.

grep -rn "filesystem" /sys/*

The above command will look for all the files containing "filesystem" in the content recursively and print out the line number along with the matching line. If you'd like to print only the line number without the content, you can use the below filter. You can substitute 2 with the column number you want to print.

grep -rn "filesystem" /sys/* | awk -F":" {'print $2'}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.