[Solved] find: paths must precede expression: 2.txt

 

Today, when we execute find./- Mtime + 30 – type F – name. PHP on the server, we report the following error:

find: paths must precede expression: 2.txt
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]

Then I checked it on the Internet, and found an article, which is like this: you need to add quotation marks when searching for multiple files

find ./ -mtime +30 -type f -name '.php'
or
find ./ -mtime +30 -type f -name ".php"

In this way, no more errors will be reported after the implementation, and a small problem will be solved

##Example:

Enter the TMP directory and create four new text files

cd /tmp
touch {1,2,3,4}.txt
find . -name *.txt
find: paths must precede expression: 2.txt
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]

```
This prompt appears because the asterisk is expanded to all files in the current directory, and such a match will of course result in an error. Just look at this.
```
echo *
1.txt 2.txt 3.txt 4.txt
echo '*'
*
echo \*
*
```

If you want the asterisk not to be expanded, you need to add brackets or backslash escape, know this we will know how to find
```
find . -name '*.txt'
./4.txt
./2.txt
./3.txt
./1.txt
```
Or use a backslash
```
find . -name \*.txt
./4.txt
./2.txt
./3.txt
./1.txt
```

Similar Posts: