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.
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.
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()
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
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.