Skip to content

Instantly share code, notes, and snippets.

@Eddy-Barraud
Last active June 16, 2018 23:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Eddy-Barraud/5f989c3644b5aea2c17d9ad85ab8bb19 to your computer and use it in GitHub Desktop.
Save Eddy-Barraud/5f989c3644b5aea2c17d9ad85ab8bb19 to your computer and use it in GitHub Desktop.
Renaming files in a folder to sequential numbers

Here are multiple ways to rename files inside a folder to a sequential numbers

1- In one line :

ls | cat -n | while read n f; do mv "$f" "$n.extension"; done

you can also use ls -uT to sort files by creation date

2- With the 'rename' command

rename -N 0001 -X 's/.*/$N/' *.jpg

3- With a script

a=1
for i in *.jpg; do
   new=$(printf "%04d.jpg" "$a") #04 pad to length of 4
   mv -i -- "$i" "$new"
   let a=a+1
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment