find and copy file using Bash


Question

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

1
32
4/25/2018 12:13:56 AM

Accepted Answer

I would recommend using find's -exec option:

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

As always, consult the manpage for best results.

75
10/13/2009 6:36:36 PM

If your cp is GNU's:

find . -ctime 15 -print0 | xargs -0 cp --target-directory=../otherfolder

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon