LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 05-27-2011, 07:18 PM   #1
jimmyrp
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, 07:39 PM   #2
T3RM1NVT0R
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, 03:27 AM   #3
Karl Godt
Member
 
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 :
Code:
find . -name "*.txt" -exec echo {} \; -o -name "*.c*" -exec echo {} \;
 
Old 05-30-2011, 03:39 AM   #4
colucix
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
Quote:
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:
Code:
find . \( -name '*.a' -o -name '*.b' \) -exec cp {} destination \;
 
  


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 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 06:31 PM
find and -exec {} gleipnir Linux - Newbie 6 02-23-2010 05:22 PM
Find with -exec argument - not giving proper output..how to find... hinetvenkat Linux - Server 4 01-25-2010 06:19 AM
find -exec syntax: bug in find? mfcarroll Programming 5 06-21-2007 07:13 PM
find -exec help dc144 Linux - Newbie 3 10-12-2006 02:14 AM


All times are GMT -5. The time now is 12:50 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration