find command in Linux to locate pdf files
find
is far from useless. You are just not using it properly.
try:
find . -type f -iname '*.pdf'
Take a look at the documentation for the findutils. The find
command is an incredibly powerful one and, consequently, has a somewhat complicated interface. You can do what you want with:
find . -type f -iname '*.pdf'
The command above means "find within . for entries of type file with a case insensitive name matching *.pdf (and print the name of such matches)". The find command can actually be used to execute commands on the files that are found (instead of or in addition to printing the file names). For your purposes, though, you may find yourself more comfortable with the locate command, which -- assuming you have built up the locate database using updatedb -- makes it very easy to find files. For example:
locate '*.pdf'
You will also find that the locate command is typically faster than the find command as locate uses an index of filenames (the locate database), whereas find will walk the hierarchy for each invocation.
You're simply missing the predicate that says what you're searching by (e.g. -name
.)
To find in home directory by name:
find ~ -name \*.pdf
Note that the wildcard *
has to be escaped so that the shell doesn't interpret it before find gets its hands on it. Using '*.pdf'
and "*.pdf"
will have the same effect as \*.pdf
.
To find case-insensitively:
find ~ -iname \*.pdf
To prune the results to files only (the name expression will probably take care of this for you, but just in case you have any weirdly-named directories):
find ~ -type f -iname \*.pdf
To make sure find follows symbolic links (I usually want to do this myself when doing searches):
find ~ -follow -type f -iname \*.pdf
To do something with the files you found: you can dump this to a file using stdout redirection (e.g. tack on > filename
at the end), or use the -exec
option to run a command (see the man page for details). The latter runs a command on each file at a time, though. it's often faster to let the xargs
command pass your found files as arguments to another command, all at once or big chunks at a time. For example, for ad-hoc (but unindexed) greps through header files:
find ~ -follow -type f -name \*.h | xargs grep -nH "identifier"
And one final extension, to make that last command work properly if you have files & directories with spaces in them:
find ~ -follow -type f -name \*.h -print0 | xargs -0 grep -nH "identifier"