Rename all files in a folder to a numbered list 1.jpg 2.jpg

I have a folder full of images with several different random file names to help organize this mess, which I would like to rename all of them in a sequential order in one command, so if I have 100 files, it starts to name the first file file-1.jpg file-2.jpg etc. Is this possible in one team?

+6
source share
2 answers

I managed to solve my problem by writing a bash script

 #!/bin/sh num=1 for file in *.jpg; do mv "$file" "$(printf "%u" $num).jpg" let num=$num+1 done 
+6
source

The shortest command line for this that I can think of is

 ls | cat -n | while read nf; do mv "$f" "file-$n.jpg"; done 

ls lists the files in the current directory, and cat -n lists the line numbers. The while reads the resulting numbered list of files line by line, stores the line number in variable n and the file name in variable f and renames it.

+36
source

Source: https://habr.com/ru/post/953440/


All Articles