Add 0 in start of filename


1 - abcd1.txt
2 - abcd2.txt
.
.
10  - abcd3.txt

need to write a script to rename all files that start with a number less than 10, to have a zero in front. ie. 1 - abcd1.txt will be renamed to 01 - abcd1.txt.

Show what you tried first.

Currently nothing in mind that's why ask question here for help. I think need to use find command but stuck in checking of less than 10.

Show your find command etc., show your code.

Nothing yet started, because this checking thing sucks and stop my mind that's why.

---------- Post updated at 01:10 PM ---------- Previous update was at 01:04 PM ----------

Please suggest this one.

#!/bin/bash
#
for i in *.txt
  do mv "$i" "0-${i/.txt}"
done

Based on some assumptions:

#!/bin/bash

for file in *.txt
do
        num="${file//[[:alpha:].]/}"
        nam="${file//[[:digit:].txt]/}"
        printf "%s%02d.txt\n" $nam $num
done

If this works for you, assign printf result to a variable and perform the rename.

It is giving below output.

sh1.sh: line 7: printf: 10-3: invalid number
-abcd10.txt
sh1.sh: line 7: printf: 1-3: invalid number
-abcd01.txt

I need to change 1-abcd1.txt to 01-abcd1.txt and 2-abcd2.txt to 02-abcd2.txt and do till 09-abcd9.txt not do only 10

Like I said before, the code was based on some assumptions.

I assumed your file names are abcd1.txt, abcd2.txt ... abcd10.txt

Here is the modified code that might work for you:

#!/bin/bash

for file in *.txt
do
        num="${file%%-*}"
        nam="${file##*-}"
        printf "%02d-%s\n" $num $nam
done
1 Like

Thanks so much sir.

for file in *.txt
do
  [[ ${file%%-*} -ge 10 ]] && continue
   echo "mv $file 0$file"
done

--ahamed

1 Like

For the names initially given in this thread, a simple way to do what was requested is:

for i in [0-9]\ *
do      mv "$i" "0$i"
done

That's a nice solution, but both of mv's arguments require double quotes.

Regards,
Alister

2 Likes

Yes, obviously. :o

I have updated my post to include the quotes.

  • Don

Giving errors. Please check my question. I want 1-abcd1 to 01-abcd1 till 09-abcd9 but not to add in file that have 10 in start.

mv: cannot stat `[0-9] *': No such file or directory

---------- Post updated at 02:43 AM ---------- Previous update was at 02:34 AM ----------

Thanks Ahamed brother. But in above script we have added 0 from 1 till 9 and file names are

01-abcd1.txt
02-abcd2.txt
...
...
09-abcd9.txt

If i will move them back to 

1-abcd1.txt
2-abcd2.txt
...
...
9-abcd9.txt

I have tried below code but not working. Please advice.

for file in *.txt
do
  [[ ${file%%-*} -ge 10 ]] && continue
   echo "mv 0$file $file"
done

How about this

for i in `ls [0-9]-*.txt`
do
mv "$i" "0${i}"
done
1 Like

It is doing


mv 4-track4.txt 04-track4.txt
mv 5-track5.txt 05-track5.txt

But now i need reverse.

04-track4.txt to 4-track4.txt
05-track5.txt to  5-track5.txt

---------- Post updated at 03:28 AM ---------- Previous update was at 03:26 AM ----------

Please confirm below code is fine?

for i in `ls [0-9]-*.txt`
do
#echo mv "$i" "0${i}"
echo mv  "0${i}" "$i"
done

This is getting silly... You have been given all the needed information to do what is asked so I am closing the thread, if you reoppen again a new thread on the subject I will get angry...
Once given a solution, we here understand you will be testing it completely and ask again if there are doubts in your understanding on what is going on, e.g. you changed some parameters to see if you understood and are in mesure to say what you expected but did not produce...
Why dont you test your code yourself then ask what is wrong if you cant sort it out yourself, the way you are using the forum gives to the public the very strong impression you are not willing to learn, therefore why should we bother loosing our time here?