Tuesday, October 28, 2025

Programming Scripting Automation: PART I (INTRODUCTION) Automatically Copy Multiple Folders In Linux Using Bash

AutoFolderCopy.sh (code)

    Hello there! I hope everyone’s doing well and staying healthy. Today, I’m presenting a demonstration of AutoFolderCopy.sh, a Bash script that automates copying a list of folders from the current directory to a destination path with its contents and sub folders. This is a real world coding demo that shows how I actually write code, make mistakes, fix typos, and debug along the way.

Alright, let’s get straight to it!


Script Design


The functions

Since I come from an OOP programming background, I’m used to separating code into classes, methods, and modules. In Bash scripting, however, it’s more procedural, so it only supports functions. To keep things clean and readable, I organize my code into functions instead of writing one big "spaghetti" script.


Header File


Header

The header contains metadata about the script/code and a short description of what the script/code does. It helps anyone reading the script/code to quickly understand its purpose and usage. It makes the script/code more informative and professional.


Configuration Variables


Configuration variables 

Configuration variables store the basic settings, data, arrays, or any other values used throughout the script or module.

They help centralize important settings in one place, so when I need to change something, I only update these variables and all related parts of the code follow automatically.

So in other words, it centralizing my variables makes my scripts/codes easier to maintain and reduces the risk of mistakes.


Script Functions


Functions

I come from an OOP background, so I naturally separate my code into MVC and modular designs. In my workflow, I usually have internal modules (used within the code) and external modules (called from the terminal). You can think of these modules like classes, each holding specific sections of code.

In Bash, however, it’s a procedural language, so it only supports functions. Based on my experience, I design scripts with well structured functions to keep the code organized, readable, and maintainable. Even in a procedural language like Bash, modularizing the code with functions helps prevent "spaghetti" scripts.

There are three main functions in my script:
  1. Main execution function: copy_folders_from_list()
  2. Log writer: write_log()
  3. Summary section: summary_section()
All these functions work together to make the script clean, organized, and easy to understand. They help separate responsibilities so the code is easier to read, maintain, and debug.
 
 

Executor Function


Executor function

This is an important function without calling it, the script won’t do anything. It’s not a bug; it’s like getting into a car without starting the engine it just won’t go anywhere.

The executor function runs the main task of the script: moving folders and their contents to the destination directory or disk.


Finally, The End

In the next post, I’ll show you how I write the script in real time. I’ll explain it, walk you through the code, and demonstrate it running successfully.

Stay tuned for the next part! Thanks for reading, and have a great day!