0

I have files named t1.txt, t2.txt, t3.txt ... t4.txt and I need a shell script to rename it like this:

file one: M.m.1.1.1.201108290000.ready

file two: M.m.1.1.1.201108290001.ready

etc, the sequence number in the last 4 digits changes.

I'd be grateful if someone helped me :)

Best Regards

5
  • 1
    And what have you tried so far? Commented Aug 29, 2011 at 14:09
  • With simple knowledge of bash you should be able to do that. Commented Aug 29, 2011 at 14:11
  • what I need is Unix 'Linux' shell scrip ,to change file name . Commented Aug 29, 2011 at 15:25
  • cd /home/me/Desktop/files/renam/ seq=201108290000 for file in ls -1 do echo $file prefix=M.m.1.1.1. file_name=M.m.1.1.1.$seq.ready echo $file_name seq=seq+1 mv $file $file_name done Commented Aug 29, 2011 at 15:35
  • 1
    What do you have that isn't working? We're happy to entertain any question you may have while stuck on something, but the community tends to frown when questions try to 'place an order'. Commented Aug 29, 2011 at 16:19

1 Answer 1

3

This might be what you need:

cd /home/me/Desktop/files/renam/
n=201108290000
for file in *.txt; do
    echo $file
    prefix=M.m.1.1.1.
    file_name=M.m.1.1.1.$n.ready
    echo $file_name
    n=$(( $n+1 ))
    mv $file $file_name
done

It's close to what you'd written yourself, you just missed some bash syntax. Note that you might want to change the initial value of n, otherwise for the files you mentioned t1.txt would become M.m.1.1.1.201108290000.ready. Depending on what your use is, that might be confusing.

I'd also advice you to avoid use the names of programs and builtins as variable names, such as seq in your case.

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

Comments

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.