Say, I have several files in a directory.

How do I rename all of them to "001", "002", etc., saving their current file extensions?

  • Terminal method
    cd (change directory) to the folder.
    run command:
  • Command 1

    rename -n -v 'our $n; my $zn=sprintf("%03d", ++$n); s/.*\./$zn./' *.*

This will display the changes. Remove -n for actual rename.
Answer from @Kusalananda

  • Command 2

    a=1; for i in *.*; do new=$(printf "%03d" "$a"); mv -i -- "$i" "$new.${i##*.}"; let a=a+1; done
  • GUI method
    Select all files to be renamed.
    Right click and select rename.
    Delete [Original file name] from the option area. Click on "Add" and select 001, 002, 003, 004.
    Click on "Rename".



enter image description here

| improve this answer | |
  • Will try tomorrow. Sleepy. It's is late here. – Vijay Mar 4 '19 at 19:45
  • 1
    seems like a lot of work when there are multiple 'rename' commands available. – rtaft Mar 4 '19 at 21:00
  • @user68186 edited command-line for use "...through terminal" – Vijay Mar 4 '19 at 21:25

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