0

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.*

To:

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

1

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.

#!/bin/bash

re='^[0-9]+$'

for i in *.*
do
 str="$i"
# echo "${str} -----"
 date=${str:0:8}
 time=${str:9:4}
  if [[ $date =~ $re ]] && [[ $time =~ $re ]]
  then
   channel=${i#*- }
   channel=${channel% -*}
   title=${i##*- }
   ext=${title##*.}
   title=${title%%.*}
   date=${date:6:2}-${date:4:2}-${date:0:4}
   mv -nv "$i" "$title - $date - $channel.$ext"
  fi
done
1

You can use this as a starting starting point:

#!/bin/bash

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
  ext=${l##*.}
  yyyymmdd=${l%% *}
  yyyymmdd="${yyyymmdd:6:2}-${yyyymmdd:4:2}-${yyyymmdd:0:4}"
  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:]]*$!!')
  title=${title%.*}
  echo "${title} - ${yyyymmdd} - ${channel}.${ext}"
done

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .