LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Looking for a find and copy command (https://www.linuxquestions.org/questions/linux-newbie-8/looking-for-a-find-and-copy-command-4175489091/)

Key67 12-24-2013 09:49 PM

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

s.verma 12-24-2013 11:35 PM

Try
Code:

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

divyashree 12-25-2013 12:47 AM

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.

Key67 12-25-2013 09:52 AM

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/'

divyashree 12-25-2013 08:36 PM

Quote:

Originally Posted by Key67 (Post 5086782)
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.

saivinoba 12-25-2013 09:17 PM

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

Key67 12-29-2013 09:22 AM

thanks divyashree, adding the -f worked

Code:

while read file;do  $(find . -name $file -exec cp -f {} /home/4/  \;);done < test.txt

divyashree 12-30-2013 04:02 AM

Quote:

Originally Posted by Key67 (Post 5088492)
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.


All times are GMT -5. The time now is 10:40 AM.