10

I have a bunch of text files with the name

foo_bar_abc_1_01_geh_original.in
foo_bar_abc_1_02_geh_original.in
foo_bar_abc_1_03_geh_original.in
...
...
foo_bar_abc_1_1000_geh_original.in

I would like to copy (and keep the original files) each file and paste to different names (in the same folder) like,

foo_bar_abc_1_01_geh_copy.in
foo_bar_abc_1_02_geh_copy.in
foo_bar_abc_1_03_geh_copy.in
...
...
foo_bar_abc_1_1000_geh_copy.in

How can I do this using a simple script?

0

2 Answers 2

13

Change directory to where you have the original files.

Then test with the following command line,

for i in *_original.*;do echo cp -p "$i" "${i/_original./_copy.}";done

and if it looks good, remove echo and do the copying,

for i in *_original.*;do cp -p "$i" "${i/_original./_copy.}";done
8

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

mcp "*original*" "#1copy#2"

This will copy every file containing the string “original” in the current directory and replace this string with “copy”. You can test what mcp will do by adding the -n flag, however it will not silently overwrite files, but ask you. Using mmv has the advantage over cp that you don’t have to call it for every single file – with one thousand files like you seem to copy, that makes a difference.

You could also use GNU parallel Install parallel in the following way (--dry-run is for testing, remove it to perform the copying):

parallel --dry-run cp -p "{}" "{=s/original/copy/=}" ::: *

or, if that gives the “Argument list too long” error:

printf "%s\0" * | parallel --dry-run -0 cp -p "{}" "{=s/original/copy/=}"

Example run

$ ls -1
foo_bar_abc_1_01_geh_original.in
foo_bar_abc_1_02_geh_original.in
foo_bar_abc_1_03_geh_original.in
foo_bar_abc_1_04_geh_original.in
$ mcp "*original*" "#1copy#2"
$ ls -1
foo_bar_abc_1_01_geh_copy.in
foo_bar_abc_1_01_geh_original.in
foo_bar_abc_1_02_geh_copy.in
foo_bar_abc_1_02_geh_original.in
foo_bar_abc_1_03_geh_copy.in
foo_bar_abc_1_03_geh_original.in
foo_bar_abc_1_04_geh_copy.in
foo_bar_abc_1_04_geh_original.in

Let’s time it, run over 1000 files of 100 KiB each on a slow machine:

$ time mcp "*original*" "#1copy#2"
real    0m1.114s
user    0m0.000s
sys     0m0.132s

You must log in to answer this question.

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