Problems to solve with sed regex

OK - I swear by “perl” rename (from CPAN : the default rename command in Debian based distros - VS the shonky piece of crap Red Hat foist on users)…

Bought a new digital download album today - a bunch of mostly doom/stoner bands covering the songs from Seattle band Soundgarden’s 1994 album “Superunknown”.

But I now have fields in filenames I want to reposition :

'Beastwars - Superunknown (Redux) - 10 The Day I Tried to Live.flac'
'Darkher - Superunknown (Redux) - 15 Like Suicide.flac'
'Dozer - Superunknown (Redux) - 14 Half.flac'
'Frayle - Superunknown (Redux) - 06 Head Down.flac'
'High Priest - Superunknown (Redux) - 02 My Wave.flac'
'Horseburner - Superunknown (Redux) - 08 Spoonman.flac'
'Jack Harlon & The Dead Crows - Superunknown (Redux) - 11 Kickstand.flac'
'Marc Urselli'\''s SteppenDoom - Superunknown (Redux) - 13 4th of July.flac'
'Marissa Nadler - Superunknown (Redux) - 03 Fell on Black Days.flac'
'Somnuri - Superunknown (Redux) - 04 Mailman.flac'
'Spotlights - Superunknown (Redux) - 07 Black Hole Sun.flac'
'The Age of Truth - Superunknown (Redux) - 12 Fresh Tendrils.flac'
'Ufomammut - Superunknown (Redux) - 01 Let Me Drown.flac'
'Valley of the Sun - Superunknown (Redux) - 05 Superunknown.flac'
'Witch Mountain - Superunknown (Redux) - 09 Limo Wreck.flac'

I want the track number as the first field - at the very least… Just for the files - I’d be happy (maybe) with everything after "(Redux) " and omit all the rest…

But FFS - I don’t know enough regex (does anyone?) to figure that out… I can get rid of " - Superunknown (Redux) - " But I don’t want that first field, “first” either - that first field is the artist or band - and that’s already in the metadata in the digital music file!

I can’t even sort it properly… WTF?

@daniel.m.tripp ,
The number of fields per record is not consistent
The only consistent feature I can see is the ‘-’ signs.
Could we somehow get everything before the first - into a set of quotes,
and
everything after the second - into a set of quotes.
I would try with awk… count fields up to each minus then concatenate them and print the concatenated value. awk will quote it.

Have fun
Neville

1 Like

I might have to settle for removing everything, up to, and preceding "(Redux) - " even if it leaves the trailing space after the dash - I can get rid of that later…

But I’m not even sure how to accomplish that… and I’d rather not use awk - because perl/CPAN rename is way more “sed” than “awk” (I think they both use the same obscure regex)… i.e. I can easily - with rename rename 's/\ //' and only replace the first space later… I could even later on replace the resulting file - e.g. 09 Limo Wreck.flac" with “09-Limo Wreck” (and later on replace all spaces - looks like a prime case for CamelCase)…

I think dash/hyphen is going to have to be my field separator…

Yep, thats the only option
Permute each minus into two quotes

2 Likes

Okay - here’s a start :

perl-rename 's/^[^-]+-//' *.flac

And at least I end up with :

' Superunknown (Redux) - 01 Let Me Drown.flac'
' Superunknown (Redux) - 02 My Wave.flac'
' Superunknown (Redux) - 03 Fell on Black Days.flac'
' Superunknown (Redux) - 04 Mailman.flac'
' Superunknown (Redux) - 05 Superunknown.flac'
' Superunknown (Redux) - 06 Head Down.flac'
' Superunknown (Redux) - 07 Black Hole Sun.flac'
' Superunknown (Redux) - 08 Spoonman.flac'
' Superunknown (Redux) - 09 Limo Wreck.flac'
' Superunknown (Redux) - 10 The Day I Tried to Live.flac'
' Superunknown (Redux) - 11 Kickstand.flac'
' Superunknown (Redux) - 12 Fresh Tendrils.flac'
' Superunknown (Redux) - 13 4th of July.flac'
' Superunknown (Redux) - 14 Half.flac'
' Superunknown (Redux) - 15 Like Suicide.flac'

Which is a huge headstart - I can sort - and I have a common string I can escape / replace with some simpler regex…

