Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

while running find command to find all the jpg files in the current directory as

find . -maxdepth 1 -type f -name *.jpg

i am getting the error as :

 find: paths must precede expression: pic1 (1).jpg
 Usage: find [-H] [-L] [-P] [-Olevel] [-Dhelp|tree|search|stat|rates|opt|exec] [path...] [expression]

i am not able to figure out what is wrong with that .

share|improve this question
2  
If you have a .jpg file in the current directory then your shell will expand your wildcard to be a list of the jpg files. Put your pattern in quotes to prevent this: "*.jpg" –  Nerdling Mar 14 '11 at 15:58
    
@Nerdling: Why don't you make your comment an answer (which it is)? :) –  arrange Mar 14 '11 at 16:22
add comment

1 Answer

up vote 6 down vote accepted

Always quote globs in find: find . -maxdepth 1 -type f -name "*.jpg"

To see why, try tracing the original command and looking at what is actually executed:

touch foo.jpg bar.jpg
strace find . -maxdepth 1 -type f -name *.jpg 2>&1 | grep jpg
execve("/usr/bin/find", ["find", ".", "-maxdepth", "1", "-type", "f", "-name", "bar.jpg", "foo.jpg"], [/* 62 vars */]) = 0
...
share|improve this answer
1  
strace is a great tool, but definitely an overkill for somebody who doesn't understand shell expansion. It's much simpler to step back in the shell history and just prepend the command with "echo". –  Adam Byrtek Mar 14 '11 at 23:42
    
Always is, as always :) , too often. find proj* -type f -name foobar` will search in every directory which matches ./proj* for example; perfectly valid and reasonable. –  user unknown Mar 26 '11 at 16:57
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.