2

I'm trying to copy all files of a certain type, in one folder and its subfolders! I know cp /source/path /path/to/destination_path. And I know find . -name "*.mp4"

I tried cp "$(find . -name "*.mp4")" /path/to/destination. Though the problem seems to be, that find puts out the results in one string.

Do you got any solution? Thanks in advance. :)

Sam
  • 23
  • 3
  • 2
    Do you want them to be copied into the corresponding subfolders in the destination, or directly into the destination folder? – Paul Dec 15 '16 at 22:51
  • Directly in the destination folder – Sam Dec 15 '16 at 22:55
  • 1
    @CharlesDuffy I don't see the distinction. If he wants to do it more than once, or maybe as part of a larger script, is it on topic? It seems like that would just be fluff and the direct question alone makes a better SSCCE. – Paul Dec 15 '16 at 23:01
  • @Paulpro, ...I generally draw the line in redirecting questions to Unix SE or SU based on whether they're focused on interactive usage. History expansion questions, for instance, are definite: the functionality isn't even enabled by default in scripts at all. On the other side of the line are questions about, say, iterating over content from JSON programmatically, or implicit subprocesses impacting variable scope, &c -- those are unquestionably development problems. This one's admittedly somewhere in the blurry middle, and I made a judgment call -- though I *do* think it to be the right one. – Charles Duffy Dec 15 '16 at 23:16
  • @Paulpro, ...to answer your question literally, though: "If he wants to do it more than once, [...] would it be on-topic?", the answer is no, at least not in those grounds -- we've hashed that out on meta before; off-topic question with "in a script" appended is still off-topic question, unless the act of automating the process at hand raises a new question in and of itself. *That* part isn't blurry at all. – Charles Duffy Dec 15 '16 at 23:18
  • @CharlesDuffy Okay, thank you for answering my question. I think that [this question](http://stackoverflow.com/questions/17372765/how-do-i-prepend-to-a-stream-in-bash) that I asked a few years ago would be in the same blurry middle. In your opinion, should I have asked that on Unix or SU, and if so, which one? – Paul Dec 15 '16 at 23:28
  • I actually think that one's fine here -- the scenario in question is one that I've seen in a scripting context more frequently than an interactive context. But you're right -- blurry, and lots of judgment calls involved. It's a completely legitimate argument that I should be waiting for the less-blurry cases (history expansion, interactive prompt customization, etc) before objecting. :) – Charles Duffy Dec 15 '16 at 23:31

2 Answers2

1

If you want to preserve folder structure, consider rsync:

rsync -r --include='*.mp4' --exclude='*' . /path/to/destination

If you don't need to preserve structure --

On a GNU system (having the nonstandard extension cp -t), you can do this in a manner than runs as few cp instances as possible:

find . -name '*.mp4' -exec cp -t /path/to/destination -- '{}' +

Otherwise, in POSIX-compliant form, one cp per file found:

find . -name '*.mp4' -exec cp -- '{}' /path/to/destination/ ';'
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
0

The output results in one string because you have quotes on the find results.

To execute command for each find result, use -exec instead (works with GNU bash and find):

find -name "*.mp4" -exec cp {} /path/to/destination/ \;
Cyker
  • 9,946
  • 8
  • 65
  • 93
  • 1
    FYI, the ability to not name a location for `find` to search from is a GNUism; for maximal compatibility, `.` (or some other starting location) should be explicitly named. – Charles Duffy Dec 15 '16 at 23:05
  • (Particularly since the OP here doesn't have `cp -t`, they're presumably on a non-Linux platform -- or a platform with the Linux kernel but non-GNU tools -- so portability is a concern). – Charles Duffy Dec 15 '16 at 23:08
  • @CharlesDuffy Interesting. I'm wondering how you find the OP doesn't have `cp -t`. – Cyker Dec 15 '16 at 23:10
  • Since the POSIX method is the only one that worked for them in my answer... – Charles Duffy Dec 15 '16 at 23:12
  • @CharlesDuffy LOL. Anyway, I recommend being as standard-compliant as possible, but sometimes dialects make the task at hand easier. – Cyker Dec 15 '16 at 23:19