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 12-24-2013, 10:49 PM   #1
Key67
LQ Newbie
 
Registered: Dec 2013
Posts: 3

Rep: Reputation: Disabled
Looking for a find and copy command


I need a command to find and copy the files listed in test.txt (check the picture) to /destination/folder/
I have been using

Code:
for i in `cat test.txt`;do cp "$i" /destination/folder/; done
to do something close, but all the txt files and test.txt need in the same folder for that command to work. Not sure how to integrate "find" into that kind of command. Also not sure if my test.txt file is correct as far as having to put some kind of directory structure in front of the file names.

http://tinypic.com/r/vn2mub/5

Thanks
 
Old 12-25-2013, 12:35 AM   #2
s.verma
Member
 
Registered: Oct 2013
Distribution: Debian Sid, Gentoo, Arch, Debian
Posts: 169
Blog Entries: 4

Rep: Reputation: 24
Try
Code:
for i in `cat test.txt`;do cp `find . -name $i` /destination/folder/; done
Because find 'pathinwhichtosearch' -name 'filename'
returns
pathinwhichfileis/filename.

Last edited by s.verma; 12-25-2013 at 12:37 AM.
 
Old 12-25-2013, 01:47 AM   #3
divyashree
Senior Member
 
Registered: Apr 2007
Location: bbsr,orissa,India
Distribution: RHEL5 ,RHEL4,CENT OS5,FEDORA,UBUNTU
Posts: 1,362

Rep: Reputation: 135Reputation: 135
HI Key67,Welcome to LQ.

Prefer
Code:
$()
substitution over
Code:
``
and while loop over for.


Code:
while read file;do  $(find . -name $file -exec cp {} /<destdir>  \;);done < test.txt
Where {} stores the files from find.

Last edited by divyashree; 12-25-2013 at 01:49 AM.
 
Old 12-25-2013, 10:52 AM   #4
Key67
LQ Newbie
 
Registered: Dec 2013
Posts: 3

Original Poster
Rep: Reputation: Disabled
Code:
$ while read file;do  $(find . -name $file -exec cp {} /home/4/  \;);done < test.txt
That copied the first file in the list then returned this this
cp: `./4/1a1.txt' and `/home/4/1a1.txt' are the same file
then stopped

Code:
for i in `cat test.txt`;do cp `find . -name $i` /destination/folder/; done
Verma, This one also copied the first file then returned the following for each of the rest of the files.

cp: missing destination file operand after `/home/4/'

Last edited by Key67; 12-25-2013 at 10:53 AM.
 
Old 12-25-2013, 09:36 PM   #5
divyashree
Senior Member
 
Registered: Apr 2007
Location: bbsr,orissa,India
Distribution: RHEL5 ,RHEL4,CENT OS5,FEDORA,UBUNTU
Posts: 1,362

Rep: Reputation: 135Reputation: 135
Quote:
Originally Posted by Key67 View Post
Code:
$ while read file;do  $(find . -name $file -exec cp {} /home/4/  \;);done < test.txt
That copied the first file in the list then returned this this
cp: `./4/1a1.txt' and `/home/4/1a1.txt' are the same file
then stopped

Code:
for i in `cat test.txt`;do cp `find . -name $i` /destination/folder/; done
Verma, This one also copied the first file then returned the following for each of the rest of the files.

cp: missing destination file operand after `/home/4/'
Use the option -f with cp and try it again.
 
Old 12-25-2013, 10:17 PM   #6
saivinoba
Member
 
Registered: Oct 2007
Distribution: Gentoo, Gentoo ~amd64, Linux Mint 17
Posts: 48

Rep: Reputation: 9
find command is recursive so if you are copying something from one of subfolders of lets say home folder (E.g. /home/you/Documents) and want to copy to some other subfolder in the same main folder (E.g. /home/you/Public) you will get that error because after finishing checking in /home/you/Documents it will search in /home/you/Public, tries to copy to itself. But it should not stop copying other files?!

Try:
Code:
for i in $(cat test.txt); do find </main/folder> -iname $i -exec mv '{}' /destination/folder \; ; done
 
Old 12-29-2013, 10:22 AM   #7
Key67
LQ Newbie
 
Registered: Dec 2013
Posts: 3

Original Poster
Rep: Reputation: Disabled
thanks divyashree, adding the -f worked

Code:
while read file;do  $(find . -name $file -exec cp -f {} /home/4/  \;);done < test.txt
 
Old 12-30-2013, 05:02 AM   #8
divyashree
Senior Member
 
Registered: Apr 2007
Location: bbsr,orissa,India
Distribution: RHEL5 ,RHEL4,CENT OS5,FEDORA,UBUNTU
Posts: 1,362

Rep: Reputation: 135Reputation: 135
Quote:
Originally Posted by Key67 View Post
thanks divyashree, adding the -f worked

Code:
while read file;do  $(find . -name $file -exec cp -f {} /home/4/  \;);done < test.txt
Hi Key67, you are welcome. If your issue got resolved, please mark it solved.

Last edited by divyashree; 12-30-2013 at 05:04 AM.
 
  


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] Copy folder using find mkdir and copy TL23 Linux - Newbie 7 06-28-2013 11:45 AM
[SOLVED] Using terminal command -Find files in a folder and copy them to a different folder j-jock Linux - General 4 11-28-2011 03:20 AM
Find and Copy command comdined together manjeshjk Linux - Newbie 8 02-26-2010 06:27 AM
How to find files and copy the found files to the floppy in one command justmehere Linux - Newbie 11 05-05-2008 12:29 AM
Using the find command with a pipe to copy dbrmik Linux - Newbie 3 06-18-2007 06:08 AM


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