1

I would like to obtain the list of all the directories which contains HTML documents, finished .htm or .html ignoring upper or lower case.

I have tried:

find / -type d -ls | tr -s [:blank:] | cut -d ' ' -f 11 | grep -i -e "*.htm" -e "*.html"

But it only lists directories, and I need to list those directories' content and I don't know how.

I have then tried:

find / -type d -exec ls -l {} \; | tr -s [:blank:] | cut -d ' ' -f 9 | grep -i -e ".htm" -e ".html"

And it does find them, but how do I print the directories where they are?.

0

I'd suggest using

find / '(' -iname '*.htm' -o -iname '*.html' ')' -printf '%h\n' | uniq | xargs -r -d '\n' ls -l

The first part, find / '(' -iname '*.htm' -o -iname '*.html' ')' -printf '%h\n', finds all files that end with .htm or .html in upper or lower case (using glob patterns), and prints the directory (%h) for each such file found, one directory per line.

Because of how find scans directories, there are one or more consecutive identical directories listed; uniq retains only one of each.

Finally, we feed the list of directories to xargs, telling it to not run a command without any directories -r, and that the separator is a newline -d '\n'. The command is ls -l; modify to your liking.

If you only need the list of directories, not those directories contents, drop the xargs part:

find / '(' -iname '*.htm' -o -iname '*.html' ')' -printf '%h\n' | uniq
0

Here are some possible commands including example output

The most simple:

$ find / -iname "*.htm*"
foo/a.HTM
foo/b.HTML
foo/b.html
foo/x.htmx
foo/a.htm
bar/a.htm

-iname means to find files matching glob and be case insensitive. The problem is the glob *.htm* also finds htmx.

To prevent finding htmx you have to split the glob:

$ find / -iname "*.htm" -o -iname "*.html"
foo/a.HTM
foo/b.HTML
foo/b.html
foo/a.htm
bar/a.htm

Or use grep which can use regex:

$ find / | grep -i "\.html*$"
foo/a.HTM
foo/b.HTML
foo/b.html
foo/a.htm
bar/a.htm

Note that regex is different than glob. Especially the dot (.) and star (*) have very different meanings in glob and regex.

See https://en.wikipedia.org/wiki/Glob_(programming)#Compared_to_regular_expressions for more information.

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.