21
submitted 2 days ago* (last edited 2 days ago) by schipelblorp@sh.itjust.works to c/linux4noobs@programming.dev

Hi, y'all. Running Linux Mint and I have the puzzle presented above.

From what I gather, I'm using rename (1p) which makes mention of Perl and in the man page it says it will also run as file-rename. I'm not sure if this is the right rename utility for the common argument

s/old_pattern/new_pattern/

but any time I try to run anything (including -n), I just get an angle bracket > and have to ctrl-c out.

I'd also need some details on how the wildcards work, which seems to be lacking in the documentation.

Edit: Instructions unclear. I have a bunch of episodes that are very wordy. I'm moving them onto DVD and truncated on my player the directory will look like:

Star Trek The Next Gene....
Star Trek The Next Gene....
Star Trek The Next Gene....
Star Trek The Next Gene....
Star Trek The Next Gene....

so I want to take (sample episode)

Star Trek The Next Generation Season 1 Episode 1 - Encounter at Far Point

and

  • Replace 'Star Trek The Next Generation Season ' with 'S0'

  • Replace 'Episode ' with 'E0' or 'E' depending on digits

  • Keep episode title as is.

So it looks like

S01E01 - Encounter at Farpoint.mkv

you are viewing a single comment's thread
view the rest of the comments
[-] towerful@programming.dev 2 points 2 days ago* (last edited 2 days ago)

This is the kinda small script I use Claude for. So yeh, it's LLM generated. Downvote away.
But I am terrible at writing bash scripts!

#!/usr/bin/env bash
# rename-episodes.sh — run inside the folder, or pass a directory as $1

shopt -s nullglob
cd "${1:-.}" || exit 1

for f in *.mkv; do
    # Match: ... Season N ... Episode N - Title.mkv
    if [[ $f =~ Season\ ([0-9]+)\ Episode\ ([0-9]+)\ -\ (.+)\.mkv$ ]]; then
        season="${BASH_REMATCH[1]}"
        episode="${BASH_REMATCH[2]}"
        title="${BASH_REMATCH[3]}"

        # Zero-pad to two digits
        new=$(printf "S%02dE%02d - %s.mkv" "$season" "$episode" "$title")

        if [[ "$f" != "$new" ]]; then
            echo "mv: $f  ->  $new"
            # uncomment the next line to actually move the file
            # mv -n -- "$f" "$new"
        fi
    else
        echo "skip (no match): $f"
    fi
done

I've commented out the mv command so you can test/fiddle/play around with it without clobbering your files.

Some notes from Claude:

Two practical notes:
If your files aren't all .mkv, change the glob (*.mkv) and the regex anchor accordingly, or loop over *.{mkv,mp4,avi}.
This assumes the literal words "Season" and "Episode" appear. If your real filenames vary (e.g. "S1", "1x01", "Ep 1"), the regex needs adjusting


Edit: I just realised that specifically file-rename is mentioned, and looks like you are getting appropriate help in other threads.

this post was submitted on 03 Jun 2026
21 points (92.0% liked)

linux4noobs

3208 readers
3 users here now

linux4noobs


Noob Friendly, Expert Enabling

Whether you're a seasoned pro or the noobiest of noobs, you've found the right place for Linux support and information. With a dedication to supporting free and open source software, this community aims to ensure Linux fits your needs and works for you. From troubleshooting to tutorials, practical tips, news and more, all aspects of Linux are warmly welcomed. Join a community of like-minded enthusiasts and professionals driving Linux's ongoing evolution.


Seeking Support?

Community Rules

founded 2 years ago
MODERATORS