I want to find all PDF files in a directory and its subdirectories, on OSX.

I know there are some PDFs in subdirectories, because e.g. this produces lots of results:

ls myfolder/pdfs/*.pdf

All my Googling suggests I want ls -R, but this produces no results:

ls -R *.pdf

What am I doing wrong?

I can find some results this way:

ls -R | grep pdf

But I can't see this full paths to the files, which isn't very helpful.


ls -R *.pdf would invoke ls recursively on anything matching *.pdf (if there's nothing matching *.pdf in the current directory, you'll get no result, and if there is, it will only recurse into it if it's a directory). ls -R | grep pdf would show you everything in the ls -R result that matches the regular expression pdf, which is not what you want.

This is what you need:

find myfolder -type f -name '*.pdf'

This will give you the pathnames of all regular files (-type f) in or below the myfolder directory whose filenames matches the pattern *.pdf. The pattern needs to be quoted to protect it from the shell.

With the zsh shell (which recently became the default shell on macOS):

print -l myfolder/**/*.pdf(.ND)

This would print out the pathnames of all regular files in or below the directory myfolder that have names ending in .pdf, in a single column. The matching would include hidden names, and it would print an empty line if there was no matches. The N and D in the glob qualifier corresponds to setting the nullglob and dotglob shell options in the bash shell (but only for this single globbing pattern), and the dot makes the pattern only match regular files (i.e. not directories etc.)

| improve this answer | |

On the native bash shell you have on macOS Terminal, enable an extended glob option globstar to enable recursive glob match on nested sub-directories.

shopt -s globstar nullglob
ls **/*.pdf

The nullglob prevents filename patterns which match no files to expand to a null string, rather than themselves. With globstar, the pattern ** used in a filename expansion context will match all files and zero or more directories and sub-directories.

To be a bit safe by avoiding setting the shell options on the current shell, you can set it in sub-shell instead

( shopt -s globstar nullglob; ls **/*.pdf )
| improve this answer | |


find . -name \*.pdf -print

this should list pdf like


note that * in *.pdf must be escaped if there is a pdf in starting directory.

| improve this answer | |

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.