Just a cute pic of me
by Mike McKee
Programming

Bash is Cooler Than Python -- I Said it

66 Days of Math and Programming -- Day 50

Bad programmers waste time by not automating tasks. Idiots.

Great programmers whip up Python scripts to automate tasks. They’re the heroes… Almost.

Because legendary programmers automate tasks using Bash.

Okay, let’s be real…

I don’t know if the above is true, but after the last two days, I’m thinking it is. Automating tasks in Bash feels 10x easier and 20x cooler than doing the same thing in Python.

After learning how to create functions in a shell script, I decided to start my first mini-project in Bash. And for that, I chose something simple – creating a script that moves specific files from one directory to another.

What’s funny is that I did the same thing for my first mini-project in Python.

Life comes full circle, and I’m doing the same thing over and over. Guess I’ve gotta automate life, too, now.

Anyway, here’s the script I wrote in Bash to move files from my Downloads folder to my 66_dmp folder…

Bash script to move file between folders

I don’t want to explain every single line to you –- I ain’t no teacher –- so I’ll give you a broad overview.

At the top you see the #!/bin/bash… It’s called a “shebang.”

Now, that’s pronounced “sha-bang,” not “she-bang.” Bash ain’t creepy.

The shebang is vital to any bash script because it tells your system what language to interpret the language as. This is necessary since every scripting language uses a “.sh” suffix for the file name, so there’s no specific language defined.

On line 8, I write cd ~/Downloads because when I run this script, it’s not in the Downloads folder I want to search through. So starting the function with this tells the system to search in the correct directory.

The for loop on line 14 iterates through all items in the Downloads folder that begin with the phrase day_. And if the system finds a matching file, it uses the mv command to move the file to the 66_dmp folder.

Every time the script moves a program to the new file, we increment the counter variable by one. This keeps track of the total number of files we switch to the 66_dmp folder.

And at the end of the function, we use the counter variable to echo a string saying the total number of files moved.

The script looks confusing (especially since it’s harder to read than Python code), but it’s simple and efficient.

Here’s what the output looks like…

Output from running my bash script