Ask Ubuntu is a question and answer site for Ubuntu users and developers. 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 a grader for COCI (a competitive programming contest) that reads input files (txt[number].in), compiles and runs a code (txt.cpp) and matches the result with an output file (txt[number].out). The problem is, COCI problem's input files have format (, (, ( etc and similar names for output files.

I need to change the extensions of the input files from[number] to txt[number].in. (Need to do the same for the output files as well)

Can anyone help me with this?

I'm currently using 13.04 Ubuntu.

share|improve this question
up vote 3 down vote accepted

Using the terminal, I think this is the easiest way:

mmv -v "*.in.*" ""

Here is a test:

$ ls
$ mmv -v "*.in.*" "" -> : done -> : done -> : done
$ ls

By default, mmv is not installed in Ubuntu, but you can install it from terminal using the following command:

sudo apt-get install mmv

See man mmv for more info.

share|improve this answer
Thanks and sorry for the late reply. I'm away from home and hardly have access to internet and ubuntu. But this looks like its just what I want. I'll get back to as soon as I get to try this. – Labib666 Oct 10 '13 at 11:31
Hi, sorry to bother you again. I just changed the question a bit. Since I dont know how mmv works, I need your help to know how I have to make the change. – Labib666 Oct 11 '13 at 6:42
@Labib666 Ok, see my new edits. But keep in mind: It's not a good practice to change your question after you received your answer. Use Ask Question button if you have new questions. – Radu Rădeanu Oct 11 '13 at 6:51
Thanks again. I'll try not to do such mistakes from the next time. I'm fairly new here. So, just learning from errors. – Labib666 Oct 11 '13 at 12:13
@Labib666 Ok. So, have you seen my new edits at the answer? – Radu Rădeanu Oct 11 '13 at 12:22

If it is okay that a becomes, then this will do

rename 's/$/.in/' *

If you think the two in's are ugly, then do

rename 's/\.in(\.\d+)$/$' *

Pass the -n option to perform a dry run.

share|improve this answer
You can make it a little simpler than that I think e.g. s/(\.in)(\.\d+)/$2$1/. Also for safety it's good practice to mark the end of the option list i.e. rename -nv -- 's/(\.in)(\.\d+)/$2$1/' * (just in case there happen to be any files starting with -) – steeldriver Oct 4 '13 at 12:44
@steeldriver Good point, the regexp isn't pinned at ^, so the first can also be reduced to 's/$/.in/'. And the second can be simplified even further. Will fix the solution. – zwets Oct 4 '13 at 18:15
I knew this one. Unfortunately .in and .out files need to have same names. So it doesn't really help. Thanks for trying though :) – Labib666 Oct 10 '13 at 11:25

This script will copy all files in the working dir, that match *.in.*

for i in $(ls *.in.*)
    target=$(echo ${i} | awk -F'.' '{ print $1$3"."$2 }')
    cp ${i} ${target}
share|improve this answer
I'll get back to you after I try it on my computer. Thanks. :) – Labib666 Oct 11 '13 at 12:37

A version using rename that works for your revised format would be

rename -- 's/(\.in|\.out)\.(\d+)/$2$1/' *


$ ls  txt.out.1  txt.out.2  txt.out.3
$ rename -nv -- 's/(\.in|\.out)\.(\d+)/$2$1/' * renamed as renamed as renamed as
txt.out.1 renamed as txt1.out
txt.out.2 renamed as txt2.out
txt.out.3 renamed as txt3.out
share|improve this answer
Thanks for the help. :) It would be helpful if you tell me how the commands would look if I was changing extension of[number][alphabet] to txt[number][alphabet].in. I just heard that there were extensions like these as well. :( – Labib666 Oct 11 '13 at 17:50
Well you could change the \d+ (one or more digits) by \w+ (one or more from the set [a-zA-Z0-9_] in any order) although that would match things like 2c3, aBa as well; if you need to match EXACTLY one or more digits optionally followed by one or more alphabetical letters, change (\d+) to (\d+([[:alpha:]]+)?) or ([[:digit:]]+([[:alpha:]]+)?) - you should probably look up some information about perl regular expressions and try to figure out exactly what you need yourself – steeldriver Oct 11 '13 at 18:29

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.