328

I want to rename the files in a directory to sequential numbers. Based on creation date of the files.

For Example sadf.jpg to 0001.jpg, wrjr3.jpg to 0002.jpg and so on, the number of leading zeroes depending on the total amount of files (no need for extra zeroes if not needed).

4
  • I've been looking at stackoverflow.com/questions/880467/…, but I cannot get that to Work for me.
    – Gnutt
    Jul 9, 2010 at 10:16
  • 2
    Linux/Unix don't store a creation date. Jul 9, 2010 at 13:50
  • 1
    ls -1tr | rename -v 's/.*/our $i;if(!$i){$i=1;} sprintf("%04d.jpg", $i++)/e'
    – maXp
    Mar 17, 2018 at 22:41
  • @maXp. This will likely not handle special chars correctly. Also never use ls without --color=never. Dec 2, 2020 at 23:43

28 Answers 28

431

Beauty in one line:

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

You can change .ext with .png, .jpg, etc.

26
  • 4
    legend :), I used this on git shell on windows, works great! ^^.
    – Mr.K
    Feb 24, 2016 at 7:14
  • 4
    Works Great. Beauty in one line. I added ls -tr get the files by last modified time. May 24, 2016 at 7:20
  • 64
    add printf like this: ls | cat -n | while read n f; do mv "$f" `printf "%04d.extension" $n`; done to have zero padded numbers Nov 23, 2016 at 19:13
  • 11
    Warning: You might want to add -n to the mv command to prevent accidentally overwriting files. Discovered this the hard way! Nov 2, 2017 at 3:50
  • 12
    Putting together some great ideas from comments on this answer and others: ls -1prt | grep -v "/$" | cat -n | while read n f; do mv -n "${f}" "$(printf "%04d" $n).${f#*.}"; done Results: (1) sorted in order of modification, later files with later indexes; (2) ignores directories - and not recursive; (3) pads indexes; (4) maintains original extension.
    – Jorge
    May 22, 2018 at 22:25
409

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

using the -i flag prevents automatically overwriting existing files, and using -- prevents mv from interpreting filenames with dashes as options.

15
  • 8
    You can also do printf -v new "%04d.jpg" ${a} to put the value into the variable. And ((a++)) works to increment the variable. Also, this doesn't do it in creation date order or minimize the padding which are things the OP specified. However, it should be noted that Linux/Unix don't store a creation date. Jul 9, 2010 at 13:50
  • 5
    Needed to double quote wrap the mv for this to work in my bash environment. mv "${i}" "${new}" Jul 11, 2012 at 12:20
  • 3
    Could just be Cygwin (although it's terminal behaviour is largely identical to normal Unix shells) but it seems to have a problem when there are spaces in the filename.
    – Geesh_SO
    Aug 13, 2013 at 9:14
  • 3
    It would probably also be desirable to use mv -- "$i" "$new" to correctly handle source filenames that start with dashes; as it is, mv will try to parse such filenames as collections of flags. Dec 3, 2014 at 15:31
  • 3
    I have lost about 800 files at a glimpse. I think -ishould be included in the answer and note should be rewritten accordingly. that'll be more safe. :(
    – Midhun KM
    Apr 7, 2017 at 6:11
64

I like gauteh's solution for its simplicity, but it has an important drawback. When running on thousands of files, you can get "argument list too long" message (more on this), and second, the script can get really slow. In my case, running it on roughly 36.000 files, script moved approx. one item per second! I'm not really sure why this happens, but the rule I got from colleagues was "find is your friend".

find -name '*.jpg' | # find jpegs
gawk 'BEGIN{ a=1 }{ printf "mv %s %04d.jpg\n", $0, a++ }' | # build mv command
bash # run that command

To count items and build command, gawk was used. Note the main difference, though. By default find searches for files in current directory and its subdirectories, so be sure to limit the search on current directory only, if necessary (use man find to see how).

8
  • 7
    I modified to use row number NR for a shorter command. gawk '{ printf "mv %s %04d.jpg\n", $0, NR }' Dec 21, 2012 at 18:25
  • 15
    Massive security vulnerabilities here. Think about if you have a file named $(rm -rf /).jpg. Dec 3, 2014 at 15:25
  • 2
    breaks down in case filename has spaces. Use quotes around the source filename. printf "mv \"%s\" %04d.jpg\n", $0, a++
    – baskin
    Feb 21, 2015 at 9:04
  • 1
    The for loop is not taking 1 second per file, strictly speaking. It is taking a long time to fetch the 36,000 file names from the file system, then loops over them reasonably quickly. Many file systems have issues with large directories, regardless of which precise commands you run; but typically, find is going to be faster than a shell wildcard because it has to fetch and sort the list of matching files.
    – tripleee
    Jan 8, 2018 at 14:21
  • 2
    @Pero Your usage of mv is dangerous. Besides @CharlesDuffy's objections, what happens if there (before running your script) already is a file with name 0001.jpg, for example? It will be silently overwritten by the first file treated. Secondly, if a file happens to start with a dash, mv treats the rest of the file name as flags. Both problems have been mentioned in the comments to gauteh's answer, as well as their solution: Use mv -i -- instead of mv. Apart from that, I agree that using find is better than using a shell glob, for the reason you have pointed out.
    – Binarus
    Jan 6, 2019 at 9:12
48

A very simple bash one liner that keeps the original extensions, adds leading zeros, and also works in OSX:

num=0; for i in *; do mv "$i" "$(printf '%04d' $num).${i#*.}"; ((num++)); done

Simplified version of http://ubuntuforums.org/showthread.php?t=1355021

6
  • Exactly what I was looking for. I like that you can set the start index.
    – Jack Vial
    Apr 12, 2018 at 23:35
  • 5
    Please note this will not keep the original order of the files. Jun 16, 2018 at 1:17
  • @RoyShilkrot What do you mean by »will not keep the original order«? Globs in bash and other posix shells expand in sorted order according to the system's locale. Of course, this does not sort 9.ext before 11.ext, but other tools (like ls) won't do so either.
    – Socowi
    Jun 4, 2020 at 5:28
  • No, this is likely a dangerous solution not to mention the fact that special chars and spaces in file names will not work correctly. Dec 2, 2020 at 23:40
  • this is the perfect solution.. cross platform and keeps the extensions too.. well done!
    – supersan
    Jan 9, 2021 at 7:52
33

using Pero's solution on OSX required some modification. I used:

find . -name '*.jpg' \
| awk 'BEGIN{ a=0 }{ printf "mv \"%s\" %04d.jpg\n", $0, a++ }' \
| bash

note: the backslashes are there for line continuation

edit July 20, 2015: incorporated @klaustopher's feedback to quote the \"%s\" argument of the mv command in order to support filenames with spaces.

7
  • 5
    Like Pero's solution, there are security vulnerabilities here. Think about if you have a file named $(rm -rf /).jpg. Dec 3, 2014 at 15:25
  • Thanks, this works. But you should quote the first argument to mv. If you have a file named i.e. some thing.jpg your script fails. This is what I used: find . -name '*.jpg' | awk 'BEGIN{ a=0 }{ printf "mv \"%s"\ wallpaper-%04d.jpg\n", $0, a++ }' | bash Jul 18, 2015 at 13:25
  • i would probably use -maxdepth 1 to work only on jpgs in current directory, as it's better to be safe than sorry. Sep 28, 2016 at 20:24
  • If you move the pipe character to the end of the previous line, you don't need any backslashes.
    – tripleee
    Jan 8, 2018 at 14:22
  • you may use ls -1 *.jpg instead of find, else it won't be sorted. Or add a |sort| before awk
    – JPT
    Dec 8, 2019 at 18:49
32

with "rename" command

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

or

rename -N 0001 's/.*/$N.jpg/' *.jpg
10
  • 39
    My rename (Linux Mint 13) doesn't have a -N option.
    – Luke H
    Apr 21, 2014 at 22:47
  • I can upvote just for the mention of the rename command, I have never heard of it
    – d.k
    Oct 13, 2015 at 16:37
  • 9
    I'm interested in knowing where one gets a rename command with -N option. My Ubuntu 14.04 doesn't have it. Dec 1, 2015 at 21:19
  • 1
    I love the rename command, but I never knew about -N! Thanks!
    – Dan Lecocq
    Apr 3, 2017 at 23:39
  • 4
    There are at least three different tools known as rename. The link posted by @Bostrot is yet another rename tool and it seems like @RomanRhrnNesterov used that one. ¶ To all the users of rename by Peder Stray and Larry Wall (package perl-rename on arch linux and package rename on debian/ubuntu): You can define $N yourself: perl-rename '$N=sprintf("%04d",++$N); s/.*/$N.jpg/' *.jpg
    – Socowi
    Jun 3, 2020 at 20:51
14

To work in all situations, put a \" for files that have space in the name

find . -name '*.jpg' | gawk 'BEGIN{ a=1 }{ printf "mv \"%s\" %04d.jpg\n", $0, a++ }' | bash
2
  • 5
    Does not in fact cover all situations. A filename containing literal quotes could trivially escape -- and since you're using double quotes rather than single quotes, expansions like $(rm -rf /) are still honored. Dec 3, 2014 at 15:26
  • This solution does cover all: stackoverflow.com/a/65046206/1208218 Dec 2, 2020 at 23:29
14

On OSX, install the rename script from Homebrew:

brew install rename

Then you can do it really ridiculously easily:

rename -e 's/.*/$N.jpg/' *.jpg

Or to add a nice prefix:

rename -e 's/.*/photo-$N.jpg/' *.jpg
3
  • 6
    That's a great script, and it only depends on Perl. brew only works on macs, but you can grab it from github (github.com/ap/rename) and use it on Linux or Windows.
    – Tim Danner
    Feb 1, 2018 at 20:47
  • 4
    this doesn't work for me. I got Global symbol "$N" requires explicit package name (did you forget to declare "my $N"?) at (user-supplied code). Aug 12, 2018 at 21:13
  • 1
    As a Mac user, I'm happy to report that the rename application linked to by @TimDanner is super portable, it was as easy as downloading the Zip archive, dropping the rename utility into a directory that my bash profile path is set to see, and it works right out of the box. I'm surprised this isn't available on MacPorts. I find it so weird that I have a BSD manpage for Rename (2) but when run rename it would say no command exists. Go figure.
    – adamlogan
    Oct 2, 2019 at 1:57
11

NOTE The rename commands here include -n which previews the rename. To actually perform the renaming, remove the -n

If your rename doesn't support -N, you can do something like this:

ls -1 --color=never -c | xargs rename -n 's/.*/our $i; sprintf("%04d.jpg", $i++)/e'

NOTE The rename commands here includes -n which previews the rename. To actually perform the renaming, remove the -n

Edit To start with a given number, you can use the (somewhat ugly-looking) code below, just replace 123 with the number you want:

ls -1 --color=never  -c | xargs rename -n 's/.*/our $i; if(!$i) { $i=123; } sprintf("%04d.jpg", $i++)/e'

This lists files in order by creation time (newest first, add -r to ls to reverse sort), then sends this list of files to rename. Rename uses perl code in the regex to format and increment counter.

However, if you're dealing with JPEG images with EXIF information, I'd recommend exiftool

This is from the exiftool documentation, under "Renaming Examples"

   exiftool '-FileName<CreateDate' -d %Y%m%d_%H%M%S%%-c.%%e dir

   Rename all images in "dir" according to the "CreateDate" date and time, adding a copy number with leading '-' if the file already exists ("%-c"), and
   preserving the original file extension (%e).  Note the extra '%' necessary to escape the filename codes (%c and %e) in the date format string.
4
  • How can I start at a specific number using rename?
    – Jan
    Aug 18, 2014 at 0:14
  • Does it parse spaces, double quotes, backslashes and CR's in filenames correctly? Dec 4, 2020 at 7:18
  • When I ran the first option, it printed the new filename but never actually renamed the files to that. All I see is a bunch of lines like the following: ``` rename(20210520170654__5__88613416_141565274577465_5180391433346310275_n.mp4, 0000.mp4) rename(20210228193758__6__102160185_231347665376237_8972327157841210326_n.mp4, 0001.mp4) ``` Did you maybe forget to remove a "dry-run" option from the command?
    – Raleigh L.
    Jul 27, 2022 at 1:35
  • You are correct, this argument needs to be removed, I'll update the answer
    – Luke H
    Jul 27, 2022 at 23:58
6
find .  | grep 'avi' | nl -nrz -w3 -v1 | while read n f; do mv "$f" "$n.avi"; done

find . will display all file in folder and subfolders.

grep 'avi' will filter all files with avi extension.

nl -nrz -w3 -v1 will display sequence number starting 001 002 etc following by file name.

while read n f; do mv "$f" "$n.avi"; done will change file name to sequence numbers.

1
  • I used this to rename a tv show in absolute order. With printf zero padding this perfectly worked for me find . | grep 'mkv' | nl -nrz -w3 -v1 | while read n f; do mv "$f" `printf "Name.Year.E%03d.mkv" $n`; done. You can change the starting number on the nl part with the -v argument.
    – bachph
    Aug 1 at 14:09
4

Follow command rename all files to sequence and also lowercase extension:

rename --counter-format 000001 --lower-case --keep-extension --expr='$_ = "$N" if @EXT' *
2
  • Does this handle spaces and special chars like slashes etc. in filenames? Dec 2, 2020 at 23:41
  • What OS and version of rename are you running? None of those options exist on my version of rename and my OS, I get this error: ``` Unknown option: counter-format Unknown option: lower-case Unknown option: keep-extension Unknown option: expr Usage: rename [ -h|-m|-V ] [ -v ] [ -0 ] [ -n ] [ -f ] [ -d ] [ -u [enc]] [ -e|-E perlexpr]*|perlexpr [ files ] ``` I'm running Ubuntu 22 with version 1.3.0 of the rename tool
    – Raleigh L.
    Jul 27, 2022 at 1:33
2

Let us assume we have these files in a directory, listed in order of creation, the first being the oldest:

a.jpg
b.JPG
c.jpeg
d.tar.gz
e

then ls -1cr outputs exactly the list above. You can then use rename:

ls -1cr | xargs rename -n 's/^[^\.]*(\..*)?$/our $i; sprintf("%03d$1", $i++)/e'

which outputs

rename(a.jpg, 000.jpg)
rename(b.JPG, 001.JPG)
rename(c.jpeg, 002.jpeg)
rename(d.tar.gz, 003.tar.gz)
Use of uninitialized value $1 in concatenation (.) or string at (eval 4) line 1.
rename(e, 004)

The warning ”use of uninitialized value […]” is displayed for files without an extension; you can ignore it.

Remove -n from the rename command to actually apply the renaming.

This answer is inspired by Luke’s answer of April 2014. It ignores Gnutt’s requirement of setting the number of leading zeroes depending on the total amount of files.

2
  • This was the most useful answer here for me, mostly because I wanted to dry-run the op before it ran, and also because I could tune the order by changing to ls -1tU for creation time. It also needed rename installed, which I didn't have.
    – antonyh
    Feb 15, 2017 at 23:46
  • There are issues with this. Ref my comments on the top answer. Dec 2, 2020 at 23:30
2

I spent 3-4 hours developing this solution for an article on this: https://www.cloudsavvyit.com/8254/how-to-bulk-rename-files-to-numeric-file-names-in-linux/

if [ ! -r _e -a ! -r _c ]; then echo 'pdf' > _e; echo 1 > _c ;find . -name "*.$(cat _e)" -print0 | xargs -0 -t -I{} bash -c 'mv -n "{}" $(cat _c).$(cat _e);echo $[ $(cat _c) + 1 ] > _c'; rm -f _e _c; fi

This works for any type of filename (spaces, special chars) by using correct \0 escaping by both find and xargs, and you can set a start file naming offset by increasing echo 1 to any other number if you like.

Set extension at start (pdf in example here). It will also not overwrite any existing files.

2

Not related to creation date but numbered based on sorted names:

python3 -c \
'ext="jpg"
start_num=0
pad=4
import os,glob
files=glob.glob(f"*.{ext}")
files.sort()
renames=list(zip(files,range(start_num,len(files)+start_num)))
for r in renames:
  oname=r[0]
  nname=f"{r[1]:0{pad}}.{ext}"
  print(oname,"->",nname)
  os.rename(oname,nname)
'
0

I had a similar issue and wrote a shell script for that reason. I've decided to post it regardless that many good answers were already posted because I think it can be helpful for someone. Feel free to improve it!

numerate

@Gnutt The behavior you want can be achieved by typing the following:

./numerate.sh -d <path to directory> -o modtime -L 4 -b <startnumber> -r

If the option -r is left out the reaming will be only simulated (Should be helpful for testing).

The otion L describes the length of the target number (which will be filled with leading zeros) it is also possible to add a prefix/suffix with the options -p <prefix> -s <suffix>.

In case somebody wants the files to be sorted numerically before they get numbered, just remove the -o modtime option.

0
a=1

for i in *.jpg; do
 mv -- "$i" "$a.jpg"
 a=`expr $a + 1`
done
1
  • Does this handle spaces and special chars correctly? Dec 2, 2020 at 23:31
0

Again using Pero's solution with little modifying, because find will be traversing the directory tree in the order items are stored within the directory entries. This will (mostly) be consistent from run to run, on the same machine and will essentially be "file/directory creation order" if there have been no deletes.

However, in some case you need to get some logical order, say, by name, which is used in this example.

find -name '*.jpg' | sort -n | # find jpegs
gawk 'BEGIN{ a=1 }{ printf "mv %s %04d.jpg\n", $0, a++ }' | # build mv command
bash # run that command 
2
  • 4
    Do NOT use this. Redirecting to bash like this is a massive security risk. What if one of the filename is of the form blahblah; rm -r * blahblah.jpg?
    – xhienne
    Jan 8, 2017 at 12:29
  • No, this does not cover all special chars and spaces. See my comments on top answer. Dec 2, 2020 at 23:32
0

The majority of the other solutions will overwrite existing files already named as a number. This is particularly a problem if running the script, adding more files, and then running the script again.

This script renames existing numerical files first:

#!/usr/bin/perl

use strict;
use warnings;

use File::Temp qw/tempfile/;

my $dir = $ARGV[0]
    or die "Please specify directory as first argument";

opendir(my $dh, $dir) or die "can't opendir $dir: $!";

# First rename any files that are already numeric
while (my @files = grep { /^[0-9]+(\..*)?$/ } readdir($dh))
{
    for my $old (@files) {
        my $ext = $old =~ /(\.[^.]+)$/ ? $1 : '';
        my ($fh, $new) = tempfile(DIR => $dir, SUFFIX => $ext);
        close $fh;
        rename "$dir/$old", $new;
    }
}

rewinddir $dh;
my $i;
while (my $file = readdir($dh))
{
    next if $file =~ /\A\.\.?\z/;
    my $ext = $file =~ /(\.[^.]+)$/ ? $1 : '';
    rename "$dir/$file", sprintf("%s/%04d%s", $dir, ++$i, $ext); 
}
2
  • Does this handle spaces and special chars (slashes) etc. correctly? Dec 2, 2020 at 23:42
  • It should do, but please comment here again if not Dec 3, 2020 at 8:47
0

Sorted by time, limited to jpg, leading zeroes and a basename (in case you likely want one):

ls -t *.jpg | cat -n |                                           \
while read n f; do mv "$f" "$(printf thumb_%04d.jpg $n)"; done

(all on one line, without the \)

3
  • In the grand scheme of things, many answers on this page are horrible, so maybe this doesn't particularly deserve a downvote. I hesitate to upvote anything which uses ls in a script, though.
    – tripleee
    Jan 8, 2018 at 14:28
  • I save this answer from the negative side as while perhaps fragile, it worked for a specific use-case I had.
    – Ville
    Apr 20, 2019 at 7:15
  • There are issues with this. Ref my comments on the top rated answer. Dec 2, 2020 at 23:29
-1

This script will sort the files by creation date on Mac OS bash. I use it to mass rename videos. Just change the extension and the first part of the name.

ls -trU *.mp4| awk 'BEGIN{ a=0 }{ printf "mv %s lecture_%03d.mp4\n", $0, a++ }' | bash
2
  • 1
    Do NOT use this. Redirecting to bash like this is a massive security risk. What if one of the filename is of the form blahblah; rm -r * blahblah.mp4?
    – xhienne
    Jan 8, 2017 at 12:38
  • And it will likely not handle certain things like spaces and special chars nor ls colors. Dec 2, 2020 at 23:34
-1
ls -1tr | rename -vn 's/.*/our $i;if(!$i){$i=1;} sprintf("%04d.jpg", $i++)/e'

rename -vn - remove n for off test mode

{$i=1;} - control start number

"%04d.jpg" - control count zero 04 and set output extension .jpg

2
  • Upvoting this answer as it only uses the native rename. My use case was to replace the first two digits of files by numbers starting at 01: rename -v -n 's/../our $i;if(!$i){$i=1;} sprintf("%02d", $i++)/e' *
    – minterior
    Oct 12, 2018 at 6:36
  • No, special chars and spaces will not be handled correctly. Not to mention ls color output. Dec 2, 2020 at 23:33
-1

To me this combination of answers worked perfectly:

ls -v | gawk 'BEGIN{ a=1 }{ printf "mv %s %04d.jpg\n", $0, a++ }' | bash
  • ls -v helps with ordering 1 10 9 in correct: 1 9 10 order, avoiding filename extension problems with jpg JPG jpeg
  • gawk 'BEGIN{ a=1 }{ printf "mv %s %04d.jpg\n", $0, a++ }' renumbers with 4 characters and leading zeros. By avoiding mv I do not accidentally try to overwrite anything that is there already by accidentally having the same number.
  • bash executes

Be aware of what @xhienne said, piping unknown content to bash is a security risk. But this was not the case for me as I was using my scanned photos.

1
  • No, spaces and special chars will be an issue. Dec 2, 2020 at 23:33
-1

Here is what worked for me.
I Have used rename command so that if any file contains spaces in name of it then , mv command dont get confused between spaces and actual file.

Here i replaced spaces , ' ' in a file name with '_' for all jpg files
#! /bin/bash
rename 'y/ /_/' *jpg         #replacing spaces with _
let x=0;
for i in *.jpg;do
    let x=(x+1)
    mv $i $x.jpg
done
2
  • Install rename on linux (ubuntu) , "sudo apt install rename" Mar 19, 2020 at 5:53
  • This may handle spaces, but not sure about special chars. Dec 2, 2020 at 23:32
-1

Nowadays there is an option after you select multiple files for renaming (I have seen in thunar file manager).

  1. select multiple files
  2. check options
  3. select rename

A prompt comes with all files in that particular dir just check with the category section

-1

Using sed :

ls -tr | sed "s/(.*)/mv '\1' \=printf('%04s',line('.').jpg)/" > rename.sh
bash rename.sh

This way you can check the script before executing it to avoid big mistakes

-2

Here a another solution with "rename" command:

find -name 'access.log.*.gz' | sort -Vr | rename 's/(\d+)/$1+1/ge'
3
  • To restore leading zeros: rename 's/(\d+)/sprintf("%04d",$1)+1/ge' Mar 24, 2015 at 21:27
  • Doesn't work as expected, for a folder that has files with names like this : DSC_3451.JPG, DSC_3452.JPG etc. find -name '*.JPG' | sort -Vr | rename 's/(\d+)/$1+1/ge' modifies nothing
    – Tiberiu C.
    Jul 29, 2015 at 9:19
  • No, you need -print0 on the find, and use xargs with -0 to make this work correctly with special chars and spaces. Dec 2, 2020 at 23:35
-3

Pero's answer got me here :)

I wanted to rename files relative to time as the image viewers did not display images in time order.

ls -tr *.jpg | # list jpegs relative to time
gawk 'BEGIN{ a=1 }{ printf "mv %s %04d.jpg\n", $0, a++ }' | # build mv command
bash # run that command
1
  • 3
    Do NOT use this. Redirecting to bash like this is a massive security risk. What if one of the filename is of the form blahblah; rm -r * blahblah.jpg? Just an extreme example.
    – xhienne
    Jan 8, 2017 at 13:02
-4

To renumber 6000, files in one folder you could use the 'Rename' option of the ACDsee program.

For defining a prefix use this format: ####"*"

Then set the start number and press Rename and the program will rename all 6000 files with sequential numbers.

1
  • 1
    The OP asks for a set of commands in Unix Bourne Shell. You are proposing a commercial product that only runs in Windows environment.
    – xhienne
    Jan 8, 2017 at 12:45

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