Example Bash script to rename directories

By kenglish

This script will rename directories based on a pattern. My goal was to rename my directories so the album year would be in [] and not (). For example, we would move /home/Music/ACDC/Back in Black (1980) to /home/Music/ACDC/Back in Black [1980]. The downside is you have to enter the pattern twice, once for bash and once for sed.

#!/bin/bash
 
find  /home/kenglish/Music -type d | while read DIR; do
 
  if [[ "$DIR" =~ \([0-9]{4}\)$ ]]; then
    NEW_DIR=`echo $DIR | sed 's/(\([0-9][0-9][0-9][0-9]\))$/[\1]/'`
    echo "$DIR    -->    $NEW_DIR" 
    ` mv "$DIR" "$NEW_DIR" `
  fi
done


categoriaLinux commentoNo Comments dataNovember 17th, 2009

About... kenglish

This author published 69 posts in this site.

Share

FacebookTwitterEmailWindows LiveTechnoratiDeliciousDiggStumbleponMyspaceLikedin

Leave a comment