1755075 Members
3291 Online
108829 Solutions
New Discussion юеВ

The usage of find

 
Jack_27
Advisor

The usage of find

Hi,

When I used the command as below:
$find . -name "*.c" -o "*.cpp"
All C and CPP files were listed.

However while an extra option "-exec" was appended,only the CPP files appeared.
$find . -name "*.c" -o "*.cpp" -exec ls {} \;

Could you please give some comments?Thanks.

Regards
Jack
12 REPLIES 12
Kevin Lamb_2
Frequent Advisor

Re: The usage of find

Jack,

This could be due to the syntax of your first find only looking for *.c files and ignoring the second option.

Try the following:

find . -name "*.c" -o -name "*.cpp" -exec ls -l {} \;

Kev
I'd Rather be Flying!!!
Carlos Fernandez Riera
Honored Contributor

Re: The usage of find

Try:

find . -name "*.c" -o "*.cpp" | xargs ls


or
find . \( -name "*.c" -o "*.cpp\) -exec ls {} \;


unsupported
Deepak Extross
Honored Contributor

Re: The usage of find

Jack,

Try this:
find . -name "*.c" -exec ls {} \; -o -name "*.cpp" -exec ls {} \;

Hope this helps.
-deepak.
harry d brown jr
Honored Contributor

Re: The usage of find

find . \( -name "*.c" -o -name "*.cpp" \) -exec ls {} \;


live free or die
harry
Live Free or Die
Steve Steel
Honored Contributor

Re: The usage of find

Hi


See man

Format is

find . \( -name '*.c' -o -name '*.cpp' \) -exec ls {} \;


Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
harry d brown jr
Honored Contributor

Re: The usage of find

Jack,

by "grouping" them in parens you tell find to locate either a "*.c" file OR "*.cpp", then when a result is found execute the command in "-exec".

live free or die
harry
Live Free or Die
Jean-Luc Oudart
Honored Contributor

Re: The usage of find

Jack,

you may use a pipe to xargs as this would speed up the output.

find . -name "*.c" -o "*.cpp" | xargs ls

if the list is too long for "ls" use the -l option of xargs.

man xargs.

Jean-Luc
fiat lux
Trond Haugen
Honored Contributor

Re: The usage of find

According to the manpage the expression needs to be inside escaped parentheses. So your command should be:
find . \(-name "*.c" -o "*.cpp"\) -exec ls {} \;

Regards,
Trond
Regards,
Trond Haugen
LinkedIn
Jack_27
Advisor

Re: The usage of find

Hi,all friends

Many thanks for your suggestion.
Now it works correctly as we wish.

There or more formats:
$cd prog
$ls
c-file perl shell
$ls c-file/
1.tar megaco now.tar src1 src3 src5
first.c now second.cpp src2 src4 telnet
$find . -name "*.c" -exec ls {} \; -o -name "*.cpp" -exec ls {} \;
./c-file/src1/test1.c
./c-file/src1/test2.c
./c-file/second.cpp
./c-file/first.c
$find . \( -name '*.c' -o -name '*.cpp' \) -exec ls {} \;
./c-file/src1/test1.c
./c-file/src1/test2.c
./c-file/second.cpp
./c-file/first.c
$find . \( -name "*.c" -o -name "*.cpp" \) -exec ls {} \;
./c-file/src1/test1.c
./c-file/src1/test2.c
./c-file/second.cpp
./c-file/first.c
$