0

I have some files named as below:

e2ed150l145l245St01-Oxs_MinDriver-Magnetization-00-0002105.omf
e2ed150l145l250St01-Oxs_MinDriver-Magnetization-00-0002167.omf
e2ed150l145l255St01-Oxs_MinDriver-Magnetization-00-0001519.omf
e2ed150l145l260St01-Oxs_MinDriver-Magnetization-00-0001841.omf
e2ed150l145l265St01-Oxs_MinDriver-Magnetization-00-0002730.omf
e2ed150l145l270St01-Oxs_MinDriver-Magnetization-00-0002788.omf
e2ed150l145l275St01-Oxs_MinDriver-Magnetization-00-0001616.omf
e2ed150l150l250St01-Oxs_MinDriver-Magnetization-00-0002386.omf
e2ed150l150l2100St01-Oxs_MinDriver-Magnetization-00-0015577.omf

I want to rename them to:

e2ed150l145l245St01.omf
e2ed150l145l250St01.omf
e2ed150l145l255St01.omf
e2ed150l145l260St01.omf
e2ed150l145l265St01.omf
e2ed150l145l270St01.omf
e2ed150l145l275St01.omf
e2ed150l150l250St01.omf
e2ed150l150l2100St01.omf

Following some of the similar forums I used several commands including:

rename 's/-Oxs_MinDriver-Magnetization-00-[0-9]*//g' e2e*.omf -v

But it did not work. Please advise.

0

1 Answer 1

1

You can also use mv as well:

for i in e2e*.omf; do
    mv $i ${i%%-*}.omf
done

or

for i in e2e*.omf; do
    mv $i ${i//-*/}.omf
done

To collapse to a single line:

for i in e2e*.omf; do mv $i ${i%%-*}.omf; done

If the destination already exists use mv -f

Sign up to request clarification or add additional context in comments.

1 Comment

That is a part of parameter expansion being used for substring extraction. One of the most handy parts of bash. ${var##*x} means start at left and remove everything, up to and including, x. (use one # for just the first occurrence). ${var%%x*} is the same but start at the right. (note: how * changes position) Same note for one %. The second form ${var//x*/} is substring replacement. Here it says find x* and replace it with nothing. True form is ${var//x/y/}. With y being nothing -- the closing / is implied. HTH

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.