0

Directory Structure:

one.pdf
./subdir/two.pdf
./anothersubdir/three.pdf

When I type:

find ./ -type f -name "*.pdf"    

it retrieves all the pdfs, including subdirectories.

Bash Function

function getext {find ./ -type f -name "$1"}

With this function in bashrc, typing:

getext *.pdf

It only retrieves "one.pdf" but not the rest.

Question: what happens here with the function? What's missing from it compared to standard input to only get the first file and stop?

Thank you for your help.

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
farhang
  • 115
  • Apart from everything else, your function contains a syntax error. There's a missing ; (or newline) before the closing }. – Kusalananda Feb 15 '17 at 07:27
  • I know the role of the semi-colon in general and had this function contained anything else I would have used it. But can you help me understand why this works as it is? A syntax error should prevent it from working but it does not in this case. – farhang Feb 15 '17 at 17:40

1 Answers1

5

For the same reason that you quote "*.pdf" in the arguments to find inside your function, you need to quote it when you call the function:

getext "*.pdf"

Otherwise, the shell will attempt to match *.pdf to filenames in the current directory resulting in it being expanded - in this case to one.pdf - before being passed to your function.

steeldriver
  • 81,074