Regular Expressions for Renaming get_iplayer Downloads

Here are a few useful commands for trimming excess information from the end of files downloaded by get_iplayer.

The commands make use of 3 standard utilities that should be included on any linux distribution. They are find, rename and sed.

You will need to have some understanding of regular expressions to work out how the files are renamed.

Find is used first to provide a coarse list of candidate files to work on. I’m merely using it to recursively list all files in a directory but you can easily fine-tune it with the -name option to filter by filename or file extension. If you don’t want to recurse through directories then you can either skip using find (just using rename) or add -maxdepth 1 as the first argument to find.

Find then passes each matching file it finds to rename which renames the file according to the sed expression it takes as an argument.

To make this a bit safer for anyone reading it, I’ve included the -n flag in the rename command. This modifies the behaviour of rename to not rename anything! Instead it prints out what it would have renamed had the -n “no action” flag not been present. This way, you get to preview all changes before unleashing what may well be a very large and irreversible batch renaming operation.

If you’re at all worried

  • Preview each batch operation with the -n flag for rename
  • Create a backup of any files in the path passed to find
  • Don’t put the backup where find can find it!

Remove iPlayer PIDs from the Filename




find /mnt/media -type f -execdir rename -n 's/_[0-9A-Za-z]{8}_default//' {} ;

Add Leading Zeroes to Episode Numbers




find /mnt/media/ -type f -execdir rename -n 's/Episode_([0-9]){1}([._]){1}/Episode_0$1$2/' {} ;

Remove Spaces From Filenames

Finally, another useful operation (to use on torrent downloads, for example, as iPlayer downloads do not have any) is to remove all spaces from filenames, replacing them with underscores. It affects only the filename, leaving any directories with spaces in the path to the file unchanged. Note the use of of ‘g’ at the end of the sed expression (e.g. ‘s/foo/bar/g’) to replace all occurrences of the pattern, not just the first it finds.

find /mnt/media -type f -execdir rename -n 's/ /_/g' {} ;

Leave a Reply

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

You are commenting using your 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 )

Connecting to %s