I am new to unix (using the Mac OS X terminal) and am trying to rename certain files by adding some text in the middle of the filename. For all files in the folder ./temp I would like to replace filenames beginning dr_ic0004
with dr_ic0004_DMN
.
For example
dr_0004_tstat1.txt
and dr_0004_tstat2.txt
with
dr_0004_DMN_tstat1.txt
and dr_0004_DMN_tstat2.txt
respectively.
Suppose you had dr_ic0004foo
, dr_ic0004bar
and dr_ic0004baz
. To see them all, you'd do:
ls dr_ic0004*
To isolate the variable parts (foo
, bar
and baz
) you could run that list through sed:
ls dr_ic0004*|sed 's/^dr_ic0004//'
To rename a single file you would use mv
:
mv dr_ic0004foo dr_ic0004_DMNfoo
And to iterate over a list, you could use a for
loop:
for x in foo bar baz; do echo $x ; done
Put them all together and you get:
for x in `ls dr_ic0004*|sed 's/^dr_ic0004//'`; do mv dr_ic0004$x dr_ic0004_DMN$x; done