I'm pretty new to batch renaming and I've looked around a bit and I have found some stuff on renaming lots of files that are all the same type but nothing really on renaming a bunch of files that have different extensions.

by this I mean I have a directory like so:


and I'm trying to figure out how I can rename it to be like this:


I'd like to retain the extensions of the files in the folder but rename them like I have above here.

I've been looking for a while and haven't really come across any example of this, any help would be great!


I think I'd do

shopt -s nullglob

cd Folder

for file in *.*; do 
  printf -v newfile 'Renamed_%04d.%s' $((n++)) "${file##*.}"
  echo mv -- "$file" "$newfile"
mv -- EBUFEWO.png Renamed_0001.png
mv -- file.jpg Renamed_0002.jpg
mv -- otherFile.jpg Renamed_0003.jpg

(echo added for testing purposes). Alternatively, with the perl-based prename

cd Folder
prename -n 'our $n; s/(.*)\.(.*)$/sprintf "Renamed_%04d.%s", ++$n, $2/e' *.*
EBUFEWO.png renamed as Renamed_0001.png
file.jpg renamed as Renamed_0002.jpg
otherFile.jpg renamed as Renamed_0003.jpg

(-n added for testing purposes).

  • Thanks @steeldriver this worked just like I needed it to! also thanks for an additional bit for prename I'll have to check it out(never used prename) – Kyle Littlestar Jan 26 '17 at 1:37

I hope this helps

count=1; \
for file in * ; do ext=${file##*.}; \
while [ ${#count} -ne 3 ]; do count="0"$count; done; \
mv "$file" Renamed_$count.$ext; count=$((10#$count+1)); done

This was tested on the contents of a single folder and performed as you specified.

  1. ext=${file##*.} getting extension of the file to be renamed.
  2. while [ ${#count} -ne 3 ]; do count="0"$count; done you requested that the renamed files hold 3 digits this adds the required number of leading zeros, 1 becomes 001 and 20 becomes 020 etc
  3. mv "$file" Renamed_$count.$ext; renames the file to the parameters specified.

Given my limited experience I am sure there are simpler more efficient methods.

Note that this was executed on a specified folder once.

  • at first it works but once it reached 008 I get a error -bash: ((: 008: value too great for base (error token is "008") – Kyle Littlestar Jan 26 '17 at 1:23
  • my bad ... I have made the necessary adjustments .. – SwallowTail Jan 26 '17 at 23:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.