Skip to content
Switch branches/tags

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time
Automatically sorts TV show episodes into an organised directory structure. 
	dir structure:[show]/[season x]/[eps]

Sorts and moves episodes and complete season directories.

Tested on Linux and Windows

- Optional integration with xbmc (notification and library update).
- Can optionally move non-episodes into a separate directory. 
- Can also rename episodes.
- Options for renaming, including custom naming formats and fetching episode names from
- Sort by move, copy, or symlink.
- Many other options (see below).

This provides the magic required to have your home theatre PC (HTPC) keep itself sorted out. 
You can combine this script with an automatic episode downloader (there are many available*), 
and this script can sort all of your new episodes, renaming them appropriately if you like.
Then, if you use xbmc as a front end, it can tell xbmc to check for the new episodes and 
display a message to anyone viewing, that new episodes are available.

All you will have to do is sit back and enjoy.

* Episodes of many openly licenced shows are available, and episodes can be downloaded using 
RSS feeds, torrent downloaders, file hosting site downloaders etc.

Copyright 2010 Z. Cliffe Schreuders
z.cliffe {at}

Get SortTV here:

Please goto the xbmc forum to discuss SortTV:

Paypal donations:

This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.


You need to install Perl and then save wherever. Eg: on Linux into /home/[yourusername]/sorttv/

Edit the sorttv.conf file to the settings you want (at minimum specify the location of the directories to sort from and to)

Install the modules the script uses.
To do so, run this command on Linux:
sudo cpan File::Copy::Recursive File::Glob LWP::Simple TVDB::API Text::Capitalize
On Windows run the same command, without "sudo"


If you want to make it sort new episodes every hour:

On Linux: 
  in a console type: crontab -e
  on a new line enter "5 * * * * " followed by the command to run the script.
  This will run the script 5 mins past every hour.
  5 * * * * nice perl /home/cliffe/sorttv/

  Every hour this would sort new episodes according to the configuration in /home/cliffe/sorttv/sorttv.conf

On Windows:
  Create a .bat file with the command and use scheduler.
  Google is your friend. 

USAGE: [OPTIONS] [directory-to-sort directory-to-sort-into]

By default SortTV tries to read the configuration from sorttv.conf
	(an example config file is available online)

The simplest way to use SortTV is to edit the example config file, then just run "perl" to do the sorting