(I made “perl-rename” a symlink to /usr/bin/prename - most Debian based systems install prename, and create an alias “alias rename=prename” - I’m sure most Ubuntu versions shipped with prename pre-installed - but apparently not with Ubuntu 23.04 - had to install it - no drama - but an alarming sign of things to come - like needing “sudo” to run dmesg, and not installing f–cking cal/nal by default)…

2 Likes

My biggest problem now is that regex uses all sorts of brackets - all of them AFAIK : “([{” (and the other side) and I’ve had problems with sed before if a string contained any sort of bracket character… If you want to get rid of any of them - it makes things more complex than they should perhaps, be…

1 Like

Maybe this will work:

sort -k6 -o output.txt input.txt

where

-k6 means sort using the sixth field (which is the track number)
-o means send output to a file rather than standard output.

2 Likes

Maybe you could find a Python library to read the information from the metadata and rename the file that way.

1 Like

This did it :
rename 's/\ Superunknown\ \(Redux\)\ -\ //' *

then
rename 's/\ /-/' *
replace first space " " with a dash

then
rename 's/\ //g' *
then subsequently remove ALL (/g) spaces

And got this :

01-LetMeDrown.flac
02-MyWave.flac
03-FellonBlackDays.flac
04-Mailman.flac
05-Superunknown.flac
06-HeadDown.flac
07-BlackHoleSun.flac
08-Spoonman.flac
09-LimoWreck.flac
10-TheDayITriedtoLive.flac
11-Kickstand.flac
12-FreshTendrils.flac
13-4thofJuly.flac
14-Half.flac
15-LikeSuicide.flac
3 Likes

ran into an issue over the weekend where I had a bunch of folders with the year name as the last field… Easy enough to identify using awk ‘{print $NF}’ - problem I ran into was it wasn’t easy to do in sed (and thus perl rename “prename”) regex and I gave up and did it manually…

1 Like

Would *$ match the last field in sed?

1 Like

I have only just read this thread so late in replying. I copied the sample text into file ‘DanTripp.txt’, then assuming all text lines have 3 fields separated by ‘-’ as in Dan’s original post then
cut -d- -f3 DanTripp.txt | sed ‘s/^ //’ | sed ‘s/ /-/’ | sort
produces the output shown in the thread.
I hope this is of interest since it uses ‘cut’ which is different from solutions shown.
Cheers
Clive

4 Likes

Interesting - I actually used cut before I ever used sed or awk…

1 Like

Waking this up again…

I had some files that start with what I thought was '" ’ (i.e. double quote, followed by space).

BUT NO! perl rename / prename wasn’t finding " in the files and giving up. But I was wrong!

This is the char :

Yeah - it looks like double quote followed by a space - but - it’s a SINGLE char - how is that possible?

Here’s my list of files :

-rw-rw-r--  1 x x  6350349 Nov  8  2023 '"A Calf Born in Winter" by Khruangbin from THE HOLDOVERS [GIkzMStIMcE].mp3'
-rw-rw-r--  1 x x  7645989 Jan  7  2024 '"A Girl in Tow ⧸ Back to Barton" by Mark Orton from THE HOLDOVERS [9c0MG3FW-zk].mp3'
-rw-rw-r--  1 x x  4276173 Nov 29  2023 '"Candlepin Bowling" by Mark Orton from THE HOLDOVERS [eHY1FqVemMY].mp3'
-rw-rw-r--  1 x x  2404533 Jan 15  2024 '"Carol of the Drum Little Drummer Boy" by TRapp Family Singers from THE HOLDOVERS [SuRVpOaJFjo].mp3'
-rw-rw-r--  1 x x  5413581 Nov 19  2023 '"Crying, Laughing, Loving, Lying" by Labi Siffre from THE HOLDOVERS [J3i5qQnFPNA].mp3'
-rw-rw-r--  1 x x  3851229 Jan  7  2024 '"Danny ⧸ The Glove ⧸ Let’s Make the Best of It" by Mark Orton from THE HOLDOVERS [TM_MTZoLU_g].mp3'
-rw-rw-r--  1 x x  3818253 Nov 30  2023 '"Drive to Boston" by Mark Orton from THE HOLDOVERS [CvhkOjHneoA].mp3'
-rw-rw-r--  1 x x 13852653 Nov 30  2023 '"In Memory of Elizabeth Reed" by Allman Brothers Band from THE HOLDOVERS [WovGxE7Zdls].mp3'
-rw-rw-r--  1 x x  3467445 Jan 22  2024 '"It'\''s Christmas!" by Mark Orton from THE HOLDOVERS [cqfpXF_5iI8].mp3'
-rw-rw-r--  1 x x  5906253 Jan 15  2024 '"Jingle Bells" by Herb Alpert & The Tijuana Brass from THE HOLDOVERS [kw0tckOotJY].mp3'
-rw-rw-r--  1 x x  6009141 Jan  7  2024 '"Knock Three Times" by Tony and Orlando Dawn from THE HOLDOVERS [i24KFFcTjrE].mp3'
-rw-rw-r--  1 x x  5948661 Jan 21  2024 '"Medley" by The Swingle Sisters from THE HOLDOVERS [VUUP8zkAeqE].mp3'
-rw-rw-r--  1 x x  3333957 Jan  7  2024 '"Nursing Home" by Mark Orton from THE HOLDOVERS [M0kDugwntwk].mp3'
-rw-rw-r--  1 x x  1398837 Dec  1  2023 '"Primal Architecture" by Mark Orton from THE HOLDOVERS [RbYViT5pUqQ].mp3'
-rw-rw-r--  1 x x  7884621 Dec 31  2023 '"See Ya ⧸ Into the Unknown" by Mark Orton from THE HOLDOVERS [INNERPfbGfo].mp3'
-rw-rw-r--  1 x x 11699997 Jan  7  2024 '"Silent Night" by The Temptations from THE HOLDOVERS [5sjH7GuCKTU].mp3'
-rw-rw-r--  1 x x  3580221 Nov 19  2023 '"Silver Joy" by Damien Jurado from THE HOLDOVERS [l_G7tXzTHVE].mp3'
-rw-rw-r--  1 x x  8082285 Jan  1  2024 '"The Glove ⧸ Now He’s History ⧸ 5⧸4 for Constantine" by Mark Orton from THE HOLDOVERS [PT-1lGa3kbY].mp3'
-rw-rw-r--  1 x x  4765677 Jan 22  2024 '"The Most Wonderful Time of the Year" by Andy Williams from THE HOLDOVERS [-R_5Zfx2c6Y].mp3'
-rw-rw-r--  1 x x  9822213 Nov 29  2023 '"The Time Has Come Today" by The Chamber Brothers from THE HOLDOVERS [cPxwLS0jFkc].mp3'
-rw-rw-r--  1 x x  3246189 Nov 19  2023 '"The Wind" by Cat Stevens from THE HOLDOVERS [CXNAYyKnkv4].mp3'
-rw-rw-r--  1 x x  5409501 Nov 27  2023 '"Venus" by Shocking Blue from THE HOLDOVERS [amv5yOfVM2U].mp3'
-rw-rw-r--  1 x x  3591909 Nov 14  2023 '"When Winter Comes" by Artie Shaw from THE HOLDOVERS [7KH3fPzx0ew].mp3'
-rw-rw-r--  1 x x  4103853 Jan 22  2024 '"White Christmas" by The Swingle Singers from THE HOLDOVERS [U042cBcUrLQ].mp3'

Then a dry run :

╭─x@titan ~/tmp/holdovers  
╰─➤  rename -n 's/\"//' *
rename("A Calf Born in Winter" by Khruangbin from THE HOLDOVERS [GIkzMStIMcE].mp3, A Calf Born in Winter" by Khruangbin from THE HOLDOVERS [GIkzMStIMcE].mp3)
rename("A Girl in Tow ⧸ Back to Barton" by Mark Orton from THE HOLDOVERS [9c0MG3FW-zk].mp3, A Girl in Tow ⧸ Back to Barton" by Mark Orton from THE HOLDOVERS [9c0MG3FW-zk].mp3)
rename("Candlepin Bowling" by Mark Orton from THE HOLDOVERS [eHY1FqVemMY].mp3, Candlepin Bowling" by Mark Orton from THE HOLDOVERS [eHY1FqVemMY].mp3)
rename("Carol of the Drum Little Drummer Boy" by TRapp Family Singers from THE HOLDOVERS [SuRVpOaJFjo].mp3, Carol of the Drum Little Drummer Boy" by TRapp Family Singers from THE HOLDOVERS [SuRVpOaJFjo].mp3)
rename("Crying, Laughing, Loving, Lying" by Labi Siffre from THE HOLDOVERS [J3i5qQnFPNA].mp3, Crying, Laughing, Loving, Lying" by Labi Siffre from THE HOLDOVERS [J3i5qQnFPNA].mp3)
rename("Danny ⧸ The Glove ⧸ Let’s Make the Best of It" by Mark Orton from THE HOLDOVERS [TM_MTZoLU_g].mp3, Danny ⧸ The Glove ⧸ Let’s Make the Best of It" by Mark Orton from THE HOLDOVERS [TM_MTZoLU_g].mp3)
rename("Drive to Boston" by Mark Orton from THE HOLDOVERS [CvhkOjHneoA].mp3, Drive to Boston" by Mark Orton from THE HOLDOVERS [CvhkOjHneoA].mp3)
rename("In Memory of Elizabeth Reed" by Allman Brothers Band from THE HOLDOVERS [WovGxE7Zdls].mp3, In Memory of Elizabeth Reed" by Allman Brothers Band from THE HOLDOVERS [WovGxE7Zdls].mp3)
rename("It's Christmas!" by Mark Orton from THE HOLDOVERS [cqfpXF_5iI8].mp3, It's Christmas!" by Mark Orton from THE HOLDOVERS [cqfpXF_5iI8].mp3)
rename("Jingle Bells" by Herb Alpert & The Tijuana Brass from THE HOLDOVERS [kw0tckOotJY].mp3, Jingle Bells" by Herb Alpert & The Tijuana Brass from THE HOLDOVERS [kw0tckOotJY].mp3)
rename("Knock Three Times" by Tony and Orlando Dawn from THE HOLDOVERS [i24KFFcTjrE].mp3, Knock Three Times" by Tony and Orlando Dawn from THE HOLDOVERS [i24KFFcTjrE].mp3)
rename("Medley" by The Swingle Sisters from THE HOLDOVERS [VUUP8zkAeqE].mp3, Medley" by The Swingle Sisters from THE HOLDOVERS [VUUP8zkAeqE].mp3)
rename("Nursing Home" by Mark Orton from THE HOLDOVERS [M0kDugwntwk].mp3, Nursing Home" by Mark Orton from THE HOLDOVERS [M0kDugwntwk].mp3)
rename("Primal Architecture" by Mark Orton from THE HOLDOVERS [RbYViT5pUqQ].mp3, Primal Architecture" by Mark Orton from THE HOLDOVERS [RbYViT5pUqQ].mp3)
rename("See Ya ⧸ Into the Unknown" by Mark Orton from THE HOLDOVERS [INNERPfbGfo].mp3, See Ya ⧸ Into the Unknown" by Mark Orton from THE HOLDOVERS [INNERPfbGfo].mp3)
rename("Silent Night" by The Temptations from THE HOLDOVERS [5sjH7GuCKTU].mp3, Silent Night" by The Temptations from THE HOLDOVERS [5sjH7GuCKTU].mp3)
rename("Silver Joy" by Damien Jurado from THE HOLDOVERS [l_G7tXzTHVE].mp3, Silver Joy" by Damien Jurado from THE HOLDOVERS [l_G7tXzTHVE].mp3)
rename("The Glove ⧸ Now He’s History ⧸ 5⧸4 for Constantine" by Mark Orton from THE HOLDOVERS [PT-1lGa3kbY].mp3, The Glove ⧸ Now He’s History ⧸ 5⧸4 for Constantine" by Mark Orton from THE HOLDOVERS [PT-1lGa3kbY].mp3)
rename("The Most Wonderful Time of the Year" by Andy Williams from THE HOLDOVERS [-R_5Zfx2c6Y].mp3, The Most Wonderful Time of the Year" by Andy Williams from THE HOLDOVERS [-R_5Zfx2c6Y].mp3)
rename("The Time Has Come Today" by The Chamber Brothers from THE HOLDOVERS [cPxwLS0jFkc].mp3, The Time Has Come Today" by The Chamber Brothers from THE HOLDOVERS [cPxwLS0jFkc].mp3)
rename("The Wind" by Cat Stevens from THE HOLDOVERS [CXNAYyKnkv4].mp3, The Wind" by Cat Stevens from THE HOLDOVERS [CXNAYyKnkv4].mp3)
rename("Venus" by Shocking Blue from THE HOLDOVERS [amv5yOfVM2U].mp3, Venus" by Shocking Blue from THE HOLDOVERS [amv5yOfVM2U].mp3)
rename("When Winter Comes" by Artie Shaw from THE HOLDOVERS [7KH3fPzx0ew].mp3, When Winter Comes" by Artie Shaw from THE HOLDOVERS [7KH3fPzx0ew].mp3)
rename("White Christmas" by The Swingle Singers from THE HOLDOVERS [U042cBcUrLQ].mp3, White Christmas" by The Swingle Singers from THE HOLDOVERS [U042cBcUrLQ].mp3)

Then a final run (2x) :

╭─x@titan ~/tmp/holdovers  
╰─➤  ls -al            
total 132732
drwxrwxr-x  2 x x     4096 Dec 14 20:49  .
drwxrwxr-x 35 x x     4096 Dec 14 18:01  ..
-rw-rw-r--  1 x x  6350349 Nov  8  2023 'A Calf Born in Winter by Khruangbin from THE HOLDOVERS [GIkzMStIMcE].mp3'
-rw-rw-r--  1 x x  7645989 Jan  7  2024 'A Girl in Tow ⧸ Back to Barton by Mark Orton from THE HOLDOVERS [9c0MG3FW-zk].mp3'
-rw-rw-r--  1 x x  4276173 Nov 29  2023 'Candlepin Bowling by Mark Orton from THE HOLDOVERS [eHY1FqVemMY].mp3'
-rw-rw-r--  1 x x  2404533 Jan 15  2024 'Carol of the Drum Little Drummer Boy by TRapp Family Singers from THE HOLDOVERS [SuRVpOaJFjo].mp3'
-rw-rw-r--  1 x x  5413581 Nov 19  2023 'Crying, Laughing, Loving, Lying by Labi Siffre from THE HOLDOVERS [J3i5qQnFPNA].mp3'
-rw-rw-r--  1 x x  3851229 Jan  7  2024 'Danny ⧸ The Glove ⧸ Let’s Make the Best of It by Mark Orton from THE HOLDOVERS [TM_MTZoLU_g].mp3'
-rw-rw-r--  1 x x  3818253 Nov 30  2023 'Drive to Boston by Mark Orton from THE HOLDOVERS [CvhkOjHneoA].mp3'
-rw-rw-r--  1 x x 13852653 Nov 30  2023 'In Memory of Elizabeth Reed by Allman Brothers Band from THE HOLDOVERS [WovGxE7Zdls].mp3'
-rw-rw-r--  1 x x  3467445 Jan 22  2024 'It'\''s Christmas! by Mark Orton from THE HOLDOVERS [cqfpXF_5iI8].mp3'
-rw-rw-r--  1 x x  5906253 Jan 15  2024 'Jingle Bells by Herb Alpert & The Tijuana Brass from THE HOLDOVERS [kw0tckOotJY].mp3'
-rw-rw-r--  1 x x  6009141 Jan  7  2024 'Knock Three Times by Tony and Orlando Dawn from THE HOLDOVERS [i24KFFcTjrE].mp3'
-rw-rw-r--  1 x x  5948661 Jan 21  2024 'Medley by The Swingle Sisters from THE HOLDOVERS [VUUP8zkAeqE].mp3'
-rw-rw-r--  1 x x  3333957 Jan  7  2024 'Nursing Home by Mark Orton from THE HOLDOVERS [M0kDugwntwk].mp3'
-rw-rw-r--  1 x x  1398837 Dec  1  2023 'Primal Architecture by Mark Orton from THE HOLDOVERS [RbYViT5pUqQ].mp3'
-rw-rw-r--  1 x x  7884621 Dec 31  2023 'See Ya ⧸ Into the Unknown by Mark Orton from THE HOLDOVERS [INNERPfbGfo].mp3'
-rw-rw-r--  1 x x 11699997 Jan  7  2024 'Silent Night by The Temptations from THE HOLDOVERS [5sjH7GuCKTU].mp3'
-rw-rw-r--  1 x x  3580221 Nov 19  2023 'Silver Joy by Damien Jurado from THE HOLDOVERS [l_G7tXzTHVE].mp3'
-rw-rw-r--  1 x x  8082285 Jan  1  2024 'The Glove ⧸ Now He’s History ⧸ 5⧸4 for Constantine by Mark Orton from THE HOLDOVERS [PT-1lGa3kbY].mp3'
-rw-rw-r--  1 x x  4765677 Jan 22  2024 'The Most Wonderful Time of the Year by Andy Williams from THE HOLDOVERS [-R_5Zfx2c6Y].mp3'
-rw-rw-r--  1 x x  9822213 Nov 29  2023 'The Time Has Come Today by The Chamber Brothers from THE HOLDOVERS [cPxwLS0jFkc].mp3'
-rw-rw-r--  1 x x  3246189 Nov 19  2023 'The Wind by Cat Stevens from THE HOLDOVERS [CXNAYyKnkv4].mp3'
-rw-rw-r--  1 x x  5409501 Nov 27  2023 'Venus by Shocking Blue from THE HOLDOVERS [amv5yOfVM2U].mp3'
-rw-rw-r--  1 x x  3591909 Nov 14  2023 'When Winter Comes by Artie Shaw from THE HOLDOVERS [7KH3fPzx0ew].mp3'
-rw-rw-r--  1 x x  4103853 Jan 22  2024 'White Christmas by The Swingle Singers from THE HOLDOVERS [U042cBcUrLQ].mp3'

Above files were grabbed using youtube-dl or yt-dlp… soundtrack for the movie “The Holdovers”…

2 Likes

The single character " does not have any inbuilt space around it ?
It must be being done by the typesetting algorithm.

1 Like

Those files came from youtube URLs (thanks to yt-dlp / youtube-dl) - I suspect some metatag in the URL had that glyph - i.e. a double quote with a hard space in a single “glyph”:

'"A Calf Born in Winter" by Khruangbin from THE HOLDOVERS [GIkzMStIMcE].mp3'
2 Likes

OK, not typesetting

1 Like

OK - now I have another issue - with files… (edit: resolved without sed regex) :

171 songs in a folder…

They’re being sorted “wrong”…

i.e.

...
-rwxrwxr-x  1 x family  8193420 Jan  6 08:44 '159 - Abused Keyhole - Bewitched.mp3'
-rwxrwxr-x  1 x family  7236173 Jan  6 08:32 "15 - Black Spell - Saturn's Death.mp3"
-rwxrwxr-x  1 x family  2798865 Jan  6 08:44 '160 - Hecho Mierda - G.O.R.N.R..mp3'
...

I want some sed regex that will pad a leading zero onto the 2 digit numbers…

I don’t want to use awk or tr - I need sed regex syntax for (p)rename command…

Basically I want to pad a leading zero onto the filenames that start with 2 digits…

Tried this using find :
find . -maxdepth 1 -iname \?\?\ -\ \* -printf "%P\n"

That shows me the files with out the “./” - but - if I try to exec on that - it’s still trying to use “./” :
find . -maxdepth 1 -iname \?\?\ -\ \* -printf "%P\n" -exec mv {} 0{} \;

So gave up on that - did a for loop that worked :
for FCK in $(ls ??\ -\ *) ; do echo "$FCK" ; mv "$FCK" 0"$FCK" ; done

But I’d prefer the eloquence of sed regex.

I found this regex to increment a number in a filename by a value :
's|(\d+)|sprintf("%2d",$1+15)|e'

e.g. I don’t want a disk 1, disk 2, disk 3 etc folder for multiple disk albums (e.g. Led Zeppelin’s Physical Graffiti) - I just want one single folder, but I want track 1 of disk 2, to numerically follow the last track of disk 1… My Media DATABASE is my filesystem - so sort order is important - 'cause I only use filesystem sort order on music files - in Sayonara on Linux, in Calibri on MacOS and Music Player Folder Full on Android…

I could have used that to add 100, so e.g. the last track, 171, would be 271, and track 01 would be 101… But I don’t like that kludge… I didn’t want arithmetic, I just wanted to pad a 0 onto 2 digit numbers in a filename - so string based, not maths…

2 Likes

@daniel.m.tripp

From a file

~ $ cat tmp
01azz
02bzz
~ $ sed s/^/0/ tmp
001azz
002bzz
~ $

Done in Termux

2 Likes

That adds a zero (prepends) to all numbers - i.e. no matter how many digits…

I only wanted to add a “0” onto 2 digit numbers…

So with :

╭─x@titan ~/tmp/caca  
╰─➤  ls
'01 - File - 01.txt'  '03 - File - 03.txt'  '05 - File - 05.txt'  '07 - File - 07.txt'  '09 - File - 09.txt'    '10 - File - 10.txt'  '160 - File - 160.txt'  '99 - File - 99.txt'
'02 - File - 02.txt'  '04 - File - 04.txt'  '06 - File - 06.txt'  '08 - File - 08.txt'  '100 - File - 100.txt'  '11 - File - 11.txt'  '171 - File - 171.txt'

Then use (p)rename with that regex :

╭─x@titan ~/tmp/caca  
╰─➤  rename -n 's/^/0/' *                                                                           
rename(01 - File - 01.txt, 001 - File - 01.txt)
rename(02 - File - 02.txt, 002 - File - 02.txt)
rename(03 - File - 03.txt, 003 - File - 03.txt)
rename(04 - File - 04.txt, 004 - File - 04.txt)
rename(05 - File - 05.txt, 005 - File - 05.txt)
rename(06 - File - 06.txt, 006 - File - 06.txt)
rename(07 - File - 07.txt, 007 - File - 07.txt)
rename(08 - File - 08.txt, 008 - File - 08.txt)
rename(09 - File - 09.txt, 009 - File - 09.txt)
rename(100 - File - 100.txt, 0100 - File - 100.txt)
rename(10 - File - 10.txt, 010 - File - 10.txt)
rename(11 - File - 11.txt, 011 - File - 11.txt)
rename(160 - File - 160.txt, 0160 - File - 160.txt)
rename(171 - File - 171.txt, 0171 - File - 171.txt)
rename(99 - File - 99.txt, 099 - File - 99.txt)

rename -n” is a dummy run - shows you what it would do - and what it would do is make 2 digit numbers 3 digit - but - already 3 digit numbers 4 digits - so the files would still not sort in the desired sort order…
So then do it for real :

╭─x@titan ~/tmp/caca  
╰─➤  rename 's/^/0/' *
╭─x@titan ~/tmp/caca  
╰─➤  ls -al
total 8
drwxrwxr-x  2 x x 4096 Jan  7 16:12  .
drwxrwxr-x 35 x x 4096 Jan  7 16:09  ..
-rw-rw-r--  1 x x    0 Jan  7 16:09 '001 - File - 01.txt'
-rw-rw-r--  1 x x    0 Jan  7 16:09 '002 - File - 02.txt'
-rw-rw-r--  1 x x    0 Jan  7 16:09 '003 - File - 03.txt'
-rw-rw-r--  1 x x    0 Jan  7 16:09 '004 - File - 04.txt'
-rw-rw-r--  1 x x    0 Jan  7 16:09 '005 - File - 05.txt'
-rw-rw-r--  1 x x    0 Jan  7 16:09 '006 - File - 06.txt'
-rw-rw-r--  1 x x    0 Jan  7 16:09 '007 - File - 07.txt'
-rw-rw-r--  1 x x    0 Jan  7 16:09 '008 - File - 08.txt'
-rw-rw-r--  1 x x    0 Jan  7 16:09 '009 - File - 09.txt'
-rw-rw-r--  1 x x    0 Jan  7 16:09 '0100 - File - 100.txt'
-rw-rw-r--  1 x x    0 Jan  7 16:09 '010 - File - 10.txt'
-rw-rw-r--  1 x x    0 Jan  7 16:09 '011 - File - 11.txt'
-rw-rw-r--  1 x x    0 Jan  7 16:09 '0160 - File - 160.txt'
-rw-rw-r--  1 x x    0 Jan  7 16:09 '0171 - File - 171.txt'
-rw-rw-r--  1 x x    0 Jan  7 16:09 '099 - File - 99.txt'

And file 0100 appears before 010 - which I don’t want…

Anyway - if I ever come across something similar again - I’ll just use a for loop on output of “ls ??\ -\ \*” command

3 Likes