tags:

views:

383

answers:

5

Hi,

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

+3  A: 

I would recommend using find's -exec option:

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

As always, consult the manpage for best results.

asveikau
The find man page suggests using single quotes around the braces since it may get misinterpreted without them.
Jonathan Sternberg
+3  A: 

I usually use this one:

find . -ctime -15 -exec cp {} ../otherfolder/ \;
tangens
+1  A: 

-exec is likely the way to go, unless you have far too many files. Then use xargs.

Norman
+3  A: 

You can do it with xargs:

$ find . -ctime 15 -print0 | xargs -0 -I{} cp {} ../otherfolder

See also grep utility in shell script.

Andrey Vlasovskikh
This has the advantage of being faster than `find -exec` because it doesn't create a new `cp` process for each file. However if you have GNU find you can do `find -exec ... +` instead of `find -exec ... ';'` for the same effect :)
hobbs
+1  A: 

If your cp is GNU's:

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