Homelab, Linux, JS & ABAP (~˘▾˘)~
 

[Terminal] Bash script to add leading season and episode numbers by parsing from file names

I had some videos in the format “My Episode #01.mkv” which I wanted to rename to “S01E01 My Episode #01.mkv“. This little script did the job for me:

# Specify the directory containing the files. For current directory use: $(dirname "$0")
directory="/path/to/your/directory"	

# Loop through all .mkv files in the directory
for file in "$directory"/*.{webm,mkv}; do
    # Check if the file exists to avoid errors when no files match
    [ -e "$file" ] || continue
    
    # Extract the base filename (without the directory path)
    filename=$(basename "$file")

    # Use regex to find the episode number (e.g., #01, #02)
    if [[ $filename =~ \#([0-9]+) ]]; then
        episode_number=${BASH_REMATCH[1]}

        # Pad the episode number with a leading zero if it's a single digit
        if [ ${#episode_number} -eq 1 ]; then
            episode_number="0$episode_number"
        fi  
		
        # Construct the new filename
        new_filename="S01E${episode_number} $filename"
        
        # Rename the file
        mv "$file" "$directory/$new_filename"
        echo "Renamed: $filename -> $new_filename"
    fi
done

[Home Assistant] Ikea Traefdri Reset Script / Pairing mode

Handy script, to put Ikea Bulbs into pairing mode by using a Zigbee Switch: https://community.home-assistant.io/t/handy-script-to-reset-ikea-tradfri-bulb/435622

Just go to Settings -> Automations -> Scripts and paste the following YAML script:

alias: Reset IKEA bulb with Switch
sequence:
  - repeat:
      count: "5"
      sequence:
        - service: switch.turn_off
          data: {}
          target:
            entity_id: "{{ smart_plug }}"
        - delay:
            hours: 0
            minutes: 0
            seconds: 0
            milliseconds: 900
        - service: switch.turn_on
          data: {}
          target:
            entity_id: "{{ smart_plug }}"
        - delay:
            hours: 0
            minutes: 0
            seconds: 0
            milliseconds: 500
variables:
  smart_plug: switch.REPLACE_SWITCH_NAME
mode: single

Now just insert your light bulb with a light socket into the plug and run the script.

To use the script with other bulbs, just adjust the counter and seconds to your needs, e.g. like here for Ledvance bulbs:

alias: Reset Ledvance Gardenpole with Switch
sequence:
  - repeat:
      count: "5"
      sequence:
        - service: switch.turn_off
          data: {}
          target:
            entity_id: "{{ smart_plug }}"
        - delay:
            hours: 0
            minutes: 0
            seconds: 5
            milliseconds: 0
        - service: switch.turn_on
          data: {}
          target:
            entity_id: "{{ smart_plug }}"
        - delay:
            hours: 0
            minutes: 0
            seconds: 5
            milliseconds: 0
variables:
  smart_plug: switch.REPLACE_SWITCH_NAME
mode: single

[CAP] Download MTA deployment logs after deployment

With this simple command, you can download the MTA deployment logs of your recent deployment. Instead of manually having to use cf dmol -i <operation id>, just save the command (replace appId with your Id) as a shell script, make it executable (chmod +x) and run the script after your deployment. It stores the recent logs into the folder logs.

dmol is short for download-mta-op-logs

cf download-mta-op-logs --mta <appId> --last 1 -d logs  

To just have a quick look at the recent logs, use

cf logs <appId>-srv --recent