PLEX Media Server and Bash commands for file organizing

Introduction

This weekend, I set up a PLEX Media Server to be able to share my legally acquired Linux ISO Collection to some friends and family. It would also let me access my files via the web and smartphone. I had previously purchased a lifetime Plex Pass during one of the sales, so I already had access to the Android App and an account set up. I had tried seting up PLEX in a container, but ran into some problems mapping the file shares.

Preparation

To prepare for PLEX to be able to index the files properly, I went through the entire file collection and renamed files into a PLEX friendly format. PLEX has a few knowledge base articles on renaming files, if you need them:

While organizing files, I found the following commands very helpful.

Renaming files to match the folder name

Frequently when downloading Linux ISOs, you can get a dummy filename, where the folder name is correct but the filename is garbage. Some applications like sabnzbd have ways of correcting this during download, but it doesn’t always work. A quick way to rename all files in folders to match the folder name, but keep their file extension, is to run the following command:

rename 's/(.*)\/.*\./$1\/$1./' */*

Moving all files in subfolders into current folder

Once you’ve renamed all your Linux ISOs to proper names, you’ll want to get them out of the folders, delete any extraneous files, and then move them into properly named folders. To move all of the files in subfolders into the current folder, run the following command:

find ./ -type f -exec mv {} . \;

Renaming part of files in current folder to a standardized format

While organizing files to the standard PLEX needs as per this article, I found I frequently had to fix formatting. This seems to be a common question on the /r/plex subreddit. The rename command was very helpful, as you could use regular expressions to change part of all filenames. PLEX mentions Linux ISOs should have the notation sXXeXX. If your Linux ISO has a different notation, you can use a command such as this to change it:

rename 's/1x/S01E/' *

Mounting shares on the PLEX virtual machine

I created a generously resourced virtual machine in VMware vSphere, and used a Debian 11 Bullseye template I had created using Packer. See this article for more details on how that was done, and this GitHub link for instructions. Once I had the static networking, hostname, date/time, and some minor packages such as smbclient all installed and set up, I set up the mappings for the network file shares. First I created the folders in the /media directory using the mkdir command. Then I edited the /etc/fstab file and added the following lines for the network shares:

//192.168.x.x/NAS/Media/ISO1 /media/ISO1 cifs defaults,credentials=/root/.smbcredentials,file_mode=0777,dir_mode=0777,uid=1000,gid=1000 0 0

//192.168.x.x/NAS/Media/ISO2 /media/ISO2 cifs defaults,credentials=/root/.smbcredentials,file_mode=0777,dir_mode=0777,uid=1000,gid=1000 0 0

For the credentials to log into the SMB file shares, I created a file /root/.smbcredentials so only root would have access to the file. The contents of the file are simply:

username=readonlyusername
password=readonlypassword

Once this is saved, I can then run “sudo mount /media/ISO1” and “sudo mount /media/ISO2” and the folders would mount, and will mount automatically on a reboot.

Leave a Reply

Your email address will not be published.