LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   find: paths must precede expression (http://www.linuxquestions.org/questions/linux-general-1/find-paths-must-precede-expression-569868/)

lead2gold 07-16-2007 01:51 PM

find: paths must precede expression
 
Hi,

I'm new to this forum, and am having a little bit of script troubles...
The command i want works beautifully from the command line:
Code:

$> find . \( -name '*.cpp' -o -name '*.c' -o -name '*.h' -o -name '*.glade' \)  -print | sed -e 's|^\./||g'
However... take that same command and call it from a script file and it will not work.
Code:

#!/bin/bash
CMD="find . \( -name '*.cpp' -o -name '*.c' -o -name '*.h' -o -name '*.glade' \)  -print | sed -e 's|^\./||g'"
$CMD

I get the error:
Code:

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

This is just a small snippit from a larger script; but this is the snippet of the part that doesn't work. Does anyone have any ideas?

Thanks in advance!
Chris

stress_junkie 07-16-2007 02:14 PM

I tried your command with the same result. Then I started chopping parts off of it until it worked. I found that if you change
Code:

....... -print | sed .......
to
Code:

....... -exec sed .......
then the error with the find command goes away and an error with sed is reported. Yay!!! Now I call that progress! :)

pixellany 07-16-2007 02:23 PM

Quote:

Originally Posted by stress_junkie
I tried your command with the same result. Then I started chopping parts off of it until it worked. I found that if you change
Code:

....... -print | sed .......
to
Code:

....... -exec sed .......
then the error with the find command goes away and an error with sed is reported. Yay!!! Now I call that progress! :)

I can't tell if this was intended to be helpful....

As to the original question, here's a guess:
Running "live", the pathname can be abbreviated based on where you are when you run the command. In a script, maybe the pathname can become ambiguous. I'm guessing that putting in the full pathname will work.

stress_junkie 07-16-2007 02:27 PM

Yes it was intended to be helpful. It got rid of the find error. The next step would be to figure out why the sed command reported an error.

pixellany 07-16-2007 02:37 PM

Quote:

Originally Posted by stress_junkie
Yes it was intended to be helpful. It got rid of the find error. The next step would be to figure out why the sed command reported an error.

sorry--I got thrown off by the OP title which just talked about paths...

lead2gold 07-16-2007 02:57 PM

Quote:

Originally Posted by stress_junkie
Yes it was intended to be helpful. It got rid of the find error. The next step would be to figure out why the sed command reported an error.

