Problems to solve with sed regex

Got fed up of my music collection “subset” (that I travel with) being so large…

On my NAS - about 2/3 to 3/4 are FLAC…

But - when I listen to music from portable “subset” it’s almost always over Bluetooth - so having FLAC format makes no sense - as BT audio is inherently “lossy”…

So today - I’ve been slowly converting any folders of FLAC music back to mp3 - in my subset collection…

So far I’ve saved over 100GB - there’s no point having lossless audio in the stuff I carry around on my phone - as I hardly ever listen to that stuff via analog means - it’s almost always via bluetooth headphones (including bone conducting headset) - so FLAC doesn’t make any sense. When sitting at my desktop, with my Pop! desktop connected via Gigabit to my NFS share on my NAS - with good headphones connected via analog - I can tell the difference between an mp3 and an flac version of the same song…

But over BT? No - they sound the same to me (but neither as good as analog audio from vinyl!)…

I’ve been a bit cavalier - but not overly concerned - anything I inadvertantly delete on my subset collection - should be still available in lossless format from the main music file collection on my NAS…

I was doing stuff like “convert-it $ARG-FLAC ; delete-it $ARG-FLAC” - and messed up a few folders on my subset collection… but easy enough to restore from my NAS…

Summary - I have a symlink to my flac-2-mp3 script as “flacco” :

for F in $(ls) ; do echo -e "\t --- $F ---" ; flacco $F ; rm -Rf $F ; done

BTW - “flacco” was a clown on late night TV in Australia (on the right) :

OK - hit the old “prefix two digit numbers with a zero issue” again - actually twice recently…

Couldn’t remember how I resolved the first one…

Spent ages trolling back through my shell history and couldn’t find it…

List of 158 files (music files)… 01..99 - Song.mp3 - then 100..158 - Song.mp3 - that doesn’t sort properly on my system… I don’t use tags or metadata to organise my music (I do use tags - but not for that).

Had to do the same thing again when I had them all as FLAC files (purchased on Bandcamp Friday).

Previously if I found something tricky I couldn’t resolve - I’d save it in a shell script “collection” of previously used regex… Forgot the first time…

Anyway - I found it (in my shell history) and saved it in my shell script regex repository (this only works if the number is the start of the filename):

# Pad 2 digit number in filename to 3 digit with leading zero...
rename -n 's/^/0/' ??\ *.flac

(“-n” means a dummy run)
Buggered if I can figure out how that works :smiley:

I “rewrote” my “flacco” script to also copy, then remove the converted mp3 album… new script is called “calfo”…
e.g. :

╭─x@titanii ~/MPZ/WOLFE-Chelsea  
╰─➤  ls -al
...
drwxrwxr-x+    2 x family   13 Aug 22 11:09 2026-TheDark-FLAC
...
╭─x@titanii ~/MPZ/WOLFE-Chelsea  
╰─➤  calfo 2026-TheDark-FLAC
...
going to rsync and then delete /mnt/BARGEARSE/MPZ/WOLFE-Chelsea/2026-TheDark-mp3...
rsync --remove-source-files -az /mnt/BARGEARSE/MPZ/WOLFE-Chelsea/2026-TheDark-mp3 /home/x/ResilioSync/Music/WOLFE-Chelsea/.
removing empty folder /mnt/BARGEARSE/MPZ/WOLFE-Chelsea/2026-TheDark-mp3...
╭─x@titanii ~/MPZ/WOLFE-Chelsea  
╰─➤  ls -al /home/x/ResilioSync/Music/WOLFE-Chelsea
...
drwxr-x---   2 x family  4096 Oct  1 11:57 2026-TheDark-mp3
...

Took me a while to figure out rsync --remove-source-files doesn’t remove the folder - only the files contained therein…

───────┬───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
       │ File: /home/x/bin/calfo