You can overwrite any config options with commandline arguments, which match the format of the config file (except that each argument starts with "--")

		A directory containing files to sort
		For example, set this to where completed downloads are stored

		Where to sort episodes into (dir that will contain dirs for each show)
		This directory will contain the structure (Show)/(Seasons)/(episodes)
		Alternatively set this to "KEEP_IN_SAME_DIRECTORIES" for a recursive renaming of files in directory-to-sort

		Where to sort music into
		If not specified, music is not moved

		Where to put things that are not episodes
		If this is supplied then files and directories that SortTV does not believe are episodes will be moved here
		If not specified, non-episodes are not moved

		Only copy if the file matches one of these patterns
		Uses shell-like simple pattern matches (eg *.avi)
		This argument can be repeated to add more rules

		Don't copy if the file matches one of these patterns
		Uses shell-like simple pattern matches (eg *.avi)
		This argument can be repeated to add more rules

		Only copy files which fall within these filesize ranges.
		Examples for the pattern include 345MB-355MB or 1.05GB-1.15GB

		Sort only files or directories that are older than this number of days. 
		If not specified or zero, sorts everything.

		host:port for xbmc webserver, to automatically update library when new episodes arrive
		Remember to enable the webserver within xbmc, and "set the content" of your TV directory in xbmc.
		If not specified, xbmc is not updated

		Log to this file
		If not specified, output only goes to stdout (the screen)

		Output verbosity. Set to TRUE to show messages describing the decision making process.
		If not specified, FALSE

		Secondary config file, overwrites settings loaded so far
		If not specified, only the default config file is loaded (sorttv.conf)

		Fetch show titles from (for proper formatting)
		If not specified, TRUE

		Rename episodes to "show name S01E01.ext" format when moving
		If not specified, FALSE

		the format to use if renaming to a new format (as specified above)
		Hint: including the Episode Title as part of the name slows the process down a bit since titles are retrieved from
		The formatstring can be made up of:
			[SHOW_NAME]: "My Show"
			[EP1]: "S01E01"
			[EP2]: "1x1"
			[EP3]: "1x01"
			[EP_NAME1] " - Episode Title"
			[EP_NAME2] ".Episode Title"
		If not specified the format is, "[SHOW_NAME] - [EP1][EP_NAME1]"
		For example:
			for "My Show S01E01 - Episode Title" (this is the default)
			--rename-format:[SHOW_NAME] - [EP1][EP_NAME1]
			for "My Show.S01E01.Episode Title"

		Renames episodes to replace spaces with dots
		If not specified, FALSE

		Season title
		Note: if you want a space it needs to be included
		(eg "Season " -> "Season 1",  "Series "->"Series 1", "Season."->"Season.1")
		If not specified, "Season "

		Season format padded to double digits (eg "Season 01" rather than "Season 1")
		If not specified, FALSE

		Match type. 
		LIBERAL assumes all files are episodes and tries to extract season and episode number any way possible.
		If not specified, NORMAL

		Attempt to sort files that are named after the episode title or air date.
		For example, "My show - My episode title.avi" or "My show - 2010-12-12.avi"
		 could become "My Show - S01E01 - My episode title.avi"
		Attempts to lookup the season and episode number based on the episodes in database.
		Since this involves downloading the list of episodes from the Internet, this will cause a slower sort.
		If not specified, TRUE

		Sort by moving or copying the file. If the file already exists because it was already copied it is silently skipped.
		The MOVE-AND-LEAVE-SYMLINK-BEHIND option may be handy if you want to continue to seed after sorting, this leaves a symlink in place of the newly moved file.
		PLACE-SYMLINK does not move the original file, but places a symlink in the sort-to directory (probably not what you want).
		If not specified, MOVE

		How to treat directories. 
		AS_FILES_TO_SORT - sorts directories, moving entire directories that represents an episode, also detects and moves directories of entire seasons
		RECURSIVELY_SORT_CONTENTS - doesn't move directories, just their contents, including subdirectories
		IGNORE - ignores directories

		Only sort into show directories that already exist
		This may be helpful if you have multiple destination directories. Just set up all the other details in the conf file, 
		and specify the destination directory when invoking the script. Only episodes that match existing directories in the destination will be moved.
		If this is false, then new directories are created for shows that dont have a directory.
		If not specified, FALSE

		Deletes symlinks from the directory to sort while sorting.
		This may be helpful if you want to remove all the symlinks you previously left behind using --sort-by:MOVE-AND-LEAVE-SYMLINK-BEHIND
		You could schedule "perl --remove-symlinks:TRUE" to remove these once a week/month
		If this option is enabled and used at the same time as --sort-by:MOVE-AND-LEAVE-SYMLINK-BEHIND, 
		 then only the previous links will be removed, and new ones may also be created
		If not specified, FALSE

		Substitutes files equal to NAME1 for NAME2
		This argument can be repeated to add multiple rules for substitution

		Use these IDs to lookup these shows
		This argument can be repeated to add multiple rules for substitution

		Define additional extensions for music files (SortTV knows a lot already)
		This argument can be repeated to add multiple additional extensions

		Forces MSWindows compatible file names, even when run on other platforms such as Linux
		This may be helpful if you are writing to a Windows share from a Linux system
		If not specified, TRUE

		Set language for thetvdb lookups, this effects episode titles etc
		Valid values include: it, zh, es, hu, nl, pl, sl, da, de, el, he, sv, eng, fi, no, fr, ru, cs, en, ja, hr, tr, ko, pt
		If not specified, en (English)

		Should non-episode files loose their directory structure?
		This option only has an effect if a non-episode directory was specified.
		If set to TRUE, they will be renamed after directory they were in.
		Otherwise they keep their directory structure in the new non-episode-directory location.
		If not specified, FALSE

		Download images for shows, seasons, and episodes from thetvdb
		Downloaded images are copied into the sort-to (destination) directory.
		NEW_SHOWS - When new shows, seasons, or episodes are created the associated images are downloaded
		FALSE - No images are downloaded
		if not specified, NEW_SHOWS
		Sets the image format to use, poster or banner.
		if not specified, POSTER

		What to do if a file already exists in the destination
		If not specified, SKIP

		Extracts the contents of archives (.zip, .rar) into the directory-to-sort while sorting
		If "rar" and "unzip" programs are available they are used.
		If not specified, TRUE

		Disables all the network enabled features such as:
			Disables notifying xbmc
			Disables tvdb title formatting
			Disables fetching images
			Disables looking up files named "Show - EpTitle.ext"
			Changes rename format (if applicable) to not include episode titles

Does a sort, as configured in sorttv.conf:

The directory-to-sort and directory-to-sort-to can be supplied directly:
To sort a Downloads directory contents into a TV directory
	perl /home/me/Downloads /home/me/Videos/TV
	perl --directory-to-sort:/home/me/Downloads --directory-to-sort-into:/home/me/Videos/TV

To move non-episode files in a separate directory:
	perl --directory-to-sort:/home/me/Downloads --directory-to-sort-into:/home/me/Videos/TV --non-episode-dir:/home/me/Videos/Non-episodes

To integrate with xbmc (notification and automatic library update):
	perl --directory-to-sort:/home/me/Downloads --directory-to-sort-into:/home/me/Videos/TV --xbmc-webserver:localhost:8080

And so on...


Paypal donations welcome:

If you find this script helpful then please consider making a $5 paypal donation :)


fork of sorttv from cliffe



No releases published


No packages published