Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 05-27-2011, 08:18 PM   #1
LQ Newbie
Registered: May 2011
Posts: 9

Rep: Reputation: 0
find -exec not working for me

I have the command:

find . -name '*.a' -or -name '*.b' -exec cp {} /direc/direc/ \;

this will only copy the files with a .b filetype, not the ones with .a. If I do -exec echo {} \; then it only shows files with the .b filetype.

If i just use

find . -name '*.a' -or -name '*.b'

I get file types with both .a and .b

thanks in advance
Old 05-27-2011, 08:39 PM   #2
Senior Member
Registered: Dec 2010
Location: Internet
Distribution: Linux Mint, SLES, CentOS, Red Hat
Posts: 2,385

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
@ Reply

Hi there,

The reason the command is not working as you are expecting because {} bracket takes only single expression i.e. it will either take *.a or *.b

As you said when you run the command only files with *.b gets copied that is because when find command start searching from / it found *.b files first and then passed those file names to {} for cp command to get executed.

If you will run the same command from a directory where there are both *.a and *.b files are together then only *.a file will get copied.

You can verify that performing a simple test. Create a directory say testfind under / and create some blank files using touch say a.a, aa.a, abc.a, bbc.b, zyz.b and then run the following command:

find /testfind -name '*.a' -or -name '*.b' -exec cp {} /root/testprocessed/ \;

You will see that only files with *.a got copied and not *.b. Above command you are starting find specifically from /testfind for testing.
Old 05-30-2011, 04:27 AM   #3
Karl Godt
Registered: Mar 2010
Location: Kiel , Germany
Distribution: once:SuSE6.2,Debian3.1, aurox9.2+3,Mandrake?,DSL? then:W7st,WVHB, #!8.10.02,PUPPY4.3.1 now:Macpup
Posts: 314

Rep: Reputation: 45
this works for me :
find . -name "*.txt" -exec echo {} \; -o -name "*.c*" -exec echo {} \;
Old 05-30-2011, 04:39 AM   #4
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Originally Posted by jimmyrp View Post
I have the command:

find . -name '*.a' -or -name '*.b' -exec cp {} /direc/direc/ \;

this will only copy the files with a .b filetype, not the ones with .a.
That's correct. Take in mind that when you don't specify an action the default behaviour is to print out the name of the items that match the specified criteria. But when you specify at least one action, the default is not applied anymore. Moreover the -o option excludes any action from the left-hand side if the right-hand side is TRUE. Just to tell that the -exec action belongs only to the right-hand side of the expression.

If you want to apply the same action to both the conditions, you have to group them together, using escaped parentheses:
find . \( -name '*.a' -o -name '*.b' \) -exec cp {} destination \;


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 Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] subshell in a find -exec not working as expected ebarrere Linux - General 4 07-22-2010 07:31 PM
find and -exec {} gleipnir Linux - Newbie 6 02-23-2010 06:22 PM
Find with -exec argument - not giving proper to find... hinetvenkat Linux - Server 4 01-25-2010 07:19 AM
find -exec syntax: bug in find? mfcarroll Programming 5 06-21-2007 08:13 PM
find -exec help dc144 Linux - Newbie 3 10-12-2006 03:14 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:12 PM.

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