0

Can I copy *.png files without change subfolders address and name?

Example: Copy /photo to /pic converting /photo/2017/hello.png to /pic/2017/hello.png.

9
  • 2
    You can use rsync. Jun 13, 2018 at 20:37
  • Do you want to keep both /photo and /pic folders?
    – Terrance
    Jun 13, 2018 at 20:45
  • 1
    It should be something like this: mkdir -p pic/2017/ && cp photo/2017/*.png pic/2017/.
    – velix
    Jun 13, 2018 at 20:48
  • Yes i want to keep both Jun 13, 2018 at 21:01
  • 1
    @velix is that not an answer? Jun 13, 2018 at 21:04

2 Answers 2

1

First create the directory you want to copy to:

mkdir -p pic/2017

Then copy over the files. Normally one would just need cp:

cp photo/2017/*.png pic/2017/

As you mentioned there are too many files and you probably receive an “Argument list too long” error, we’ll use printf and xargs to run cp as often as necessary:

printf "%s\0" photo/2017/*.png | xargs -0 cp -t pic/2017/

You can also use mcp from the mmv packageInstall mmv in the following way:

mcp -n "photo/2017/*.png" pic/2017/

-n let’s mcp only list the changes, remove it to actually perform the copying. Note the quoted asterisk: It’s not evaluated by the shell, but rather by mcp.

2
  • But in 2017, there are not all of the files. Some: /photo/2017/11 I do not know about these Jun 13, 2018 at 21:37
  • 3
    @AmirhosseinWP Seems like your question is unclear then – please edit and add the actual file structure, as it is and as you want it to be.
    – dessert
    Jun 13, 2018 at 21:43
0

What you want is to copy the directory recursively, so, you should do:

cp -a /photo /pic

This will copy all files and directories recursively and preserves file metadata.

If /photo has files that are not png files that you do not want to copy, you can do this after doing the above command:

find /pic -not -iname '*.png' -type f -print0 | xargs --no-run-if-empty -0 rm
0

Not the answer you're looking for? Browse other questions tagged .