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

top 26 comments
sorted by: hot top controversial new old
[-] bjoern_tantau@swg-empire.de 8 points 2 days ago

For anyone looking for a GUI tool I like to use krename.

[-] MagnificentSteiner@lemmy.zip 8 points 2 days ago* (last edited 1 day ago)

Tested this with perl-rename and it seems to work fine:

perl-rename -n 's/.* (\d{1,2}).* (\d{1,2})/sprintf "S%02dE%02d",$1,$2/e' *

-n does a test-run and shows you the results, remove it to do the actual rename

's/ text to find / text to replace it with / modifier' is the format used

. represents any single character

'*' matches the previous character 1 or more times

'.* ' matches any series of characters before a space

() encloses something you want to keep

\ is the escape character

d represents any number

{1,2} means the number will be 1 or 2 digits

In the sprintf, %02d is to format each number with 2 digits.

$1 and $2 pass the first and second values enclosed in parentheses from the first argument into the 2 d's in the "S%02dE%02d" of the second argument.

This link has a better explanation of the /e modifier than I could give you xD

https://www.geeksforgeeks.org/perl/perl-e-modifier-in-regular-expression/ ___

[-] schipelblorp@sh.itjust.works 7 points 2 days ago

Cool! Thanks!

Is this standard PERL stuff? How I learn what all these things mean? One day I hope to write my own arguments.

[-] forestbeasts@pawb.social 3 points 20 hours ago* (last edited 20 hours ago)

Perl has its own man pages, like commands do, but specifically for Perl stuff!

There's also a "perldoc" command that works a lot like man, that documents the functions and things.

For this, check out perldoc -f s (the s "function") and man perlretut (regular expressions). MagnificentSteiner here is using /e on the end of the regex to write Perl code in the replacement instead of a string (that's mentioned in the perlre man page), and then using the sprintf function to do some formatting, which, perldoc -f sprintf.

For a general intro to Perl, man perlintro!

... Oh, uh, you may need to install the perl-doc package (or equivalent) to have any of this documentation.

-- Frost

[-] MagnificentSteiner@lemmy.zip 4 points 2 days ago* (last edited 2 days ago)

No idea lol

I just wanted a rename utility and Arch didn't have the GUI one I used on Mint so I gradually pieced some of this stuff together. If there's one source online that lays it all out I haven't found it yet!

If you'd like me to explain the one here I'll do my best ;)

Edit: nvm, I edited the original comment to explain everything

[-] b_van_b@programming.dev 6 points 2 days ago

I think you want something like this:

$ ls
'Star Trek Next Generation Season 01 Episode 22 - Bob.mkv'
'Star Trek Next Generation Season 1 Episode 1 - Encounter at Farpoint.mkv'
$ file-rename 's/.*Season (\d+) Episode (\d+)/sprintf("S%02dE%02d", $1, $2)/ie' *.mkv
$ ls
'S01E01 - Encounter at Farpoint.mkv'  'S01E22 - Bob.mkv'

If you are getting an angle bracket, it's probably because you didn't add *.mkv at the end.

[-] schipelblorp@sh.itjust.works 1 points 2 days ago* (last edited 2 days ago)

Thanks! I'll dig into this tonight. The man page could use a bit of work, too, if you're feeling generous. A lot is assumed to be known.

Edit: Your entry is winning “shortest code” so fa r...

[-] Barbarian@sh.itjust.works 3 points 2 days ago* (last edited 2 days ago)

It's so short because it's special-purpose rather than general purpose. Perl is a programming language that can do lots and lots of different things. file-rename does one thing, and one thing very well: it renames files using regex

[-] triplenadir@lemmygrad.ml 1 points 1 day ago
[-] plantsmakemehappy@lemmy.zip 4 points 2 days ago

Share the rename commands you've tried

[-] thr0w4w4y2@sh.itjust.works 3 points 2 days ago

what’s the outcome you’re trying to achieve? you’ve got a folder full of mkv files and you just want to rename them?

why not keep it simple and use a for loop with mv and awk to perform the rename?

[-] schipelblorp@sh.itjust.works 2 points 2 days ago

editted for clarity. thanks. I have a bad habit of not providing context. underdeveloped theory of mind.

[-] schipelblorp@sh.itjust.works 1 points 2 days ago

Because I need to preserve the Season and Episode information.

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

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

Keep episode title as is.

[-] 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.

[-] schipelblorp@sh.itjust.works 2 points 2 days ago

I'll look into it. I'm going to be running a media server eventually, so thinking learning the command line arguments would be a good investment.

In this case, I'm moving these onto DVD and the truncated file names will all look the same on the DVD player, so I need to shorten 24*7 files.

[-] schipelblorp@sh.itjust.works 2 points 2 days ago

Re-phrased the title. It's a plain language question.

[-] Successful_Try543@feddit.org 2 points 2 days ago

To me, your problem feels like the command you're trying to execute isn't complete syntax-wise. It may be useful if you'd quote the actual command you're trying to execute.

[-] ulkesh@piefed.social 2 points 2 days ago

This doesn't answer your question at all, so my apologies up front.

I'm not at all intending to advertise here, and I'm not at all affiliated with them, but in the past, I've sidestepped having to write scripts by using FileBot. It's always just worked for me.

[-] SanderZeldenthuis@nord.pub 2 points 2 days ago
mv “Star Trek Next Generation Season 1 Episode 1 - Encounter at Farpoint.mkv” “S01E01 - Encounter at Far Point.mkv”  

Or am I missing something about what you're trying to do?

[-] teft@piefed.social 2 points 2 days ago

I think he wants to rename more than one file. Sounds like he wants to rename entire folders using that formula.

[-] schipelblorp@sh.itjust.works 1 points 2 days ago

Yes, the “etc.” part. Imagine I have 7*24 episodes following this convention. How do I rename them all in one swell foop?

find command with an exec. Basically, find can list files matching a pattern and then run a command for each of them. The exec will probably be a bit gnarly, though, so if you want something a little more palatable, you want a script that grabs the names of the files into a variable, then takes each entry via a for loop (find command and a bash while read might work), stores it in a variable, changes it based on your pattern (sed might work here), stores the changed name in another variable and then mv $former-name $new-name

[-] SanderZeldenthuis@nord.pub 1 points 2 days ago* (last edited 2 days ago)

If the naming of each series is that consistent, you could just use parameters in bash and build the new title out of it and then do the rename, in a loop. It would be a very short shell script.

eta: using printf to format the new title variable will let it handle the number formatting clearnly, like 01 for 1

eta: something like this

for f in *"Season "*; do s_ep="${f#*Season }"; s="${s_ep%% Episode*}"; ep_t="${s_ep#*Episode }"; ep="${ep_t%% - *}"; t="${ep_t#* - }"; printf -v new "S%02dE%02d - %s" "$s" "$ep" "$t"; mv "$f" "$new"; done 
[-] SiblingNoah@piefed.social 1 points 2 days ago
[-] krolden@lemmy.ml 0 points 2 days ago
this post was submitted on 03 Jun 2026
21 points (92.0% liked)

linux4noobs

3208 readers
30 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