LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 07-16-2007, 01:51 PM   #1
lead2gold
LQ Newbie
 
Registered: Jul 2007
Posts: 3

Rep: Reputation: 0
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
 
Old 07-16-2007, 02:14 PM   #2
stress_junkie
Senior Member
 
Registered: Dec 2005
Location: Massachusetts, USA
Distribution: Ubuntu 10.04 and CentOS 5.5
Posts: 3,873

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
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!
 
Old 07-16-2007, 02:23 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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.
 
Old 07-16-2007, 02:27 PM   #4
stress_junkie
Senior Member
 
Registered: Dec 2005
Location: Massachusetts, USA
Distribution: Ubuntu 10.04 and CentOS 5.5
Posts: 3,873

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
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.
 
Old 07-16-2007, 02:37 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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...
 
1 members found this post helpful.
Old 07-16-2007, 02:57 PM   #6
lead2gold
LQ Newbie
 
Registered: Jul 2007
Posts: 3

Original Poster
Rep: Reputation: 0
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)

Last edited by lead2gold; 07-16-2007 at 07:05 PM.
 
Old 07-16-2007, 06:26 PM   #7
stress_junkie
Senior Member
 
Registered: Dec 2005
Location: Massachusetts, USA
Distribution: Ubuntu 10.04 and CentOS 5.5
Posts: 3,873

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
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.

Last edited by stress_junkie; 07-16-2007 at 07:08 PM.
 
Old 02-08-2009, 10:04 PM   #8
simplyrahul
LQ Newbie
 
Registered: Oct 2004
Location: India
Posts: 26

Rep: Reputation: 15
Thumbs up Solution to the Problem of using "Find" in crontab

Quote:
Originally Posted by lead2gold View Post
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 ‘*.*’
 
Old 11-27-2009, 10:46 AM   #9
arcs
LQ Newbie
 
Registered: Oct 2009
Posts: 5

Rep: Reputation: 0
Thumbs up 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!!
 
Old 07-13-2011, 02:35 PM   #10
venki_recw
LQ Newbie
 
Registered: Jul 2011
Posts: 1

Rep: Reputation: Disabled
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
 
Old 02-03-2012, 03:01 PM   #11
ppostma1
LQ Newbie
 
Registered: Dec 2006
Location: Michigan USA
Distribution: slackware, ubuntu, Debian, knoppix, Fedora, CentOS
Posts: 14

Rep: Reputation: 0
Talking 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
 
Old 02-27-2014, 07:06 AM   #12
bekseju
LQ Newbie
 
Registered: Feb 2014
Posts: 2

Rep: Reputation: Disabled
Lightbulb Find paths must precede expression

Quote:
Originally Posted by lead2gold View Post
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!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Algorithm to find paths xeon123 Programming 5 04-24-2007 02:18 AM
Wine can't find paths in Mandriva 2005 LE kalahari875 Mandriva 3 06-28-2005 07:22 AM
Where are my paths? colly Linux - General 1 06-17-2004 07:15 AM
'find' expression question Thorsten Linux - General 3 05-03-2004 07:41 AM
Automatically resolving WINDOWS paths to pre-configured Linux paths gazzy Linux - General 1 09-05-2003 10:15 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 04:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration