I have a folder containing like 1500 files, I want to rename them automatically via the terminal or a shell script as follows: "prefix_number.extension".
Example: cin_1.jpg, cin_2.png ...
I have a folder containing like 1500 files, I want to rename them automatically via the terminal or a shell script as follows: "prefix_number.extension".
Example: cin_1.jpg, cin_2.png ...
Try:
Or, if you prefer your commands spread over multiple lines:
How it works
count=0
This initializes the variable
count
to zero.for f in *; do
This starts a loop over all files in the current directory.
[ -f "$f" ] && mv -i "$f" "cin_$((++count)).${f##*.}"
[ -f "$f" ]
tests to see if the file$f
is a regular file (not a directory).If
$f
is a regular file, then the move (mv
) command is run.-i
tellsmv
not to overwrite any existing files without asking."cin_$((++count)).${f##*.}"
is the name of the new file.cin_
is the prefix.$((++count))
returns the value ofcount
after it has been incremented.${f##*.}
is the extension of file$f
.done
This marks the end of the loop.
Example
Consider a directory with these three files:
Let's run our command:
After running our command, the files in the directory now are:
A
rename
oneliner:This matches every file with a dot in its name (the last one, if multiple) and renames the part until the dot with
cin_
followed by an incrementing number and a dot. With the-n
flag it just prints what it would do, remove it to perform the renaming.Example run
Source: How to rename multiple files sequentially from command line?