I have an Enigma2 FreeSat recorder that I've now hooked up to my Plex Media Server.

Plex can see and play the files from the Enigma2 just fine, but the file naming makes this unattractive.

How can I rename files of this format:

yyyymmdd nnnn - channel - title.* e.g. 20181128 2100 - BBC One HD - The Apprentice.*


title - dd-mm-yyyy - channel.* e.g. The Apprentice - 28-11-2018 - BBC One HD.*

(in such a way I can run this every few minutes from the command line).

I want to be sure that it only matches files in the first format so it doesn't try to rename files already renamed.

Later I'll want to have this running as a docker container.

2 Answers 2


I think the following shellscript will do it.

It will check that the date and time of the original file name are numbers, and if they are, will rearrange the name to the preferred format.

Otherwise the file will be skipped, so files that are already modified will not be touched.

Change directory to where you have your files and run the script.



for i in *.*
# echo "${str} -----"
  if [[ $date =~ $re ]] && [[ $time =~ $re ]]
   channel=${i#*- }
   channel=${channel% -*}
   title=${i##*- }
   mv -nv "$i" "$title - $date - $channel.$ext"

You can use this as a starting starting point:


regexp='^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9] [0-9][0-9][0-9][0-9] -'
while read l; do
  [[ ! ${l} =~ ${regexp} ]] && continue
  yyyymmdd=${l%% *}
  channel=$(echo ${l} | awk -F- '{print $2}' | sed -e 's!^[[:space:]]*!!' -e 's![[:space:]]*$!!')
  title=$(echo ${l} | awk -F- '{print $NF}' | sed -e 's!^[[:space:]]*!!' -e 's![[:space:]]*$!!')
  echo "${title} - ${yyyymmdd} - ${channel}.${ext}"

exit 0

This sets your regular expression to "yyyymmdd nnnn -" so it can filter out anything that does not match this string. Then for every line read in (which could be the output from find(1), ls(1), etc), it checks to see if that regular expression is matched. If so, it skips to the next line.

The next set of variable assignments are getting the various components you want. The ext assignment is getting the file extension. The yyyymmdd is getting the year, month and day. It is then reformatting by using variable substrings.

Since channel and title are delimited by a dash, I thought it would be easier to use awk(1) to get the appropriate field. The sed afterwards is to trim any spaces before and after the string. The last title is to remove the file extension.

Once have all of the variables, print out the line. This could easily be replaced with

mv ${l} "${title} - ${yyyymmdd} - ${channel}.${ext}"

to rename the file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.