Operating System - HP-UX
1833017 Members
2231 Online
110048 Solutions
New Discussion

find command - finding a file type in all subdirectories

 
SOLVED
Go to solution
Danny Fang
Frequent Advisor

find command - finding a file type in all subdirectories

Hi,
May I know how I could specify the "find" command to search for a specific file type in a dir and also all its subdirectories?

I tried:
find . -depth -type f -name ".html" -print

or by specifying the absolute path with the preceeding / before "-type" :
find /opt/IBM/WebSphere/Express51/A
ppServer/installedApps/DefaultNode/iscapuat.ear/iscap.war.cj/ -type f -name ".jsp" -print

and also:
find ./ -depth -type f -name ".html" -print

However, both of these commands did not display the files.

Could anyone help me out?
9 REPLIES 9
Stephen Keane
Honored Contributor

Re: find command - finding a file type in all subdirectories

Try

# find . -depth -type f -name "*.html" -print
Leif Halvarsson_2
Honored Contributor

Re: find command - finding a file type in all subdirectories


Hi
Maybe better using ' (not expanded by the shell)
find . -type f -name '*.html'


Ionut Grigorescu_2
Super Advisor

Re: find command - finding a file type in all subdirectories

find . -depth -type f -name "*\.html"
If it weren't for STRESS I'd have no energy at all
Bill Hassell
Honored Contributor

Re: find command - finding a file type in all subdirectories

-name is the key but it is using regular expressions (man regexp) to match against filenames. ".html" will only match one file: .html whereas telling -name that zero or omore characters may precede .html as in: "*.html" will find the file names you're looking for. The "" characters are required to escape the special meaning of * to the shell. The shell always looks for special characters like * ? [] and so on in order to match file and directory names. The "" send the string untouched to find so it can do it's own pattern matching.

In a pinch, you can always do this:

cd
find . -type f | grep html

or use grep's multi-pattern capability:

find . -type f | grep -e .html -e .jsp

Note that -depth and -print are defaulted to true so -print and -depth are redundant. If you need to stay at the current directory level (and not go down into other directories), use -prune. Note that when -depth is explicity set on the command line, -prune is disabled. -print is always true unless the output of find is directed to something like -exec, in which case you can add -print to feed the names to exec *and* see the names on stdout at the same time.


Bill Hassell, sysadmin
Danny Fang
Frequent Advisor

Re: find command - finding a file type in all subdirectories

Hi everyone,
Thanks for taking the effort in posting solutions to my question. I've tried all of the methods suggested in the earlier 3 replies to my question - however, none of these commands would search for the specified files (.html and .jsp) in the subdirectory beneath the current directory. In fact, I had tried the method #find . -depth -type f -name "*.html" as suggested by Stephen Keane before receiving replies to my question - it still could not retrieve the files from the subdirectories in the current sub-dir.

Greatly appreciate it if anyone could provide other ideas/suggestions to this issue.

john korterman
Honored Contributor
Solution

Re: find command - finding a file type in all subdirectories

Hi,

# find -name "*.html" -o -name "*.jsp"

regards,
John K.
it would be nice if you always got a second chance
Bill Hassell
Honored Contributor

Re: find command - finding a file type in all subdirectories

Are you running the command as root or just a normal user login? find cannot read directories where you do not have acess rights. Can you see the html files with an ls command? Are any of the directories symbolic links?


Bill Hassell, sysadmin
Danny Fang
Frequent Advisor

Re: find command - finding a file type in all subdirectories

Hi everyone,
The command below worked.
# find -name "*.html" -o -name "*.jsp"

Thanks to everybody for helping out in this question.
Danny Fang
Frequent Advisor

Re: find command - finding a file type in all subdirectories

Problem resolved.