Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have 12 files named dmp_000, dmp_001, etc, all the way to dmp_011. I would like to copy all of them to rdmp_000, rdmp_001, and so on, sequentially so the numbered files coincide. There must be an easy way to do this rather than individually?

share|improve this question
up vote 4 down vote accepted

I used this command to generate some sample data

% for  i in `seq 0 11`;do touch dmp_$(printf "%03d" $i);done

% ls
dmp_000  dmp_001  dmp_002  dmp_003  dmp_004  dmp_005  dmp_006  dmp_007  dmp_008  dmp_009  dmp_010  dmp_011

To duplicate the files and preserve the original files modify/create dates:

% for i in dmp*;do cp -p "$i" "r${i}";done

% ls
dmp_000  dmp_002  dmp_004  dmp_006  dmp_008  dmp_010  rdmp_000  rdmp_002  rdmp_004  rdmp_006  rdmp_008  rdmp_010
dmp_001  dmp_003  dmp_005  dmp_007  dmp_009  dmp_011  rdmp_001  rdmp_003  rdmp_005  rdmp_007  rdmp_009  rdmp_011

To move the files wdmp* to rdmp*

% for i in wdmp*;do newname=$(echo $i|sed 's/^w/r/'); mv "$i" "$newname";done

NOTE: You could also have used @warl0ck's answer for that using the rename command.

share|improve this answer
Thank you so much! That worked great! – Gary Cai Jan 31 '13 at 2:40
In addition to that, what if I need copy wdmp_000, wdmp_001, etc to rdmp_000, rdmp_001? Where I have to rename the file itself? The duplication command adds the 'r' at the beginning, but I would like it to replace the 'w'. – Gary Cai Feb 5 '13 at 9:05
Are you asking how to copy wdmp_000 to rdmp_000, but replace the r with a w? Use the unix command mv instead of cp. I'll add another example for that to the answer. – slm Feb 5 '13 at 13:17
Thanks, that's works! warl0ck's doesn't work somehow. The fortran code doesn't read the new files. – Gary Cai Feb 9 '13 at 6:37

If the use of zsh is acceptable, then:

autoload -U zmv            # load zmv
zmv -C 'dmp_(*)' 'rdmp_$1' # rename files

Or simply in this case:

zmv -C 'dmp_*' 'r$f'

($1, $2 expand to the corresponding (...) groups and $f to the whole file path).

See this link for more examples.

share|improve this answer

Posix compatible, short, and all your files can be referenced ( within the parens ) as positional params 1-12 if you want to expand on it.

% ( set -- wdmp0[0-1][0-9] ;\
> for f ; do cp $f r${f#?} ; done )
share|improve this answer

I'd do this

for f in dmp_{000..011}; do [ -f $f ] && cp "$f" "r$f"; done
share|improve this answer

Try rename command, and only rename those started with dmp_,

rename dmp rdmp dmp_*

To keep the files, try an alternate method like this,

for x in dmp_*; do cp $x r$x; done

share|improve this answer
Does that remove the original files? I would like to keep them, so rename won't work. – Gary Cai Jan 30 '13 at 4:30
@GaryCai updated – warl0ck Jan 30 '13 at 4:42

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.