Find all the *.config files, that have a particular folder (e.g config) in path and that have a specific text (e.g hostname). Then display the matching lines along with the file permissions.

Right now the below command gives me all the config file that have 'config' folder in path and have 'hostname' text along with the file permission.

ls -la `find / -type f -name "*.config" -exec grep -li 'hostname' {} + | grep config`

sample output

-rw-r--r-- 1 user users  2240 Jan 17  2014 /home/config/1.config
-rw-r--r-- 1 user users  2146 Aug  2  2013 /home/test/config/abc.config

These file have text 'hostname' in them. Now I also want to see the line where is 'hostname' match has happened. Is there a way to do it, with out bash scripting ?

  • 1
    do you need "file permissions" to be displayed in the output? – RomanPerekhrest Jun 6 '17 at 11:49

Use find command options:

find -type f -path "*/config/*.config" -exec grep 'hostname' {} \;  -ls

  • -path <pattern> - File name matches shell pattern <pattern>

  • -ls - list current file in ls -dils format on standard output

  • why does the \ before ; means ? – 3lokh Jun 6 '17 at 11:05
  • I don't think the OP wants the -ls. You may want to use -exec grep -hi 'hostname' {} + for (slightly) improved efficiency. – roaima Jun 6 '17 at 11:45
  • @NikhilGeorge, \; - Both of these constructions might need to be escaped (with a \') or quoted to protect them from expan‐ sion by the shell. See more extended description in man find – RomanPerekhrest Jun 6 '17 at 11:46
  • @roaima, that was the way I have interpreted the question, I have left a comment – RomanPerekhrest Jun 6 '17 at 11:50

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.