Hmmm... Thanks for the responses... but your sollution won't work i'm afraid :(.
I guess at this point i should give a little bit more of the snippits :

Code:

#!/bin/bash
## GET INPUT
SOURCEDIR=$1
## ESC INPUT
ESCSOURCEDIR=`echo $SOURCEDIR | sed -e 's|\.|\\\.|g' | sed -e 's|\\$||g'`
## RETRIEVE
CMD="find "$SOURCEDIR" \( -name '*.cpp' -o -name '*.c' -o -name '*.h' -o -name '*.glade' \)  -print | sed -e 's|^${ESCSOURCEDIR}/||g'"
$CMD

the error again:
Code:

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

Essentially the part of the script that isn't working is the part that should generate a large listing of all files (specific to the file extensions). The input of the script should be a directory. Assume error checking is done in the real script. But to simplify things, i'm just going to paste the main issue.

Your last suggestion would never work because the 'find -exec' command expects a {} and a \; which I could never supply. Thus the reason is because i'm looking for information (with find) that causes conflicts with the 'sed' wildcards... this i need to escape them.

Naturally i didn't expect you to have preformed osmosis and figure that one out. I thought maybe my issue was common. But now that i've added a bit more complexity to the issue.... can it still be done?

Chris

EDIT: Thanks to stress_junkies attention, i forgot to update the CMD= line... (above is what i meant it to read)

stress_junkie 07-16-2007 06:26 PM

lead2gold, your last code segment did not have the dot following the find command. I will assume that the second code segment is incorrect. I will update this post with more information if I have anything to report.

OK. This find command does not report an error.
Code:

CMD="find .  -name '*.cpp' -o -name '*.c' -o -name '*.h' -o -name '*.glade'  -print"
I will work on the sed command now. Since I've never used sed but I am familiar with vi so this will be new territory.

(Some time later.)
After a bit of experimenting it seems that the find command doesn't like to have anything to do with another command on the same line. Even a command delimiter, the semicolon, will cause that error.
Code:

CMD="find . -print ;"
Maybe you can put the results of the find command into a file or a FIFO for sed to pick up. That's as far as I can go since I've never used FIFOs and reading the man page for sed was enough new stuff for one day.

simplyrahul 02-08-2009 10:04 PM

Solution to the Problem of using "Find" in crontab
 
Quote:

Originally Posted by lead2gold (Post 2825627)
Hmmm... Thanks for the responses... but your sollution won't work i'm afraid :(.
I guess at this point i should give a little bit more of the snippits :

Code:

#!/bin/bash
## GET INPUT
SOURCEDIR=$1
## ESC INPUT
ESCSOURCEDIR=`echo $SOURCEDIR | sed -e 's|\.|\\\.|g' | sed -e 's|\\$||g'`
## RETRIEVE
CMD="find "$SOURCEDIR" \( -name '*.cpp' -o -name '*.c' -o -name '*.h' -o -name '*.glade' \)  -print | sed -e 's|^${ESCSOURCEDIR}/||g'"
$CMD

the error again:
Code:

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

Essentially the part of the script that isn't working is the part that should generate a large listing of all files (specific to the file extensions). The input of the script should be a directory. Assume error checking is done in the real script. But to simplify things, i'm just going to paste the main issue.

Your last suggestion would never work because the 'find -exec' command expects a {} and a \; which I could never supply. Thus the reason is because i'm looking for information (with find) that causes conflicts with the 'sed' wildcards... this i need to escape them.

Naturally i didn't expect you to have preformed osmosis and figure that one out. I thought maybe my issue was common. But now that i've added a bit more complexity to the issue.... can it still be done?

Chris

EDIT: Thanks to stress_junkies attention, i forgot to update the CMD= line... (above is what i meant it to read)

(Rahul Sinha)

Simply enclosing the wildcard in single quotes makes it work! e.g.

find . -name *.*

arcs 11-27-2009 10:46 AM

Thanks
 
Yes, final posting with tip on using single quotes around search expresison worked a treat:
eg. find . -type f -name 'cronfile*.txt'

Without it, you get that cryptic error message "find: paths must precede expression".

Thanks!!

venki_recw 07-13-2011 02:35 PM

Hi arcs,

are you sure the command really retrieved anything? I agree we do not get the error message anymore, but I guess the command wouldn't retrieve any results at all. I mean, it didn't for me !!! Instead of single quotes, we may need to use double quotes around cronfile* and then append with .txt

This worked for me, and to explain more on the situations where we stumble upon this error. Assume, we have 2 files
1. c_a.xyz
2. c_b_a.xyz
in current directory.

If we do, find . -iname c*.xyz, we get the error.
If we do, find . -iname 'c*.xyz', no files are listed. So, I figured out, the solution would be to use
find . -iname "c*".txt

Any comments / suggestions?

thanks,
Venki

ppostma1 02-03-2012 03:01 PM

got if figured out
 
to explain the wild card issue, the solution is \*

The reason why, and why its a spontaneous error. lets just say I run:
---------------------
]# find / -name fcgi*
---------------------

That works fine in _most_ directories... however, if I'm in the FCGI directory:
---------------------
]# ls
fcgi.c fcgi.h fcgi.o

]# find / -name fcgi*
find: paths mus precede expression

--------------------

its because the shell in this instance replaces
find / -name fcgi*
with:
find / -name fcgi.c fcgi.h fcgi.o

but that does not happen if there are no files matching the pattern in the current directory

bekseju 02-27-2014 07:06 AM

Find paths must precede expression
 
Quote:

Originally Posted by lead2gold (Post 2825550)
Hi,

I get the error:
Code:

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

This is just a small snippit from a larger script; but this is the snippet of the part that doesn't work. Does anyone have any ideas?

Thanks in advance!
Chris

I had a similar problem. I found that adding quotes " ", back-ticks ' ', and an escape character \. Had no effect. The find worked in one script and not another. The cause of my "error" was that one script had to be run as root but the other could be run by a user.

So, there was nothing wrong with the structure of the find statement!

You have probably solved your problem but this reply is submitted now for the benefit of anyone searching for a solution!


All times are GMT -5. The time now is 07:13 AM.