Ubuntu: How can I change file types of multiple files at once?



Question:

I would like to change file type of multiple files at once. The files are like A.1, A.2, A.3 ... and I want them to be A.01, A.02, A.03 ...

It's kind of weird file type, but that's just how my program outputs results.

I use Ubuntu 14.04 for your information.

Thank you in advance,


Solution:1

Although there are simpler ways in this particular case, the general approach I'd take is to re-print the numeric suffix with your desired field width and padding - either using the shell's printf function

for f in A*; do     printf -v fnew '%s.%02d' "${f%.*}" "${f##*.}"    echo mv -v -- "$f" "$fnew"  done  

(remove the echo once you've checked that it's doing the right thing) or with the perl-based rename/prename

prename -vn -- 's/\.(\d+)$/sprintf ".%02d", $1/e' A*  

(this time, remove the n to actually rename).


Solution:2

I am assuming:

  • you just want to insert 0 after the . in all the filenames
  • A represents whatever is before the . and it is the same every time and has no special characters (for example, there is only one .)

If this is true it is very easy to replace A. with A.0 in all the file names.

(However, you should definitely do what steeldriver says, as there's no doubt it's an entirely superior method)

Make sure you are in the directory where the files are (open a terminal and cd to the right directory, or navigate to it in Nautilus and then right click and select "open in terminal") by doing ls - you should see the files you want to rename. You can use a for loop

for f in A*; do mv "$f" "${f/A./A.0}"; done  

Or even easier, use rename

rename 's/A\./A.0/' A*  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »