Filebot is supported on Windows, Linux, and MacOS
Filebot is a fantastic utility for getting your episodes organized in a way that Sonarr can successfully parse. Version 4.7.9 can still be downloaded for free from a SourceForge mirror, but there are also paid versions in the Windows and Apple stores. On Linux, your distribution of choice may have a package for it, like in Arch's AUR package or .deb files for Debian/Ubuntu from their download page. It has both a GUI and a CLI, so it should satisfy almost everyone.
There is great help available, including their format expressions page. Filebot's TV format expressions can produce a Series Name (Year)/Season XX/ layout that Sonarr parses cleanly.
To keep this pattern for future imports, you should set:
Settings => Media Management (Advanced Settings Shown) => Series Folder Format
{Series TitleYear}Season {season:00}Note: You can adjust the tokens above using any of the series naming tokens that Sonarr supports.
The following script will take all video files within your selected folder and move each into a folder based on the series portion of its name. Note that this does not go into subfolders within the starting/selected folder. Sort the resulting folders before importing — this is a starting point, not a parser.
cd /path/to/your/episode/files/
find . -maxdepth 1 -type f \( -iname "*.mkv" -o -iname "*.mp4" \) -exec sh -c 'mkdir "${1%.*}" ; mv "${1%}" "${1%.*}" ' _ {} \;
Alternatively in Windows you can run the following script in Powershell to iterate over each file in a directory, and move it to a folder with the same name.
Get-ChildItem -File
| ForEach-Object {
$dir = New-Item -ItemType Directory -Name $_.BaseName -Force
$_ | Move-Item -Destination $dir
}