Ubuntu – “paths must precede expression” error when trying to find all jpg files in the current directory

find

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 .

Best Answer

  • tl;dr Always quote globs in find: find . -maxdepth 1 -type f -name "*.jpg" (notice the " characters surrounding *).

    In your case, the shell is interpreting *.jpg (note the * character) and trying to match file names within the current directory that end in .jpg. There is a file named pic1 (1).jpg so that file name replaces *.jpg. The command the system is given by the shell becomes

    find . -maxdepth 1 -type f -name 'pic1 (1).jpg'
    

    To see in depth, try tracing the original command using strace. What is actually executed is:

    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
    ...