What is a Terminal/Shell?

If you’ve already had a go at programming, there’s a good chance you’ve familiarized yourself with the command-line interface (CLI or Shell).

This is an example of the macOS CLI, Terminal.

Command-lines are the most powerful tool you have on your computer. It allows you to go places and do stuff otherwise not allowed. The reason why programmers are introduced to command-lines in the first place is usually because they have compilers built into them. A compiler is what translates your code into instructions your computer can understand and run. We won’t go into detail on compilers right now because Python, our current language of choice, doesn’t need to be compiled. You only need to create a python file like so:


Then, using the bash (the most commonly used shell), you navigate to the files’ location and type

python3 test.py

The shell will now run your program like a charm (that is, provided you’ve written flawless code). Visit codecademy to take their course on using the command-line for free.

When you get really familiar with the command-line, you’ll find yourself writing scripts to automate tasks on your computer. Let me give you a real world example: A few weeks ago I decided to move all my personal pictures onto one computer. So I’m sitting there looking through my MacBook Pro, discovering that the iPhoto app has stored all the pictures of my old iPhone. Problem was that iPhoto had a really bad way of storing these. Inside a directory named “Master” there was three directories: “2013”, “2014” and “2015”. All seems fine, right? Well, I went on and to my horror, discovered that there were about ten sub-directories inside each of these three directories. You’re probably thinking “thirty folders containing pictures? It could’ve been way worse.” and you’re right. What if I told you that each single one of these directories contained about a hundred more? Well yes. It turned out that iPhoto had made a directory for each day a picture was taken. Lets say I’d taken a random picture on an uneventful day. iPhoto had now made an entire directory only for storing this one picture. As you can imagine, this would’ve taken well over a day to go through. I however wasn’t about to waste a single hour doing this.

“What makes the greatest programmers is laziness”

Entering the bash-scripts. Remember when I told you how powerful the command-line can be? Being a programming wizard lets you make sets of commands for the shell that can do anything you want! You can probably see where this is going, but before I reveal my magic trick, let me drop a little knowledge on you. What makes the greatest programmers is laziness. What would make me say that you ask? A lazy programmer never repeats his code. He instead writes code that can be used over and over again. Code designed with reusability in mind. Code like that usually takes more time to construct, but ends up leading to less work further down the line. In my case this meant writing a script that could go through every sub-directory of Master regardless of name and size. So after sketching a quick flowchart, I got to work.

My script would look inside the Master directory to see if there was any directories within it. It would then look inside the first one, in this case the 2013 directory, to see if there was any directories inside. It would keep on doing so until it was at the end of the line. Now it looked for any picture or video file. Prior to running this script I had made a directory in my Home directory and designated it as the target destination, meaning that any time my script discovered any media files, it would move them to this folder. When the directory was empty, it jumped out and deleted it. Now having a new “first” directory it would just repeat these steps until it had gone through every single directory inside Master, deleting the sub-directories in the process.

This probably took some time considering there was well over a thousand directories to go through, right? Well. Do you still remember the “powerful part I mentioned? I’ll proudly have you know it only took about two seconds for the script to complete its task. Admittedly the files were only moved to a new location on the same storage device, but I think we can agree that two seconds’ still amazingly fast for this amount of media files. So that’s my tale of how I turned a day’s work into thirty minutes using the command-line.

So, in conclusion, learn how to use the command-line as soon as possible. It’s a must for every programmer. Knowing it, and how to write shell scripts gives you limitless control. This is my awesome, but potentially highly dangerous bash-script


DIR=$(ls -d */|head -n 1)

while true; do
    if [ "$(ls -A */)" ]; then
        cd $DIR
        if [ "$(ls)" ]; then
            if [ "$(pwd)" == "$startDIR" ]; then
                mv * ~/Pictures/
            cd ../
            rm -r $(ls -d */|head -n 1)
    DIR=$(ls -d */|head -n 1)

Until next time, keep coding.

Learn how to use the command-line at codecademy.com

Leave a Reply

Your email address will not be published. Required fields are marked *