Solve the error report of the find command: paths must precede expression

I want to clear some pdf files in the server /tmp directory, about 10,000 files, when I execute the command

find /tmp -maxdepth 1 -mtime 30 -name *.pdf

An error occurred:

find : paths must precede expression
Usage: find [-H] [-L] [-P] [path...] [expression]

Then I checked it on the Internet, and I found an article. It probably said: When searching for multiple files, you need to add single quotation marks. Double quotation marks have always been used. I didn’t expect to use single quotation marks when looking for multiple files. Well, I learned another trick. After modification:
find ./ -mtime +30 -type f -name’*.php’
so that no more errors will be reported after execution, and a small problem will be solved.
Example explanation:
# Enter the tmp directory Create 4 new text files
# cd /tmp
# touch {1,2,3,4}.txt
# find .-name *.txt
find: paths must precede expression: 2.txt

This prompt appears because the asterisk is expanded to For all files in the current directory, such matching will of course be wrong. Look at this to know:
# echo *
1.txt 2.txt 3.txt 4.txt
# echo’*’
*
# echo \*
*

If you want the asterisk not to be expanded, you need to add parentheses or backslashes to escape , Knowing these we will know how to find
# find. -Name’*.txt’
find.
-Name ‘*.txt’ ./4.txt
./2.txt
./3.txt
./1.txt #Or
use backslash to
 find .-name \*.txt
./4.txt
./2.txt
./3.txt
./1.txt

Read More:

Leave a Reply

Your email address will not be published. Required fields are marked *