4

I have a good amount of files I have to rename. My files are called sample_1.wav sample_2.wav ...

I need to rename each one of them with a smaller number in the name, sample_1.wav has to become sample_0.wav and so on.

I have tried this script, but it does not maintain the order:

#!/bin/bash

count=0

for file in *.wav
do
    new=$(printf "sample_%d.wav" "$count")
    mv -- "$file" "$new"
    (( count++ ))
done

Thank you for the help

Grimdrem
  • 233

2 Answers2

4

I think I did what you were asking using Python:

#!/usr/bin/env python

import os

for i in range(0, 30):  # up to the highest number of your filenames
    os.system("mv sample_%i.wav sample_%i.wav" % (i+1, i)) 

This reduces the number after the underscore by 1 for each of the files. Just make sure that you enter the largest number of your files in the second entry of range.

Mart
  • 56
4

While Python or Perl will be faster, you can implement the same idea as @Mart's answer in the shell:

for i in {1..100}; do mv sample_$i.wav sample_$((i-1)).wav; done
terdon
  • 100,812