Mac OS X/Linux: Batch rename files prefix directory name

Posted: May 28, 2013 in Apple, Linux
Tags: , ,

Update: Recommend NameChanger in Mac OS X

Plex media server only recognizes TV Shows when the video file name contains the show name. All my old Japanese Anime series are named by sequence number, so that they cannot be found by Plex media server. Here is the plex media server naming guide: http://wiki.plexapp.com/index.php/Media_Naming_and_Organization_Guide#Multiple_Episodes_in_One_File

To rename all the video files by prefixing the directory name, I wrote the following shell script.

#!/bin/sh

DIR=$1
#echo $DIR
cd "$DIR"

#get the current directory name without the full path
DIRNAME=${PWD##*/}

#echo $DIRNAME

for f in * ;
do
    #echo $f ;
    mv "$f" "$DIRNAME - $f" ;
done

To run it, open the Terminal in Mac OS X, save the above script in a <you_choose_a_name>.sh file.

This script take the command line argument as parameter, so that be careful when your run it.

Example: sh <you_choose_a_name>.sh /Users/XJ/TV\ Series/Slam\ Dunk

By running this command, all the files in /Users/XJ/TV\ Series/Slam\ Dunk will be renamed and folder name, i.e., Slam Dunk is prefixed.

If the directory name contains space, you need escape it with \

Advertisements
Comments
  1. […] Mac OS X/Linux: Batch rename files prefix directory name […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s