0

This is about the twentieth time I've tried to write this post but it always becomes a behemoth... so I'm going to try and keep it short.

Basically what I want to do is push my noobie skills to the limit by writing a script that take the output from the Deluge torrent client's console, namely the path and name of a folder when a torrent is added or completes, and then creates hardlinks for all the files within the torrent, naming them based on the various parts of the existing file's name(s) and path.

The existing download paths and file names contain everything I need except a few words that will be the same for every hardlink's name (i.e. each hardlink name would have "The" added to the beginning). I'll also need to replace certain bits of the file name with snippets of text that I can define within the script (i.e. replacing "One" with "1", or "Foos.Bar" with "Foos' Bar"). The various bits of info that can be derived from the folder path and filename would need to be rearranged in order for the new hardlink's name to match Plex's naming conventions.

The one other essential thing that the script must account for is that it should only create the hardlink if it does not already exist in the destination folder. This is complicated by the fact that the torrent(s) in question have multiple version released through the course of a week, with each version adding additional files to the folder.

I can get the deluge-console to output $torrentpath/$torrentname to a file but I don't now where to go from there.

If anyone can point me in the right direction in regards to the next step I need to take or what shell tools I should be using, then I'm happy to do as much of the leg work as I need to.


[EDIT 1]

if [[ $torrentname == "Name.*.of.Torrent.1080P" ]] then ;
    cd $torrentpath/$torrentname
#the follwing gives just the YYYY from a the uploader's filename.
    Eyear=$"ls 01* | sed -r 's/.{6}//; s/(.{4}).*/\1/'"

[EDIT 2]

I've answered my own question for the most part but I won't post the exact solution I came up with (and it's regex's) as it all depends on my specific scenario (folder structure and the names of the torrents I download). I will give a general overview though.

In order to set the variables I want, I'm using (or should that be "playing around with"?) ls and various sed and/or awk regex commands and pipes. I've got the commands working so that they work with the different naming schemes used by both the uploaders I use, and so that I can get most of the information I need to create a name that conforms to Plex's naming schemes (the rest of the name is standard text which I can simply insert).

In order to get to the point of having a working script, I need find answers to questions that are far beyond the scope of my original question. So before I mark this thread as solved, I'll close by saying if you're in the same boat as me and you want to extract strings from both a filename and it's folder path, research sed, awk, regex, and pipes...

sed for extracting strings from filenames, and for modifying the text

awk (piped into sed) for extracting strings from folder paths

  • 1
    Hardlinks are a very rarely used tool, so my first instinct would be to ask what the higher level problem is that you're trying to solve. It looks like one thing you're trying to do is some sort of simple string sanitation, which is usually done using `sed`. If you want to rename files using similar pattern matching logic there's the `rename` tool. A common tool for arbitrary manipulation of a stream of data like this would be the `while read -r line` loop. – l0b0 Jan 14 '21 at 01:41
  • tl;dr reading headline only - are you asking for dynamic variables? – alecxs Jan 14 '21 at 13:12
  • @l0b0 I've been playing around with `sed` and I'm able to get all the info I need from the various filenames (the sed command edited into my OP works as I want it to). I'm just not sure where to go from there. Would the code under "[EDIT 1]" work / set a dynamic variable? @alecxs I think so but as I said in my post being a noob, I'm not sure whether I'm going in the wrong direction. – Hollis Hurlbut Jan 15 '21 at 21:18
  • 1
    no that's unrelated to dynamic vars. you don't want derive multiple variable names. you just want derive multiple file names. you should give some real file names and expected result. also i don't get the hard link part. basically that's like cp but saves disk space. to redirect stdout into variable use *Eyear=$(ls 01\* | head -n1 | sed ...)* – alecxs Jan 15 '21 at 22:58
  • I use hardlinks so because... 1. when I started using them, Plex didn't support symlinks properly. 2. So that I can have files that conform to Plex's naming requirements and still be able to seed the torrents. 3. to save space. @alecxs a big thank you for pointing me in the direction of using pipes! It was a vital piece of the puzzle. As for wanting to derive various filenames, I guess that's true but the filenames I want to create have a number of strings within them that vary with each torrent and that can be derived as I describe in my OP. So I thought variables were the way to go. – Hollis Hurlbut Jan 17 '21 at 23:44

0 Answers0