Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the error "find: paths must precede expression" trigger when multiple results are returned from "find"

Tags:

linux

bash

Why does the error find: paths must precede expression: input.txt trigger when multiple results are returned from "find" in subprocess but not when a single result is returned?

The dir contains three files.

ls
input2.txt  input.txt  input.log 

There is only one file matching the find query and the result can be assigned to $foo

$ foo=$(find . -name *.log )
echo $foo
./plot.log

When > 1 reults are returned find throw's an error.

$ foo=$(find . -name *.txt )
find: paths must precede expression: input.txt

I don't understand why this is happening.

like image 613
Dave Avatar asked Dec 05 '22 23:12

Dave


1 Answers

You need to quote special characters, because globs are expanded before running the command:

find . -name '*.txt'

To see how globbing works, try for example echo *.txt - it will only actually print *.txt if there are no files in the current directory ending with .txt.

like image 75
l0b0 Avatar answered Apr 09 '23 13:04

l0b0