Databases
cancel
Showing results for 
Search instead for 
Did you mean: 

unexpected behaviour with find command.

SOLVED
Go to solution
Rajeev Tyagi
Valued Contributor

unexpected behaviour with find command.

Hi,

When i search for *.ksh and *.sh files from my home directory in another directory the result is as expected but when i do the same search from one of the sub-directory in my home directory it gives no result. However if i search for full filename (Eg. test1.ksh) it just works fine.

Depicted below is the problem in commands.

#pwd
/home/users/rajeev
#find /home/prod \( -name *.ksh -o -name *.sh \)
find: cannot open /home/prod/test1
/home/prod/test/test1.ksh
/home/prod/test/test_test1.ksh
#cd sub
#pwd
/home/users/rajeev/sub
#find /home/prod \( -name *.ksh -o -name *.sh \)
find: cannot open /home/prod/test1
Reports nothing.
#find /home/prod -name test1.ksh
/home/prod/test/test1.ksh
#

However "ls -ltR /home/prod" from this sub-directory works fine.
Any help in this case would be appreiciated.
6 REPLIES
Jean-Louis Phelix
Honored Contributor
Solution

Re: unexpected behaviour with find command.

Hi,

The problem is that shell expands meta characters BEFORE executing commands, so *.ksh is expanded if possible into current list of .ksh files if they exist in current dir before executing find. You should better use :

#find /home/prod \( -name \*.ksh -o -name \*.sh \)

or

#find /home/prod \( -name '*.ksh' -o -name '*.sh' \)

Juste to avoid expansion ...

Regards,

Jean-Louis.

It works for me (© Bill McNAMARA ...)
Helen French
Honored Contributor

Re: unexpected behaviour with find command.

Try putting the *.ksh and *.sh within quotes:
'*.ksh' and '*.sh'. This is a good practise to follows whenever you use the find command.
Life is a promise, fulfill it!
A. Clay Stephenson
Acclaimed Contributor

Re: unexpected behaviour with find command.

You would have the same problem without the -o (or) so let's ignore that. The real problem is that the shell is globbing the filenames. The -name arguments expects exactly one argument but if there is more than one file in your current directory that will match *.ksh then find has a syntax error. The solution is rather simple; instruct the sheel to do no filename expansion by surrounding your -name argument with quotes - e.g -name '*.ksh'.
If it ain't broke, I can fix that.
harry d brown jr
Honored Contributor

Re: unexpected behaviour with find command.


I'd add that you should escape-out the PERIOD also:

find /home/prod -type f -name "*\\.ksh" -o -name "*\\.sh"

live free or die
harry
Live Free or Die
harry d brown jr
Honored Contributor

Re: unexpected behaviour with find command.

oops, I thought the submit would mess with the back-slashes:

find /tmp -type f -name "*\.ksh" -o -name "*\.sh"

live free or die
harry
Live Free or Die
Rajeev Tyagi
Valued Contributor

Re: unexpected behaviour with find command.

Thanks for the immediate response. I tried them and it worked.