This is just me celebrating a small win. I’ve been slowly learning bash scripting, and just now I was able to quickly write a simple bash script to automate a file moving task without referring to my notes or the web!

It’s not a super complicated script, I’m just happy I’m starting to internalize the knowledge I’ve been building.

I’ve been organizing my media files after ripping our DVD collection. I had all the files for The Smurfs cartoon (love the Smurfs) in the main Smurfs show folder. I wanted to put them all into their respective season folders (Season.XX). Here’s the script:

#! /bin/bash

for number in {01..09}; do
	find . -type f -name "The.Smurfs.S$number*" -exec mv {} Season.$number/ \;
done

I could have done it as a one liner, but I like to keep things like this for future reference.

  • LupusBlackfur@lemmy.world
    link
    fedilink
    arrow-up
    10
    arrow-down
    1
    ·
    12 days ago

    Pfft…

    Good job.

    The “one liner” thing is entirely overrated.

    Honestly, who the fuck cares…?? 🤷🤷

    • harsh3466@lemmy.mlOP
      link
      fedilink
      arrow-up
      3
      ·
      12 days ago

      Thank you! I always worry with a one liner that I’ll fuck something up in a bad way. In a script it helps me think about the process since it’s more visually structured, and for me it’s easier to test.

  • JubilantJaguar@lemmy.world
    link
    fedilink
    arrow-up
    9
    ·
    11 days ago

    Good job. But don’t worry if you have to look up answers. I’ve been at this for 20 years and I still have to look up and double-check basic syntax like the classic find -exec one. No big deal if only takes a couple of seconds.

    This is definitely the sort of thing that LLM AI tools can help with, in theory.

  • land@lemmy.ml
    link
    fedilink
    arrow-up
    4
    ·
    12 days ago

    Great job!

    Learning Bash scripting and Vim is on my to-do list. 🎯

  • electric_nan@lemmy.ml
    link
    fedilink
    arrow-up
    3
    ·
    12 days ago

    I have studiously avoided learning any bash scripting for the 17 years I’ve used Linux, so all I can say is good job! Actually just today I found a command that I needed to get a certain appimage to run without crashing, and I remembered enough that I was able to make it into a script (I struggle with whether it’s !# or #!). Having just done it today, I can confirm you don’t need to include ‘/bin/bash’, just FYI. I believe that is assumed.

  • juipeltje@lemmy.world
    link
    fedilink
    arrow-up
    1
    ·
    11 days ago

    I really love bash scripting. I use it to symlink my dotfiles from my git repo, to configure my void system the way i like it, dmenu scripts, and i also use it on my laptop to send me notifications when my battery is almost empty, when i plug/unplug the charger, when it’s fully charged, and to hibernate when it’s lower than 5%. For some reason apps like upower don’t seem to work on my laptop so bash offered a solution.

    • harsh3466@lemmy.mlOP
      link
      fedilink
      arrow-up
      2
      ·
      11 days ago

      That’s fantastic. I’m not using it that deeply yet. I do have other scripts for managing my media files and adding them to my server as I rip music and DVDs. I also am loving learning it and using it.

    • DollyDuller@programming.dev
      link
      fedilink
      English
      arrow-up
      2
      ·
      11 days ago

      If you want to share your script with others, I think it’s a good idea to make it as easy to read as possible.

      If you’re just keeping it for yourself, that’s fine. But if you’re sharing it with me, the more readable it is, the easier I’ll be able to understand what you’re trying to do and how you’re solving the problem. This will make it easier for us to discuss ideas and improvements together. To be honest, one-liners can be a bit confusing because they can do multiple things at once. Breaking things down into individual steps makes it easier for me to follow along and for you to understand why you made certain decisions.

      Plus, it’s a good habit to get into for your own future reference - you’ll be able to look back and understand your thought process more easily.

      As the famous saying from SICP goes:

      programs must be written for people to read, and only incidentally for machines to execute

      • harsh3466@lemmy.mlOP
        link
        fedilink
        arrow-up
        1
        ·
        11 days ago

        Agree. Make it as easy to read as possible. I learned this particularly after I had written a script that had a lot of nesting. It worked initially, but not for long and when I went back to debug I was like, “What the fuck was I thinking here?”

        I ended up completely rewriting it to minimize the nesting and make it much more efficient and readable