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?
Asked
Active
Viewed 1.7k times
10

don_crissti
- 82,805

JasonGenX
- 203
5 Answers
8
find a \( -name "*.png" -or -name "*.jpg" \) -exec cp {} b \;

gardenhead
- 2,017
-
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 -
-
2
for file in $(find a -name "*.jpg" -o -name "*.png")
do
cp ${file} b/${file}
done

Jakuje
- 21,357

MelBurslan
- 6,966
0
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 || :
0
You could use bash globstar with cp --parents
:
shopt -s globstar
mkdir -p dest
cp --reflink=auto --parents **/*.jpg **/*.png dest/

xiota
- 526
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:18jpg/png
files) underb
or you just want to recursively search forjpg/png
undera
and copy them tob
? – don_crissti Feb 21 '16 at 21:02