korek
korek

Reputation: 1

Renaming multiple files using a Shell Script

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

Upvotes: 0

Views: 4789

Answers (1)

carlpett
carlpett

Reputation: 12583

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.

Upvotes: 3

Related Questions