globls

Globbing with ls to find all files matching a certain pattern


I'm trying to locate all PDF files in some folder and any subfolder, just in the terminal, as opposed to in a script. I'm also quite new to linux, so apologise if I've missed anything obvious, or perhaps vital to diagnosing my particular problem.

I'm using bash 4.1.5(1)-release (i486-pc-linux-gnu), and have done some poking about on google about glob and extglob expressions, and it appears the syntax I should be using is

$ ls **.pdf

This however finds nothing, since there is no file matching the pattern *.pdf in the current folder ./; it appears to want to read ** as *:

ls: cannot access **.pdf: No such file or directory

There are PDFs elsewhere, in subfolders between 1 and 5 deep (in particular in every subfolder 1-deep), some of which I can see by checking with

$ ls */*.pdf

Hence, ls appears to be working properly. Its manual appears to not be very helpful, since all I could see that might be of any use us calling ls with the -R flag, which does not solve the problem in any of the above cases.

I tried using extglob patterns (making sure to turn them on with shopt). I can see my depth-1 files with ls */*?(.)pdf, but I can't see anything with ls .*(/*)pdf or ls .*(/*).pdf, even from within a subdirectory where there are PDFs.

I've read elsewhere (in reference to the .gitignore file in a git repository) that the ** pattern does not work for everyone.

Could this be affecting me, and how might I remedy it (ideally without superuser privileges)? Might this (or some related problem) be also affecting the extglob functionality?


Solution

  • You may want to consider find

    find . -name '*.pdf' -exec ls -l {} \;
    

    or

    find . -name '*.pdf' -ls
    

    where . is your current working directory. The glob functionality comes with 4.0+ bash. The glob extensions are not portable in other words.