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.
大きいBOY ( @ookiiBoy@lemmy.blahaj.zone ) English8•2 months agoLook up “shellcheck”, find a plugin for your editor. It should help a lot.
Oh goodness, this looks incredibly useful. Thank you!
Mike Wooskey ( @mike_wooskey@lemmy.thewooskeys.com ) English7•2 months agoCongrats! It’s fun and empowering to make your own tools, isn’t it?
Absolutely. I love it.
double_quack ( @double_quack@lemm.ee ) English5•2 months agoGood job! Flexible tools empower us. Keep it up and spread the love with other people!
land ( @land@lemmy.ml ) 5•2 months agoGreat job!
Learning Bash scripting and Vim is on my to-do list. 🎯
Thanks! I wrote the script in vim too! :D
I’m no vim expert but I do like it.
Drew ( @crmsnbleyd@sopuli.xyz ) 4•2 months agofind
is super arcane; most people don’t know how to use it. Congrats!(a note for the future with all programming, be careful of numbers with leading zeros, they might indicate base-8, causing your program to fail on 09)
Lettuce eat lettuce ( @Lettuceeatlettuce@lemmy.ml ) 4•2 months agoNice! Might modify this and use it for my own collection if that’s cool 🤗
Of course! Feel free!! And thank you!
electric_nan ( @electric_nan@lemmy.ml ) 3•2 months agoI 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.
LeninOnAPrayer ( @LeninOnAPrayer@lemm.ee ) English1•2 months agoIt makes it usable without typing bash. Same would apply for a python script. For example you can make a python script named with no extension and add #!/usr/bin/python to the top of the file. Bash shell sees this and knows to execute the script using that python path.Then you just include the directory in your $PATH and chmod +x the script. Then you can type $python_script instead of $python python_script.py
Mr. Framework :verified_bi: ( @akalanka@masto.es ) 2•2 months ago@harsh3466 how cute! Happy to see you learn hehe
tasankovasara ( @tasankovasara@sopuli.xyz ) 1•2 months agoI’ve had to learn heavy duty bashing for work, and happily did take the plunge. However, they also had me learn PHP and I’ll drop this as a hook and line for OP: you can do shell-script duties with PHP also, and once you hit your head on sed enough times, I hope you remember me telling this. All that string manipulation is much nicer with PHP functions, and for running shell commands there is shell_exec(). :)