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?
Glob or "Shell Globing" is the process of writing glob patterns that match files in a filesystem. Glob patterns specify sets of filenames with wildcard characters. For example, the Unix Bash shell command rm -rf textfiles/*. txt removes (rm) all the files with names ending in . txt from the folder textfiles.
Globbing is the operation that expands a wildcard pattern into the list of pathnames matching the pattern. Matching is defined by: A '?' (not between brackets) matches any single character. A '*' (not between brackets) matches any string, including the empty string.
The grep command searches through the file, looking for matches to the pattern specified. To use it type grep , then the pattern we're searching for and finally the name of the file (or files) we're searching in.
The Bash shell feature that is used for matching or expanding specific types of patterns is called globbing. Globbing is mainly used to match filenames or searching for content in a file. Globbing uses wildcard characters to create the pattern.
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.
The options extglob
and globstar
are required to get the extended functionality from the glob
library; they are turned on using the shell options (shopt
) utility as described below.
To use the globstar
(which causes the **
pattern to behave as described in the bash manual) one must activate it (in bash 4.0+) with
shopt -s globstar
and to enable the more "functional" regex-type expressions like ?(ab)
and *(ab)
their full effect, use
shopt -s extglob
To turn the options off again, specify the -u
flag instead of s
, for eaxample
shopt -u globstar
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With