Copy files `*XXX*` to `*YYY*`

时间:2016-04-04 17:57:14

标签: bash copy-paste cp

I have a bunch of files with a XXX string in the name, like:

  • someXXXfile
  • someotherXXXfile
  • ...

I want to take all these files and copy them in the same folder, changing XXX with another string YYY, so I obtain in the same folder:

  • someXXXfile
  • someotherXXXfile
  • ...
  • someYYYfile
  • someotherYYYfile
  • ...

How can I do it?

2 个答案:

答案 0 :(得分:4)

In BASH you can do:

for f in *XXX*; do echo mv -i "$f" "${f/XXX/YYY}"; done

If you have rename utility then use:

rename 's/XXX/YYY/' *XXX*

答案 1 :(得分:2)

Use the super helpful rename command. Think of it as rename from XXX to YYY on all files.

rename XXX YYY *

If you are looking to copy in addition to move, copy to some subdirectory first and then move back.