journey to the cloud I : Bash by solving a Simple Organizing problem

journey to the cloud I : Bash by solving a Simple Organizing problem

Bash is fun!

Can I officially welcome you to the world of Bash Shell Scripting? I am doing it anyway!.

Problem

The vacations are over and you sure had a lot of fun!, I heard you travelled all over Nigeria, going sites seeing, trying out different delicacies like the Amala and Ewedu soup spot in Oyo State, The EdikanIkong and fufu kitchen in AkwaIbong State and you took many beautiful photographic evidences, I know the site seeing too was spectacular from climbing the Rocky mountains in Igarra in Edo State all for the amazing view of the landscape that was so worth it to playing with the white thin sands at the Meia Praia Beach in Lagos State and of course more photographic evidences. Now you are back home and lets say your vacations folders being a mess is a large understatement.

The current situation

You love to be really organised and seeing all your pictures all cramped up in one folder was a little bit unsettling so you spend hours renaming every files to begin with the location it was taken for example south-Igarra-mountains.jpg, east-ngwo-palms.jpg. After doing that you dreaded doing the next steps which includes:

  • Creating four folders named after the geographical locations in Nigeria
  • Going through every files and moving them into their respective files according to their locations. This wouldn't have been a problem if you didn't have over 500 files to go through and you wanna finish before the next decade comes, and of course you have a job to run off to the next day. All these might seem exhausting to even think about but what if I tell you can do all these in seconds with the help of a few commands in bash Shell scripting.

    Brief lesson in Bash shell

    Bash (AKA Bourne Again Shell) is a type of interpreter that processes shell commands. A shell interpreter takes commands in plain text format and calls Operating System services to do something. For example, ls command lists the files and folders in a directory. Bash is the improved version of Sh (Bourne Shell). A shell scripting is writing a program for the shell to execute and a shell script is a file or program that shell will execute. Solving this problem requires two different levels of difficulty depending on what operating system you’re running. If you’re running Linux distributions or macOS, Bash is your default shell, so it’s already installed and you can go on with the next section without having to install anything else. If you’re using a windows well that is a different story but fear not there are different ways to install bash in your computer you can find them here

common bash commands:

  • ls — List directory contents
  • echo — Prints text to the terminal window
  • touch — Creates a file
  • mkdir — Create a directory
  • grep — search
  • man — Print manual or get help for a command -pwd — Print working directory
  • cd — Change directory
  • mv — Move or rename directory
  • rmdir — Remove directory
  • locate — Locate a specific file or directory
  • less — view the contents of a text file
  • cat — Read a file, create a file, and concatenate files
  • | — Pipe: A pipe takes the standard output of one command and passes it as the input to another.
  • head — Read the start of a file
  • tail — Read the end of a file
  • chmod — Sets the file permissions flag on a file or folder
  • exit — Exit out of a directory

Now back to our problem. we would need to create our bash script. which is basically a file with a .sh and contains a string of command which can be called in the terminal. First we would change our directory/folders from home to our vacation-pictures directory where all our pictures are kept we can do this with

cd ./pictures/vacation-pictures/

from there we create a .sh file

touch picSorter.sh

Before we begin we must have a outline of what we want our bash script to do for us:

  • we want it to create four directories according to the four main geographical locations in Nigeria
  • find out all the files with each location and move it into the directory respective of the location . Now we can open our file and edit it without ever leaving the terminal using the nano picSorter.sh commands, nano is an inbuilt file editor embedded in the terminal. At the top of our picSorter file we include the following line:
#!/bin/bash

This is called the shebang line and it is notifying our terminal of the type of interpreter to be used in interpreting the file. in our case it is the bash interpreter. According to the list above the first thing we do is to create our directories:

#!/bin/bash
 mkdir Northern-Nigeria ../vacation-pictures
 mkdir Southern-Nigeria ../vacation-pictures
 mkdir Eastern-Nigeria ../vacation-pictures
 mkdir Western-Nigeria ../vacation-pictures
# creates directories in the parent files of the vacation-pictures

Our directories are created, now the following commands are run

sorting () {
        files=$( ls | grep $1 )
        for file in $files
        do
                mv $file ../$2
        done
        echo $2 all done!
}
sorting north Northern-Nigeria
sorting south Southern-Nigeria
sorting  east Eastern-Nigeria
sorting west Western-Nigeria

Lets go through each line:

sorting () {...}

This is a bash function, according to Linuxize,A Bash function is essentially a set of commands that can be called numerous times. The purpose of a function is to help you make your bash scripts more readable and to avoid writing the same code repeatedly, so instead of rewriting the same code for each zone we just pass in a function in some cases with its arguments(sorting north Northern-Nigeria),north and Northern-Nigeria these are arguments and their positioning matters when calling the fun function i.e. north = $1 and Northern-Nigeria= $2, the $1-9 are like place holders which represent the first to ninth arguments in a bash script.

files=$( ls | grep $1 )

Here we would look briefly in to variables. Sometimes we would want to remember some values or results and instead of retyping everytime we could assign them into variables, this variable simply means list ls the contents of the present working directory and using piping | search for all files containing the word $1. Save your result inside a variable called files. we can call the variable with a $files.

for file in $files... done

This is for me the most important part of the scripting this is where the iterating comes in and where 100's of files can be processed in seconds. Here for every file in files that passed the first test we are to mv $file ../$2 move each file into the directory which is the second argument then we use the done command to end the for loop circle. After everything we would love a sort of feed back to know when we are done processing so we echo $2 all done! which prints to the terminal

Western-Nigeria all done!

we are practically done with our task, if done write, you can head over to the directories using ../directory-name and ls to see our well organised files.