Used to rename TV show episodes. Original files had episode number spanning the entire run of the show. Wanted to break it into season and episode numbers.

#!/bin/sh
#
# Input is list of files
# Argument is Season number
# Pipe the output to /bin/sh
#
awk '{mine=$0 ; sub(/e([0-9]+)/,"s0"SEASON"e"sprintf("%.2d",NR),mine); print "mv \"" $0 "\" \"" mine "\"" }' SEASON=$1

Preconditions

  • Each season in a separate directory
  • Each file has e[0-9]+ as episode identifier

Script

  • For each file in the directory (in timestamp order)
  • Replace episode number with the line number (with a leading zero if necessary)
  • Print "mv ", the old name, and the new name
  • Pipe it to /bin/sh