Copy only certain file types from a folder structure to another

Clash Royale CLAN TAG#URR8PPP
I have a top folder with many sub-folders. It's named "a". There are many .png and .jpg files in there. I'd like to recursively copy "a" into a new folder "b", but only copy the .png and .jpg files. How do I achieve that?
files file-copy recursive
add a comment |
I have a top folder with many sub-folders. It's named "a". There are many .png and .jpg files in there. I'd like to recursively copy "a" into a new folder "b", but only copy the .png and .jpg files. How do I achieve that?
files file-copy recursive
2
rsync -a --include='*.png' --include='*.jpg' --include='*/' --exclude='*' a/ b/If you want to prune empty dirs add the-mswitch :rsync -am ....
– don_crissti
Feb 21 '16 at 20:18
1
Please specify whether you want to recreate the directory structure (including only thejpg/pngfiles) underbor you just want to recursively search forjpg/pngunderaand copy them tob?
– don_crissti
Feb 21 '16 at 21:02
2
If you want to copy the directory tree recursively, this is a duplicate of unix.stackexchange.com/questions/2161/… . If you want to put all the files directly under b, I can't find a duplicate. Which is it?
– Gilles
Feb 21 '16 at 21:50
add a comment |
I have a top folder with many sub-folders. It's named "a". There are many .png and .jpg files in there. I'd like to recursively copy "a" into a new folder "b", but only copy the .png and .jpg files. How do I achieve that?
files file-copy recursive
I have a top folder with many sub-folders. It's named "a". There are many .png and .jpg files in there. I'd like to recursively copy "a" into a new folder "b", but only copy the .png and .jpg files. How do I achieve that?
files file-copy recursive
files file-copy recursive
edited Feb 21 '16 at 23:35
don_crissti
51.4k15138167
51.4k15138167
asked Feb 21 '16 at 17:14
JasonGenXJasonGenX
12314
12314
2
rsync -a --include='*.png' --include='*.jpg' --include='*/' --exclude='*' a/ b/If you want to prune empty dirs add the-mswitch :rsync -am ....
– don_crissti
Feb 21 '16 at 20:18
1
Please specify whether you want to recreate the directory structure (including only thejpg/pngfiles) underbor you just want to recursively search forjpg/pngunderaand copy them tob?
– don_crissti
Feb 21 '16 at 21:02
2
If you want to copy the directory tree recursively, this is a duplicate of unix.stackexchange.com/questions/2161/… . If you want to put all the files directly under b, I can't find a duplicate. Which is it?
– Gilles
Feb 21 '16 at 21:50
add a comment |
2
rsync -a --include='*.png' --include='*.jpg' --include='*/' --exclude='*' a/ b/If you want to prune empty dirs add the-mswitch :rsync -am ....
– don_crissti
Feb 21 '16 at 20:18
1
Please specify whether you want to recreate the directory structure (including only thejpg/pngfiles) underbor you just want to recursively search forjpg/pngunderaand copy them tob?
– don_crissti
Feb 21 '16 at 21:02
2
If you want to copy the directory tree recursively, this is a duplicate of unix.stackexchange.com/questions/2161/… . If you want to put all the files directly under b, I can't find a duplicate. Which is it?
– Gilles
Feb 21 '16 at 21:50
2
2
rsync -a --include='*.png' --include='*.jpg' --include='*/' --exclude='*' a/ b/ If you want to prune empty dirs add the -m switch : rsync -am ....– don_crissti
Feb 21 '16 at 20:18
rsync -a --include='*.png' --include='*.jpg' --include='*/' --exclude='*' a/ b/ If you want to prune empty dirs add the -m switch : rsync -am ....– don_crissti
Feb 21 '16 at 20:18
1
1
Please specify whether you want to recreate the directory structure (including only the
jpg/png files) under b or you just want to recursively search for jpg/png under a and copy them to b ?– don_crissti
Feb 21 '16 at 21:02
Please specify whether you want to recreate the directory structure (including only the
jpg/png files) under b or you just want to recursively search for jpg/png under a and copy them to b ?– don_crissti
Feb 21 '16 at 21:02
2
2
If you want to copy the directory tree recursively, this is a duplicate of unix.stackexchange.com/questions/2161/… . If you want to put all the files directly under b, I can't find a duplicate. Which is it?
– Gilles
Feb 21 '16 at 21:50
If you want to copy the directory tree recursively, this is a duplicate of unix.stackexchange.com/questions/2161/… . If you want to put all the files directly under b, I can't find a duplicate. Which is it?
– Gilles
Feb 21 '16 at 21:50
add a comment |
4 Answers
4
active
oldest
votes
find a ( -name "*.png" -or -name "*.jpg" ) -exec cp b ;
Whoops, I accidentally used*.pngtwice and forgot.jpg. This is what happens when I try to answer questions while listening to music. Thanks for catching my mistake.
– gardenhead
Feb 21 '16 at 20:56
OK, I think I got it this time. I learned something new aboutfindtoday. Thanks.
– gardenhead
Feb 21 '16 at 21:06
One more thing: stick with-oinstead of-or, it's more portable.
– don_crissti
Feb 21 '16 at 21:13
add a comment |
for file in $(find a -name "*.jpg" -o -name "*.png")
do
cp $file b/$file
done
add a comment |
One-liner
cp $(find a -name "*.jpg" -o -name "*.png") b
add a comment |
A bit shorter if there are more file types, using brace expansion
cp -r source_directory/*.png,jpg,jpeg target_directory
This gives an error whenever a file of a type does not exist. see https://serverfault.com/a/153893
Add 2>/dev/null to hide these errors
Add || : to not cause an exit code
cp -r source_directory/*.png,jpg,jpeg target_directory 2>/dev/null || :
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f264798%2fcopy-only-certain-file-types-from-a-folder-structure-to-another%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
find a ( -name "*.png" -or -name "*.jpg" ) -exec cp b ;
Whoops, I accidentally used*.pngtwice and forgot.jpg. This is what happens when I try to answer questions while listening to music. Thanks for catching my mistake.
– gardenhead
Feb 21 '16 at 20:56
OK, I think I got it this time. I learned something new aboutfindtoday. Thanks.
– gardenhead
Feb 21 '16 at 21:06
One more thing: stick with-oinstead of-or, it's more portable.
– don_crissti
Feb 21 '16 at 21:13
add a comment |
find a ( -name "*.png" -or -name "*.jpg" ) -exec cp b ;
Whoops, I accidentally used*.pngtwice and forgot.jpg. This is what happens when I try to answer questions while listening to music. Thanks for catching my mistake.
– gardenhead
Feb 21 '16 at 20:56
OK, I think I got it this time. I learned something new aboutfindtoday. Thanks.
– gardenhead
Feb 21 '16 at 21:06
One more thing: stick with-oinstead of-or, it's more portable.
– don_crissti
Feb 21 '16 at 21:13
add a comment |
find a ( -name "*.png" -or -name "*.jpg" ) -exec cp b ;
find a ( -name "*.png" -or -name "*.jpg" ) -exec cp b ;
edited Feb 21 '16 at 21:06
answered Feb 21 '16 at 20:46
gardenheadgardenhead
1,3072710
1,3072710
Whoops, I accidentally used*.pngtwice and forgot.jpg. This is what happens when I try to answer questions while listening to music. Thanks for catching my mistake.
– gardenhead
Feb 21 '16 at 20:56
OK, I think I got it this time. I learned something new aboutfindtoday. Thanks.
– gardenhead
Feb 21 '16 at 21:06
One more thing: stick with-oinstead of-or, it's more portable.
– don_crissti
Feb 21 '16 at 21:13
add a comment |
Whoops, I accidentally used*.pngtwice and forgot.jpg. This is what happens when I try to answer questions while listening to music. Thanks for catching my mistake.
– gardenhead
Feb 21 '16 at 20:56
OK, I think I got it this time. I learned something new aboutfindtoday. Thanks.
– gardenhead
Feb 21 '16 at 21:06
One more thing: stick with-oinstead of-or, it's more portable.
– don_crissti
Feb 21 '16 at 21:13
Whoops, I accidentally used
*.png twice and forgot .jpg. This is what happens when I try to answer questions while listening to music. Thanks for catching my mistake.– gardenhead
Feb 21 '16 at 20:56
Whoops, I accidentally used
*.png twice and forgot .jpg. This is what happens when I try to answer questions while listening to music. Thanks for catching my mistake.– gardenhead
Feb 21 '16 at 20:56
OK, I think I got it this time. I learned something new about
find today. Thanks.– gardenhead
Feb 21 '16 at 21:06
OK, I think I got it this time. I learned something new about
find today. Thanks.– gardenhead
Feb 21 '16 at 21:06
One more thing: stick with
-o instead of -or, it's more portable.– don_crissti
Feb 21 '16 at 21:13
One more thing: stick with
-o instead of -or, it's more portable.– don_crissti
Feb 21 '16 at 21:13
add a comment |
for file in $(find a -name "*.jpg" -o -name "*.png")
do
cp $file b/$file
done
add a comment |
for file in $(find a -name "*.jpg" -o -name "*.png")
do
cp $file b/$file
done
add a comment |
for file in $(find a -name "*.jpg" -o -name "*.png")
do
cp $file b/$file
done
for file in $(find a -name "*.jpg" -o -name "*.png")
do
cp $file b/$file
done
edited Feb 21 '16 at 18:35
Jakuje
16.5k53155
16.5k53155
answered Feb 21 '16 at 17:43
MelBurslanMelBurslan
5,33611533
5,33611533
add a comment |
add a comment |
One-liner
cp $(find a -name "*.jpg" -o -name "*.png") b
add a comment |
One-liner
cp $(find a -name "*.jpg" -o -name "*.png") b
add a comment |
One-liner
cp $(find a -name "*.jpg" -o -name "*.png") b
One-liner
cp $(find a -name "*.jpg" -o -name "*.png") b
answered Feb 21 '16 at 18:48
MarwareMarware
386210
386210
add a comment |
add a comment |
A bit shorter if there are more file types, using brace expansion
cp -r source_directory/*.png,jpg,jpeg target_directory
This gives an error whenever a file of a type does not exist. see https://serverfault.com/a/153893
Add 2>/dev/null to hide these errors
Add || : to not cause an exit code
cp -r source_directory/*.png,jpg,jpeg target_directory 2>/dev/null || :
add a comment |
A bit shorter if there are more file types, using brace expansion
cp -r source_directory/*.png,jpg,jpeg target_directory
This gives an error whenever a file of a type does not exist. see https://serverfault.com/a/153893
Add 2>/dev/null to hide these errors
Add || : to not cause an exit code
cp -r source_directory/*.png,jpg,jpeg target_directory 2>/dev/null || :
add a comment |
A bit shorter if there are more file types, using brace expansion
cp -r source_directory/*.png,jpg,jpeg target_directory
This gives an error whenever a file of a type does not exist. see https://serverfault.com/a/153893
Add 2>/dev/null to hide these errors
Add || : to not cause an exit code
cp -r source_directory/*.png,jpg,jpeg target_directory 2>/dev/null || :
A bit shorter if there are more file types, using brace expansion
cp -r source_directory/*.png,jpg,jpeg target_directory
This gives an error whenever a file of a type does not exist. see https://serverfault.com/a/153893
Add 2>/dev/null to hide these errors
Add || : to not cause an exit code
cp -r source_directory/*.png,jpg,jpeg target_directory 2>/dev/null || :
answered Feb 15 at 13:53
Roger BartonRoger Barton
1
1
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f264798%2fcopy-only-certain-file-types-from-a-folder-structure-to-another%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
rsync -a --include='*.png' --include='*.jpg' --include='*/' --exclude='*' a/ b/If you want to prune empty dirs add the-mswitch :rsync -am ....– don_crissti
Feb 21 '16 at 20:18
1
Please specify whether you want to recreate the directory structure (including only the
jpg/pngfiles) underbor you just want to recursively search forjpg/pngunderaand copy them tob?– don_crissti
Feb 21 '16 at 21:02
2
If you want to copy the directory tree recursively, this is a duplicate of unix.stackexchange.com/questions/2161/… . If you want to put all the files directly under b, I can't find a duplicate. Which is it?
– Gilles
Feb 21 '16 at 21:50