LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   copying files from multiple locations to one destination (https://www.linuxquestions.org/questions/linux-general-1/copying-files-from-multiple-locations-to-one-destination-864043/)

mahmoodn 02-21-2011 09:00 AM

copying files from multiple locations to one destination
 
suppose I have a tree structure like this:
/home/mahmood/sim/a/b/file1.cpp
/home/mahmood/sim/a/b/file2.h
/home/mahmood/sim/a/c/file3.txt
/home/mahmood/sim/d/file4.txt

How can I copy all of them to /home/mahmood/sim. So that when I run "ls" in /home/mahmood/sim, I see all files:
file1.cpp
file2.h
file3.txt
file4.txt

Can 'cp' search for all file and copy them in another folder?

colucix 02-21-2011 09:18 AM

Nope. cp haven't got searching capabilities, it can only use patterns (globbing) that will be expanded by the shell. Anyway you can use find to look for files and apply an action to every file found, e.g.
Code:

find /home/mahmood/sim -type f -exec echo cp -p {} /home/mahmood/sim \;
The echo statement is for testing purposes: the commands will be printed out without being actually executed. Just to review the results before doing the copy. Furthermore you may consider to move or link the files (instead of copying them) to avoid duplicates. Hope this helps.

coolsreejith 02-21-2011 09:22 AM

you can use the command

find /home/mahmood/sim/ -name file* -exec cp {} /home/mahmood/sim/ \;

mahmoodn 02-21-2011 09:33 AM

Thanks it works fine :)


All times are GMT -5. The time now is 04:59 AM.