Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rename all files in folder to numbered list 1.jpg 2.jpg [closed]

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

like image 625
Yamaha32088 Avatar asked Sep 08 '13 17:09

Yamaha32088


People also ask

How do you rename all images in a folder at once with numbers?

You can batch rename images in Windows by selecting (Shift+click or Ctrl+click to select several files; Ctrl+A to select all) and pressing right-click > "Rename". Your file names will look like image (1), image (2), image (3) etc.

How do I rename all files in a folder sequentially?

You can press and hold the Ctrl key and then click each file to rename. Or you can choose the first file, press and hold the Shift key, and then click the last file to select a group.


2 Answers

The most concise command line to do this I can think of is

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

ls lists the files in the current directory and cat -n adds line numbers. The while loop reads the resulting numbered list of files line by line, stores the line number in the variable n and the filename in the variable f and performs the rename.

like image 109
Sven Marnach Avatar answered Dec 25 '22 07:12

Sven Marnach


I was able 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
like image 45
Yamaha32088 Avatar answered Dec 25 '22 08:12

Yamaha32088