Rename all files in a directory with numbers

Asked
Active3 hr before
Viewed126 times

6 Answers

directoryfiles
90%

nl -nrz -w3 -v1 will display sequence number starting 001 002 etc following by file name.,Follow command rename all files to sequence and also lowercase extension:,I want to rename the files in a directory to sequential numbers. Based on creation date of the files., Stack Overflow Public questions & answers

Try to use a loop, let, and printf for the padding:

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
load more v
88%

Alternative version, solves issue with duplicate filenames by appending timestamp to each filename, and then enumerating them. This solution may take longer time, as number of files increases, but for directories that range in hundreds , this won't take long time,Here looping over all the files in the current directory and renaming sequentially based on order, if you want to deal with only the .png files, use for file in *.png instead. counter variable will keep track of the increments.,This version pushes index until it is found that filename with such index is not taken. Although filenames may start at different index upon successive iterations of the script, the files themselves remain unchanged., @DaveTweed can you provide an example ? running script twice wouldn't have issue with duplicates, because entries are sorted and enumerated each time. So if 1.png already exists, it would get sorted and renamed to 1.png again – Sergiy Kolodyazhnyy Aug 7 '16 at 0:14

Assuming you want to follow the shell globbing order while sorting files, you can do:

#!/bin/bash

counter = 0
for file in * ;
do
   [
      [-f $file]
   ] && echo mv - i "$file"
$((counter + 1)).png && ((counter++))
done

Example:

$ counter = 0;
for file in * ;
do [
   [-f $file]
] && echo mv - i "$file"
$((counter + 1)).png && ((counter++));
done
mv - i file.txt 1. png
mv - i foo.sh 2. png
mv - i bar.txt 3. png
load more v
72%

What is the most straightforward way from the command line, or from a script or macro to renumber all the files starting at a certain number? I have a standard Fedora Linux install using bash.,the On glob qualifier to sort matches in decreasing order (and n to use numerical order, in case the file names don't all have leading zeroes to the same width);, I don't think it is the exact issue, that's going renumber all the images every time. I just want to renumber the images from a particular point. – robertc Jun 11 '12 at 16:30 ,A script in Bash and many other languages can do this specific transform, but most of us would rather not have to write a program every time we want to rename a bunch of files.

I think that it should do the work:

#!/bin/bash

NEWFILE = $1

for file in `ls|sort -g -r`
do
   filename = $(basename "$file")
extension = $ {
   filename # # * .
}
filename = $ {
   filename % .*
}

if [$filename - ge $NEWFILE]
then
mv "$file"
"$(($filename + 1))".$extension
fi
done
load more v
65%

Browse to the folder with the files to rename.,To rename multiple files in bulk with the same name structure, use these steps:,To rename multiple files in bulk with the same name structure with Command Prompt, use these steps:,Browse to the folder with the files to change their names.

Type the following command example to navigate to the folder with the files to rename and press Enter:

cd PATH\TO\FOLDER

cd PATH\ TO\ FOLDER
load more v
75%

As you may already know, we use mv command to bulk rename or move files and directories in Linux and Unix-like operating systems. ,Method 4 - Rename multiple files at once using vimv,Method 1 - Batch rename files using mmv,Method 5 - Batch rename files using Emacs

To install mmv on Debian, Ubuntu, Linux Mint, and Pop OS, run the following command:

$ sudo apt - get install mmv
load more v
40%

Rename all file names in your directory using Python,Rename multiple files using Python,Read JSON file using Python,Create a directory in Python

Other "directory-files" queries related to "Rename all files in a directory with numbers"