How to Rename Files in Bash to Increase Number in Name

How to rename files in bash to increase number in name?

If you can get hold of the Perl-flavoured version of rename, that is simple like this:

rename -n 's/(\d+)/$1 + 100/e' *fasta

Sample Output

'Ciprianus_maximus_11_fred.fasta' would be renamed to 'Ciprianus_maximus_111_fred.fasta'
'Ciprianus_maximus_300_fred.fasta' would be renamed to 'Ciprianus_maximus_400_fred.fasta'
'Ciprianus_maximus_3900_fred.fasta' would be renamed to 'Ciprianus_maximus_4000_fred.fasta'

If you can't read Perl, that says... "Do a single substitution as follows. Wherever you see a bunch of digits next to each other in a row (\d+), remember them (because I put that in parentheses), and then replace them with the evaluated expression of that bunch of digits ($1) plus 100.".

Remove the -n if the dry-run looks correct. The only "tricky part" is the use of e at the end of the substitution which means evaluate the expression in the substitution - or I call it a "calculated replacement".

Bash script to rename files and increment number in the file name by N

This should work

filename=$1 #Filename
filenumber=$2 #file number from where you want to rename.
count=$3 #number of files
lastfile=$4 #Last filenumber in the folder


for ((i=lastfile; i>=$filenumber; i--))
do
mv $filename$i $filename`expr $i + $count`
done

You can run it like below

./rename wizard 15 3 30

Renaming files in a folder to sequential numbers

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.

Rename files with incrementing number starting at certain number

This should work:

for f in *.ABC; do
num=$(basename "$f" .ABC)
num2=$(printf "%05d" $((num + 55)))
echo mv "$f" "$num2.ABC"
done

Notes:

  • I am assuming your files all follow the pattern of digits followed by .ABC, so that basename $f .ABC extracts the number. You'll have to adjust the num= line if this assumption does not hold.

  • $(( ... )) is the bash syntax to do arithmetic operations.

  • (printf "%05d" ...) is here to pad zeroes in front of the number, otherwise you'd just get 55 etc.

  • Remove the echo once you're convinced it's doing the right thing.

  • EDIT Warning: the new set of filenames should not overlap the old set of filenames, or you might lose some files. Carefully choosing the order the mv commands are run could solve the issue, but putting the files in a new directory as they are renamed and moving them back after would be much simpler.

EDIT:

@MarkSetchell pointed out in the comments that you want sequential numbering that is not based on the original file names, in which case this loop would work instead:

i=55 # choose your starting destination file number here
for f in *.ABC; do
dest=$(printf "%05d" $i).ABC
echo mv "$f" "$dest"
i=$((i + 1))
done

bash to batch rename files with adding numbers

Use $((expression)) for arithmetic expansion in bash shell

n=0;
for file in *.jpg ; do mv "${file}" basename"${n}".jpg; n=$((n+1)); done

How to rename files with sequential numbers with bash?

This is fairly simple with a for loop in bash.

You can loop over all files in the directory by using output of ls command to for command. You need a variable which stores the index and you can update it in the loop.

export i=1
for f in $(ls)
do
# Copy file to new location, safer than move in case you make a mistake
cp $f ../new_directory/$i"_"$f -v

# Increment
let i=i+1
# Reset if exceeds 12
if [ $i -gt 12 ]
then
export i=1
fi
done

You will have to be in the files directory, and will have to create new_directory. Best way to run is to save this as a script, and execute.

Increase file name number by shell or linux command

You can use Perl rename like this to do an "evaluated substitution" - that's the e right at the end:

rename --dry-run 's|(\d+)|sprintf("%05d",$1+5674)|e' pub*jpg

Sample Output

'public_00000.jpg' would be renamed to 'public_05674.jpg'
'public_00001.jpg' would be renamed to 'public_05675.jpg'

In case you are unfamiliar with Perl, the command basically says:

rename "substitute|THIS|with THAT|" IN_THESE_FILENAMES

In your case, THIS is \d+ which means "one or more digits" and that is enclosed within parentheses to make a "capture group". That group can then be referred to in the substitution on the right side by $1 since it is the first capture group.

The THAT in your case is simply a print statement that prints the first capture group $1 incremented by 5674 in a field that is zero-padded to be 5 digits wide using %05d.


Using Perl rename has the benefits that:

  • you can do a "dry run" to see what it would do without actually doing anything
  • it will not clobber (overwrite) files without warning
  • it is fast - it doesn't create a process for sed and another for mv for every single file, it just starts a single Perl interpreter and makes a library call to rename each file
  • it will automagically create any intermediate directories needed, if you wish
  • you can use the full power of Perl to do as much substitution or calculation as you wish

Note for macOS users... Perl is installed on macOS by default, so if you use homebrew to install your packages, you just need:

brew install rename

Note for Linux users... there are several rename packages, the one I am referring to is sometimes called prename, or "Perl rename". That means, if you run file on the rename command, it should say it's a Perl script like this:

file $(which rename)
/usr/local/bin/rename: Perl script text executable

Incrementing number in filenames in bash

You need to rename the files in reverse.

11 -> 12
10 -> 11
9 -> 10

and so on.

Increase Number in Multiple Filenames Bash

You can use:

for f in picture*.mp4; do
n="${f/picture}"
n="${n%.*}"; ((n+=3))
echo mv "$f" "picture$n.mp4";
done
mv picture20.mp4 picture23.mp4
mv picture21.mp4 picture24.mp4

When you're satisfied just remove echo from above command.



Related Topics



Leave a reply



Submit