I have 800 file with naming as:


till 30 for a month 01 and so on for all the 12 months .

I want is to rename all the files in a single go as:


and so on for the corresponding months.

A one-liner if possible. Os -- rhel5

  • How do the first two file names correspond to the second two? Which part of the filename needs to be transcribed to the target? Is it Centrum.text.YYYYMM-DD mapped to Centrum.YYYYDD-MM.text, for example? Or Centrum.text.XXXXXX-XX mapped to Centrum.XXXXXX-XX.text (i.e. just moving the text part)? Please edit your question to make it clear. – roaima Jul 4 '16 at 10:05


 rename -n 's/Centrum.text.(.*)/Centrum.$1.text/' Cen*

for a preview

  • s/Centrum.text.(.*)/Centrum.$1.text/ instruct to replace (moving en pattern inside)


rename  's/Centrum.text.(.*)/Centrum.$1.text/' Cen*


if you don't have rename, and from directory containing files

ls -1 | awk -F. '/Centrum/ {printf "mv %s.%s.%s %s.%s.%s\n",$1,$2,$3,$1,$3,$2}'

for the preview

  • -1 (one file per line) in ls might be useless, but just in case you have alias to ls.
  • /Centrum/ filter on filename
  • {printf "mv %s.%s.%s %s.%s.%s\n",$1,$2,$3,$1,$3,$2} swap date and text field.

add | bash at the end if all is good.

  • Note that this will only work if the OP has rename pointing to Perl rename which is the default on Debian based systems. – terdon Jul 4 '16 at 9:51
  • @archemar: nothing happens. These files are inside a directory named avi – AVS Jul 4 '16 at 9:55
  • @ABHISHEKSHARMA if the files are in a subdirectory, and you didn't tell us that, you need to be in that subdirectory to apply the command. – roaima Jul 4 '16 at 10:00
  • @don_crissti you are absolutly right, can you point me to rename's for HP-UX and Solaris ? (question was tag rhel after my first answer), besides I like overly obfuscate code. :) – Archemar Jul 4 '16 at 11:03
  • @don_crissti rightly or wrongly, I wasn't responding to the merits (or otherwise) of the answer. I was concerned that the "nothing happens" coupled with "These files are inside a directory named avi" might mean that the OP had simply run the command without thinking about the context. – roaima Jul 4 '16 at 11:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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