Linux find paths must precede expression

find: paths must precede expression: \+14

There should be no need to export these variables, unless they are also used in code which you are not showing which depends on their values being made available to child processes of the current shell.

I would expect you to get an error more like find: invalid argument \+14′ to -mtime’ or find: -mtime: \+14: illegal time value

2 Answers 2

You are asking find to locate files whose name matches (in a case insensitive match) the string -mtime , and then passing the string \+14 as an argument that find is interpreting as a path. It is telling you that the path must be given first. The problem is that _searchSt is unset. (You set a variable named _searchStr earlier, so presumably this is a typo.)

Note that this problem would be avoided if you had used double quotes and written find /data/ing/ur/test -type f -iname «$_searchSt» $_input_date . . And this is a point of debate regarding best practice: you cannot double quote $_input_date since you are relying on field splitting here. Best practice is probably to avoid relying on field splitting and explicitly writing -mtime with a variable containing just the argument to be used. That is; find «$path» -type f -iname «$searchStr» -mtime «$_input_date» .

You can also guard against this type of error (misuse of variable names) by using the $ <. >construct and writing find «$» -type f -iname «$» . .

You’re messing with the shell’s wildcard expansion, which will expand the expresion to all matching filenames in the given folder, thus passing several filenames to search for to find at once. Aside from that, I think you’re misusing the export directive (aren’t you?). You should modify your code as follows:

Removed_files=rmvfolder _searchStr='abc_*' # Single quotes are needed here to prevent bash from expanding the wildcard! _input_date='-mtime +14' mkdir -m777 /dm/Removed/$ find /data/ing/ur/test -type f -iname $ $ -exec mv <> /dm/Removed/$ \; 

Alternatively, you can escape the asterisk; _searchStr=abc_\*

Источник

How to Fix the Error “paths must precede expression: find”

In Linux, the terminal in Linux is perhaps the most powerful tool to perform any task. One of the more simple tasks that it can perform involves the “find” keyword. This is another keyword used in the terminal which allows the user to find any file on the system and even perform various actions on it. While dealing with this keyword, an error can be encountered with the statement “paths must precede expression”.

Читайте также:  Подсистема windows for linux

This post describes the reason for that error and the solution for fixing the error. In this post, some knowledge will be provided on the reasoning behind the error and the solution for resolving the error.

How to Resolve the Error “paths must precede expression”?

To understand how to fix any error, we need to understand what caused it first. Several reasons will invoke this problem in your system.

Reason 1: Name is Defined Before the Path

The first and most common reason that invokes this error is when the syntax of the “find” command is not followed correctly. Particularly, the name of the “file” is declared before the file’s “path”. The error is demonstrated in the snippet below:

Solution: Specify the Path First

The most obvious fix to this issue is to replace the position of the “path” and the “name” in the find expression. This way, the correct command pattern will be followed, and the error will no longer be invoked. The example for this solution is demonstrated through the command below:

Reason 2: General Syntax Problem

