I have a directory which has files with random names. What I want to do is rename the files with file1, file2 and so on.The lexicographically smaller file name should be numbered with smaller number. How can I do that?
A simple bash script should do it.
To help with a line-by-line explanation:
loop, loop, loop
You can use this python script:
I'm also using the shell optional behavior
Observe that the numbering will not have any leading zeros, so that you'll get files named so:
and this might screw up the order of the files in listings. If you need leading zeros too:
And if you want it to keep the original file's extension, replace the line
If you read down to here, I guess you'll be able to craft something that matches your very own need from the examples I gave you, hopefully using good bash practice (provided you choose to solve your problem using bash of course).
I wanted to implement the same when I was a student and was studying the Linux Shell Scripts. then I created the below script.. Have a look.. https://github.com/rushyang/GTU-OS-Bash-Scripts/blob/master/Working/System%20Scripts/final_rename_updated.sh