I have a top level folder which contains many subfolders. I am trying to copy the mp4 files from inside those subfolders to a single directory. From the small amount of knowledge I have from codecademy, this command is the closest I could come to the desire function.

cp /masterDir/*.mp4 /outputFolder

but this would only copy any mp4 files found in the top directory.

In short, is there any concise way to tell the command line I want it to go through all the subfolders and copy any that meet the file type? Is this a case for a python "for each" script?

marked as duplicate by steeldriver, Jeff Schaller, ilkkachu, Michael Homer, mdpc Dec 14 '17 at 1:30

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


find + cp solution:

find /masterdir -type f -name "*.mp4" -exec cp -t /outputFolder {} +

You can use find and execute cp for the results

$ find /masterDir -type f -name '*.mp4' -exec cp {} /outputFolder \;


-type f - Find files

-name '*.mp4' - All files ending with .mp4

-exec - Execute cp command within the results

Not the answer you're looking for? Browse other questions tagged or ask your own question.