various syntax problems will invoke the same error. This is demonstrated in the example below where the space between the “(” and the “-name” invokes the error and is mentioned in error itself:

$ find . -name 'test' -type f -not \(-name 'test.sh' -or -name 'sample.sh' \)

Solution: Use the Correct Syntax

The correct syntax of the “find” command is provided below:

$ find "directory_path" -name "file_name"

In this command, the “directory_path” represents the path where you want to search the file named “file_name”.

However, the “find” expression may be quite complex as in the above case. The above error statement mentions the “(-name” in its error, which implies that the error relates to the syntax. The correct command would be as shown below:

$ find . -name 'test' -type f -not \( -name 'test.sh' -or -name 'sample.sh' \)

In this example, the syntax is fixed by simply providing space between the “( -name” which was previously invoking the error.

That’s it from this error-resolving guide!

Conclusion

The “paths must precede expression: find” problem is invoked when the syntax of the “find” command is not followed properly. To resolve this issue, declare the path of the file before the name of the file in the “find” command. The other way to fix this error is to read the error statement, which will explain which part of the expression has an incorrect syntax and then use the correct syntax for that keyword. This article has explained the major reasoning behind this error and also demonstrated how these errors can be resolved in your system.

Читайте также:  Good web browser linux

TUTORIALS ON LINUX, PROGRAMMING & TECHNOLOGY

Источник

«find: paths must precede expression:» How do I specify a recursive search that also finds files in the current directory?

I am having a hard time getting find to look for matches in the current directory as well as its subdirectories. When I run find *test.c it only gives me the matches in the current directory. (does not look in subdirectories) If I try find . -name *test.c I would expect the same results, but instead it gives me only matches that are in a subdirectory. When there are files that should match in the working directory, it gives me: find: paths must precede expression: mytest.c What does this error mean, and how can I get the matches from both the current directory and its subdirectories?

for the record, find of msysgit may throw this error unless you surround the pattern with quotes: find . -name «*test.c» . (In case you choose to prefer it over Windows’ different find.exe and use from cmd)

9 Answers 9

Try putting it in quotes — you’re running into the shell’s wildcard expansion, so what you’re acually passing to find will look like:

find . -name bobtest.c cattest.c snowtest.c 

. causing the syntax error. So try this instead:

Note the single quotes around your file expression — these will stop the shell (bash) expanding your wildcards.

By way of example, you can see what’s happening if you do echo *test.c . the result won’t be echo expanding the wildcard, but the shell itself. The simple lesson is if you’re using wildcards, quote the filespec 🙂

Thanks for helping me with this VARIANT. I tried find . -type f -printf ‘%TY-%Tm-%Td %TT %p\n’ as found on the web, and was met with «paths must precede expression». Problem was the quote marks were too «smart». I retyped the command, causing the quotes to be replaced, and it ran.

single quotes for wildcard searches worked with Busybox & GNU find — if using a wildcard *.$variable you need double quotes.

You may aslo escape the * with a backslash ‘\’ as stated by other users here. A helpful guide about the notorious find can be found here

What’s happening is that the shell is expanding «*test.c» into a list of files. Try escaping the asterisk as:

#gitbash this was the solution for me with git bash on windows, even when quoting the PATTERN find . -name ‘*txt’

NON-BUGS Operator precedence surprises The command find . -name afile -o -name bfile -print will never print afile because this is actually equivalent to find . -name afile -o \( -name bfile -a -print \). Remember that the precedence of -a is higher than that of -o and when there is no operator specified between tests, -a is assumed. “paths must precede expression” error message $ find . -name *.c -print find: paths must precede expression Usage: find [-H] [-L] [-P] [-Olevel] [-D . [path. ] [expression] This happens because *.c has been expanded by the shell resulting in find actually receiving a command line like this: find . -name frcode.c locate.c word_io.c -print That command is of course not going to work. Instead of doing things this way, you should enclose the pattern in quotes or escape the wildcard: $ find . -name '*.c' -print $ find . -name \*.c -print 

I see this question is already answered. I just want to share what worked for me. I was missing a space between ( and -name . So, the correct way of chosen files with excluding some of them would be like below;

find . -name 'my-file-*' -type f -not \( -name 'my-file-1.2.0.jar' -or -name 'my-file.jar' \) 

I came across this question when I was trying to find multiple filenames that I could not combine into a regular expression as described in @Chris J’s answer, here is what worked for me

find . -name one.pdf -o -name two.txt -o -name anotherone.jpg 

-o or -or is logical OR. See Finding Files on Gnu.org for more information.

Читайте также:  Path in linux where to set

I was running this on CygWin.

Источник

Find throw paths must precede expression in script

Aliases don’t accept positional parameters. You’ll need to use a function.

You’ll also need to quote some of your arguments.

This defers the expansion of the glob so that find performs it rather than the shell.

No luck there I added function f() < find . -name "$1" -type f -print0 | xargs -0 grep -i "$2"; >to the .bash_profile and source it. Hits the same error [a@a ~]$ f «*.php» function find: paths must precede expression Usage: find [-H] [-L] [-P] [path. ] [expression]

@QuintinPar: The function definition and execution in your comment works for me without error. Double check to make sure what you’re actually trying matches what you posted. Also, try declare -f f to display the function definition for verification. Also, it’s not necessary to use the function keyword when you define a function using () (or vice versa). Bash is one of the few shells that accepts them together.

@quinton: you need to invoke f with the first argument in single quotes. Using double quotes is very different.

@WilliamPursell: Only if there’s a variable or parameter expansion. There’s no difference between double and single quotes for globbing. The special parameters $* and $? which have superficial similarity to globbing will be expanded inside double quotes unless the dollar sign is escaped (in the case that you’re looking for a file with a literal dollar sign in that position in its name) or the specification is enclosed in single quotes. In the case of either single or double quotes, if you’re looking for a filename which includes a literal asterisk, you’ll need to escape it.

Источник

Оцените статью
Adblock
detector