Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find and copy file using Bash [duplicate]

Anybody has an alternate way of finding and copying files in bash than:

find . -ctime -15 | awk '{print "cp " $1 " ../otherfolder/"}' | sh 

I like this way because it's flexible, as I'm building my command (can by any command) and executing it after.

Are there other ways of streamlining commands to a list of files?

Thanks

like image 801
Wadih M. Avatar asked Oct 13 '09 18:10

Wadih M.


People also ask

How do I copy duplicate files in Linux?

To copy files and directories use the cp command under a Linux, UNIX-like, and BSD like operating systems. cp is the command entered in a Unix and Linux shell to copy a file from one place to another, possibly on a different filesystem.

How do I duplicate a file in bash?

Copy a File ( cp ) You can also copy a specific file to a new directory using the command cp followed by the name of the file you want to copy and the name of the directory to where you want to copy the file (e.g. cp filename directory-name ).


2 Answers

I would recommend using find's -exec option:

find . -ctime 15 -exec cp {} ../otherfolder \;

As always, consult the manpage for best results.

like image 58
asveikau Avatar answered Sep 30 '22 01:09

asveikau


I usually use this one:

find . -ctime -15 -exec cp {} ../otherfolder/ \; 
like image 43
tangens Avatar answered Sep 30 '22 03:09

tangens