───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1   │ #!/usr/bin/env  bash
   2   │ # grab the flac files in $PWD and convert to mp3 (320k)
   3   │ PROG=$(basename $0)
   4   │ IFS=$(echo -en "\n\b")
   5   │ ALBUM=$1
   6   │ # change "-mp3" at end of dir to "-flac"
   7   │ MP3ALB=$(echo $ALBUM |sed 's/-FLAC/-mp3/')
   8   │ # remove spaces from dirname : 
   9   │ MP3ALB=$(echo $MP3ALB | sed 's/\ //g')
  10   │ 
  11   │ if [ $# -lt 1 ] ; then
  12   │     echo "need an argument"
  13   │     echo "... probably a folder with -FLAC in the end of the name..."
  14   │     echo "e.g. 1971 - If I Could Only Remember My Name-FLAC"
  15   │     echo "e.g.:"
  16   │     echo "          $PROG '1971 - If I Could Only Remember My Name-FLAC'"
  17   │     echo "... exiting now.... "
  18   │     exit 1
  19   │ fi
  20   │ 
  21   │ if [ ! -d $ALBUM ] ; then
  22   │     echo "$ALBUM folder doesn't exist..."
  23   │     echo "... so exiting doing nothing..."
  24   │     exit 1
  25   │ fi
  26   │ 
  27   │ check_it () { 
  28   │ if [ -d $MP3ALB ] ; then
  29   │     echo "$MP3ALB folder already exists..."
  30   │     echo "... so exiting doing nothing..."
  31   │     exit 1
  32   │ else
  33   │     mkdir $MP3ALB
  34   │     chmod 0750 $MP3ALB
  35   │ fi
  36   │ }
  37   │ 
  38   │ echo $MP3ALB
  39   │ echo "source : $ALBUM - destination $MP3ALB"
  40   │ 
  41   │ convert_it () {
  42   │ cd $ALBUM
  43   │ for f in $(ls *.flac)
  44   │ do 
  45   │     ffmpeg -i "$f" -ab 320k ../$MP3ALB/"${f%.*}".mp3
  46   │     # find any cover art : 
  47   │     find . -maxdepth 1 -iname \*jp\*g -o -iname \*png -exec cp {} $MP3ALB/. \;
  48   │ done
  49   │ }
  50   │ 
  51   │ ARTST=$(echo $PWD|awk -F\/ '{print $NF}')
  52   │ DEST="/home/x/ResilioSync/Music/$ARTST"
  53   │ if [ ! -d $DEST ] ; then
  54   │     echo "There's no folder for Artist : $ARTST...  creating it now..."
  55   │     echo "mkdir -p $DEST"
  56   │     mkdir -p $DEST
  57   │ fi
  58   │ PARENT="/mnt/BARGEARSE/MPZ/$ARTST"
  59   │ 
  60   │ copy_it () {
  61   │     if [ -d $DEST/$MP3ALB ] ; then
  62   │         echo "$DEST/$MP3ALB exists ..."
  63   │         echo "that album/folder's already there - exiting..."
  64   │         exit 1
  65   │     fi
  66   │     file $DEST/$MP3ALB
  67   │     echo "copying it now..."
  68   │     echo "cp -r $PARENT/$MP3ALB $DEST/."
  69   │     cp -r $PARENT/$MP3ALB $DEST/.
  70   │ }
  71   │ 
  72   │ rink_it () {
  73   │     echo "going to rsync and then delete $PARENT/$MP3ALB..."
  74   │     echo "rsync --remove-source-files -az $PARENT/$MP3ALB $DEST/."
  75   │     rsync --remove-source-files -az $PARENT/$MP3ALB $DEST/.
  76   │     echo "removing empty folder $PARENT/$MP3ALB..."
  77   │     rmdir $PARENT/$MP3ALB
  78   │ }
  79   │ 
  80   │ 
  81   │ check_it
  82   │ convert_it
  83   │ rink_it
  84   │ 
───────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

That last bit (copy and delete source) took me the longest - but had some spare time yesterday to practise…

You may ask why the convoluted rsync, then rmdir? i.e. wasted double-handling?

For whatever reason - when I “mv” to ResilioSync I get messages about being unable to change the UID/GID … It still “does the thing” but I don’t like error messages…