LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 11-24-2011, 04:52 PM   #1
ppatryk
LQ Newbie
 
Registered: Nov 2011
Posts: 8

Rep: Reputation: Disabled
find and copy


hey, trying to find files and copy to another folder, with 2 file extensions, dont know why its not working it is igoring the txt and just copying over doc, could someone take a wee look? -

find $HOME -name "*.txt" -o -name "*.doc" -exec cp {} $HOME/test \;

thank you
 
Old 11-24-2011, 05:07 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
The problem is that there's an implicit "and" that takes precedence over "or", as documented here:

http://mywiki.wooledge.org/UsingFind..._-print_action

The -exec is thus only associated with the second -name, and the first one doesn't have any action attached to it. You need to group the matching options so that the action applies to both:
Code:
find $HOME \( -name "*.txt" -o -name "*.doc" \) -exec cp {} $HOME/test \;
Please use [code][/code] tags around your code, to preserve formatting and to improve readability.

Last edited by David the H.; 11-24-2011 at 05:10 PM. Reason: rewording for clarity
 
1 members found this post helpful.
Old 11-24-2011, 05:07 PM   #3
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 210Reputation: 210Reputation: 210
Checkout this post: http://www.linuxquestions.org/questi...tterns-261595/
 
1 members found this post helpful.
Old 11-24-2011, 05:28 PM   #4
ppatryk
LQ Newbie
 
Registered: Nov 2011
Posts: 8

Original Poster
Rep: Reputation: Disabled
thanks very much!!!
 
Old 11-24-2011, 05:35 PM   #5
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 147Reputation: 147
Quote:
Originally Posted by David the H. View Post
Code:
find $HOME \( -name "*.txt" -o -name "*.doc" \) -exec cp {} $HOME/test \;
I think find will also recurse into $HOME/test, will it not? If so, then won't it recopy files in $HOME/test over themselves?

I don't think such would necessarily cause problems in this case, other than wasting resources. It just seems to me that find does have some options to avoid such situations.

edit
Here's an example of what I mean.

Code:
tmp$ mkdir -v dir{1..3} target
mkdir: created directory `dir1'
mkdir: created directory `dir2'
mkdir: created directory `dir3'
mkdir: created directory `target'
tmp$ for i in {1..3}; do touch dir$i/$i.{doc,txt}; done
tmp$ ls dir?
dir1:
1.doc  1.txt

dir2:
2.doc  2.txt

dir3:
3.doc  3.txt
tmp$ find \( -name '*.doc' -o -name '*.txt' \) -exec cp -v {} target/ \;
`./dir1/1.doc' -> `target/1.doc'
`./dir1/1.txt' -> `target/1.txt'
cp: `./target/1.doc' and `target/1.doc' are the same file
cp: `./target/1.txt' and `target/1.txt' are the same file
`./dir2/2.txt' -> `target/2.txt'
`./dir2/2.doc' -> `target/2.doc'
`./dir3/3.doc' -> `target/3.doc'
`./dir3/3.txt' -> `target/3.txt'
tmp$ find -wholename './target' -prune -o \( -name '*.doc' -o -name '*.txt' \) -exec cp -v {} target/ \;
`./dir1/1.doc' -> `target/1.doc'
`./dir1/1.txt' -> `target/1.txt'
`./dir2/2.txt' -> `target/2.txt'
`./dir2/2.doc' -> `target/2.doc'
`./dir3/3.doc' -> `target/3.doc'
`./dir3/3.txt' -> `target/3.txt'
tmp$

Last edited by Telengard; 11-24-2011 at 10:22 PM. Reason: provide example
 
  


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
I want a ISO copy of linux 2.6.37 ,where I can find it ? duanxc Linux - Newbie 5 02-15-2011 05:46 AM
[SOLVED] smbmount - find and copy hattori.hanzo Programming 4 11-20-2010 07:52 AM
[SOLVED] Find and copy in the same directory greenpoise Linux - Newbie 20 09-01-2010 02:45 PM
Using the find command with a pipe to copy dbrmik Linux - Newbie 3 06-18-2007 06:08 AM
copy with find ovince Programming 1 03-21-2007 11:46 AM


All times are GMT -5. The time now is 06:10 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