Copy specific files recursively

Hi,

I have a media center folder with many subfolders, most of each (but not all) with media files and one file named “folder.jpg” and from zero to many files named “<filename>.tbn”.

I’d like to copy only the *.jpg and *.tbn to a backup folder recursively, i.e., recreating the folders for each “folder.jpg” and *.tbn group.

First I’ve tried, for jpeg only:

cp -r *.jpg ~/public_html/backup

but I get the message

impossible to make stat in `*jpg’: File or directory not found

If I do:

cp -r * ~/public_html/backup

it works, but copies everything, obviously.

I’m sure this is simple, but I haven’t found a way yet. Any help will be greatly appreciated.

TIA

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

How I would do it (untested):

find ./ -iname ‘*.jpg’ -print0 | xargs -0 cp -t ~/public_html/backup

You can probably do it more easily with the -exec option to the find
command even.

Good luck.

On 05/03/2010 11:26 AM, brunomcl wrote:
>
> Hi,
>
> I have a media center folder with many subfolders, most of each (but
> not all) with media files and one file named “folder.jpg” and from zero
> to many files named “<filename>.tbn”.
>
> I’d like to copy only the *.jpg and *.tbn to a backup folder
> recursively, i.e., recreating the folders for each “folder.jpg” and
> *.tbn group.
>
> First I’ve tried, for jpeg only:
>
> Code:
> --------------------
> cp -r *.jpg ~/public_html/backup
> --------------------
>
> but I get the message
>> impossible to make stat in `*jpg’: File or directory not found
>
> If I do:
>
> Code:
> --------------------
> cp -r * ~/public_html/backup
> --------------------
>
> it works, but copies everything, obviously.
>
> I’m sure this is simple, but I haven’t find a way yet. Any help will be
> greatly appreciated.
>
> TIA
>
>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.12 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQIcBAEBAgAGBQJL3w13AAoJEF+XTK08PnB51MsQAL8BP56M+BSrIf8D6YPBffnz
YndCx4Hes+J1bcPvtlXs14bf+JTzSvdThOa4vKICqpch2V5u/22kJdyJwxscG862
aSgN+zc9XvBOFh7n430uhLOzVj7XmjOnEivMqu7e0g8laSQ1ZyKvrKBZJpzsrAhv
5l5LULkPKMkZVrQgook9zxcQk2YwZV4uDe34Pnd41rYrAD0bFQLKCUEd1wFflupI
dNkFwpBw4uhFHSafG+/KR6Q7SjbVadtQuQ/vrbK43lzu1OaHM0cKSiKj7sBLHWgp
Tq+VC2lKrK9WENjNUiPuupKSgd3tAiVKdeyyiqA6pPj+m6jv9IsDyFIoph4zNC6B
JMb8tHraBUwGc23L0v86BbC++j5+4hE7btAvUHm1QH2FLkPQaU07whkF4iDeGOwp
/eQm6fdhQ+upV7Rs8xvzJ53KeB0GEikctb5ruwRoUdD14UGhhXwq7tSKUGYVWTbN
GgPGe3AzdQQIEQTyL5T/zZKAiOPRmqlGXOtfshvnNWXBxKEUinXxzH5Hl51u6Yrd
Xg+piiO9rTTmDVGNlXmoC5fOcivcoRB4q+BMN00LzOEnLbeQ8ts8AML05CcfQIKz
QFYh1QM3kLUvgEKMSDGj/SVwNKqPAHDOrYTguj3VNkwzhX/pf01akMLV5ENn9ry8
Sy16GX4a3RQwmS9znZ3j
=/I95
-----END PGP SIGNATURE-----

You might also want to add --parents to that cp to retain the full path.

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Oooh… that’s neat. I never knew about that one. Three cheers for ken
yap!

Good luck.

On 05/03/2010 03:46 PM, ken yap wrote:
>
> You might also want to add --parents to that cp to retain the full path.
>
>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.12 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQIcBAEBAgAGBQJL31CuAAoJEF+XTK08PnB5iAYQANB42USsOY03xubsnoit0BbT
cN/lfpWRL7OU+rOImnVDVHksxuofykbFlMo+3JH1OfLjklPNlb6NAyaslxvGNvIy
l17vTfv7QZQA2BGDmHbPWkKmAMlf+XhJOV+9+m7amQHsr7PwGzkdimUs4RbHAzwQ
iq9Q3A/sh0KZv8s5X7EfNIhtmoQKYKCQav65oxUu5pFhMVUvWzMWz3V7I3FbfXgN
Ai9p+ssGbvtyeHXGu8SV5CHJKub4AwckxrOavlj7Gj2OEEGaluIHd1tfyA+c5GOa
oGUJZSJeddTwITVBoZWSH8coMXiZmdkfSrxDYW6XF4XCNt/w3AuXbcLyERGPviEc
LlfExvZJcY/Stsej28Lz2KxtnAQzW0stng8PwjE6ojtdM5Q6xDvW0rs0NZuRrE7k
zJ2e8MGwhAJFRK1AuYkyqdhfHqBxVdiFISyonl9sMXSZAQnXHOw7HcWWPRRukNjh
rT0jCXpLJTpDuWd+uHeWPpPak/Y5RMUHsiHjRyi05G7VV0//IvczPdVIlZKDLflS
tw4To2D3vU9ICfhXXrOFyV2MfrAHev3UupDMiuyWRP3d3XsLmtb1lpqrFrukv5Gv
3CiVAKrMgge14rL4jflDoKg9GFCSoBfxzp4qRpy1PNhXo4zhzQj5gIB/WZwjJKN1
ynumK34rS1rfnLs4CzYM
=XVw6
-----END PGP SIGNATURE-----

Brilliant! I made some tests to make sure I wouldn’t hose my system :D, but the combination of your suggestions worked like a charm:

find ./ -iname '*.jpg' -print0 | xargs -0 cp --parents -t ~/public_html/backup

Thank you both ab@novell and ken_yap!