Ubuntu: search in subdirectories for all html files containing the tag



Question:

I have a directory include many files of different extensions. What I want is to list all .html files only that contains the tag .


Solution:1

From the terminal, use the find command to find all the files ending in .html and use the grep command to filter the results to show only the names of files that contain the <abbr> string:

cd /path-to-dir ## change directories to the root directory that you are searching from  find . -name "*.html" -exec grep -l '<abbr>' {} +    

The find command searches for files in a directory hierarchy recursively by default.

Or combine the two commands into a single command:

find /path-to-dir -name "*.html" -exec grep -l '<abbr>' {} +  


Solution:2

A simple solution:

grep -r "<abbr" --include="*.html" /path-to-dir  
  • use -r for all subdirectories.
  • Use --include='*html' to match html files only.


Solution:3

For verification (namely, the file name and the lines with <abbr>), you may use a simple:

grep '<abbr>' /path-to-dir/*.html  

If you want file names only, use the following:

grep '<abbr>' /path-to-dir/*.html | awk -F: '{ print $1 }' | sort | uniq  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »