0

Let's assume we have following files.

00043.jpg

00086.jpg

00123.jpg

...

04523.jpg

What I want is, change their names to as following.

00001.jpg

00002.jpg

00003.jpg

so on

How can I do this on linux console? Or I can use python scripts.

Jun
  • 135
  • 1
  • 7
  • Possible duplicate of Renaming multiple files in a directory using Python ... https://stackoverflow.com/questions/37467561/renaming-multiple-files-in-a-directory-using-python – Abhishek Gautam Dec 17 '18 at 06:16
  • This is quite basic stuff and you can find tutorials to do this with google (and even stackoverflow). If you find a way and that does not work fr you, please post the code you have tried and what errors you get. – Juho Rutila Dec 17 '18 at 06:41
  • @Jun I have posted my answer below please try it! :) – Abhishek Gautam Dec 17 '18 at 07:32

3 Answers3

1

Supply the valid folder path and Run this code.

import os 

# Function to rename multiple files 
def main(): 
    i = 0
    path = 'E:\\files\\'    
    for filename in os.listdir("E:\\files"): 
        dst = str(i).zfill(4) + ".jpg"
        src = path + filename 
        dst = path + dst 

        # rename() function will 
        # rename all the files 
        os.rename(src, dst) 
        i += 1

# Driver Code 
if __name__ == '__main__': 

    # Calling main() function 
    main() 
Abhishek Gautam
  • 1,617
  • 3
  • 18
  • 29
  • How can you be certain that the order returned from `listdir` would match the order expected by the OP? Did you poach this code directly from Geeks for Geeks without checking it first? – Tim Biegeleisen Dec 17 '18 at 06:22
  • I don't want to Plagiarize the answer, I just gave direct reference with link. May be it could help in someway! – Abhishek Gautam Dec 17 '18 at 06:28
0

If you have PHP in your console,

<?php
$images = glob("*.jpg");

$start=0;
function squencer()
{
    global $start;
    ++$start;

    return str_pad($start, 5, "0", STR_PAD_LEFT);
}

foreach($images as $image)
{
    $newname = squencer().".jpg";
    rename($image, $newname);
}

Then run: php -f rename.php

Bimal Poudel
  • 1,214
  • 2
  • 18
  • 41
0

The only standard command for renaming files is mv and it has very limited capability. What you want to do inherently requires a script because the complexity of sequential naming is beyond the capability of globbing or even regular expressions. You have not specified whether there are other files in the directory or whether you care about the original order. Unlike Windows, Linux directory contents are not in any predictable order. I assume that you do want to maintain the original order. Your example indicates 0-padding. This is an important detail. It means that a lexical sort matches what we intuitively think, e.g. that 43 > 42. Lexically, 43 > 42 but 42 > 043. I'm going to show you how to do this using Python, assuming that the directory contains other files and that you want to maintain the original order. The first line is a shebang, telling the OS to use python3. This is particularly for Ubuntu but most systems will need this or a variation of it because they use python 2 by default. Also, although Python doesn't care what kind of line endings are in the script, the OS itself reads the shebang and it requires that these be Unix.

#!/usr/bin/env python3
import os
jpgfiles = [f for f in os.listdir() if os.path.splitext(f)[1].casefold() == jpg']
jpgfiles.sort()
for i,f in enumerate(jpgfiles) :
    os.rename(f, '%04.d' % i + '.jpg')

Python's inherent power enables a simple script like this to perform rather complex operations. But you don't want to have to write a script for every kind of file renaming you might want to do. When I couldn't find an existing program for the variety of renaming that I do, I wrote a Python program that can do what you want and much more. It is rene.py. You can get it at Sourceforge https://rene-file-renamer.sourceforge.io. It is free and open source (GNU GPL v3.0). The command to do what you want is rene *.jpg :.jpg I///5.