Automating Recursive Folder and File Creation in Linux with Shell Script

Introduction

In software development, we often need to make several folders and files for testing. Making these by hand takes a lot of time and can lead to mistakes. This article shows a smart way: using Linux Shell Script to automatically create complex folder structures easily. Let's look more into how this works.

Script

The following script creates 2 levels of folders with 3 folders in each level. And each folder will contain 4 files. Adjust these numbers in the script according to the requirements.

#!/bin/bash

# Function to create folders and files recursively
createFoldersAndFiles() {
    basePath=$1
    level=$2

    # Create 3 folders at the current level
    for i in {1..3}
    do
        folderName="Level${level}-Folder${i}"
        folderPath="${basePath}/${folderName}"
        mkdir -p "$folderPath"

        # Create 4 sample text files in each folder with level info in the name
        for j in {1..4}
        do
            fileName="SampleFile-Level${level}-Folder${i}-File${j}.txt"
            filePath="${folderPath}/${fileName}"
            echo "This is a sample text file at Level${level}Folder${i}File${j}" > "$filePath"
        done

        # Create child folders if not at the last level
        if [ $level -lt 2 ]
        then
            createFoldersAndFiles "$folderPath" $((level + 1))
        fi
    done
}

# Get the current date and time
currentDateTime=$(date +"%Y%m%d-%H%M%S")

# Construct the folder name
folderName="root-${currentDateTime}"

# Define the base path where the folders will be created
basePath="${PWD}/${folderName}"

# Create the parent folder if it doesn't exist
mkdir -p "$basePath"

# Create the folder structure
createFoldersAndFiles "$basePath" 1

echo "Folder structure created successfully."


Running the Script

In this example I stored the code under the folder /home/ubuntu/FolderAndFiles and Shell Script is named as create-folders-files.sh. After running the script, it will create a root level folder in the same folder where Script is running with the name root-yyyymmdd-hhmmss. For example: root-20170603-200121.

List the files in the folder where the script is stored

ubuntu@vmsys:~/FolderAndFiles$ find .

Result

.
./create-folders-files.sh

Run the script

ubuntu@vmsys:~/FolderAndFiles$ sudo chmod +x create-folders-files.sh
ubuntu@vmsys:~/FolderAndFiles$ sudo ./create-folders-files.sh

Result
Folder structure created successfully.

List the created folders and files

ubuntu@vmsys:~/FolderAndFiles$ find .

Result:

.
./create-folders-files.sh
./root-20170603-200121
./root-20170603-200121/Level1-Folder1
./root-20170603-200121/Level1-Folder1/Level2-Folder3
./root-20170603-200121/Level1-Folder1/Level2-Folder3/SampleFile-Level2-Folder3-File2.txt
./root-20170603-200121/Level1-Folder1/Level2-Folder3/SampleFile-Level2-Folder3-File4.txt
./root-20170603-200121/Level1-Folder1/Level2-Folder3/SampleFile-Level2-Folder3-File1.txt
./root-20170603-200121/Level1-Folder1/Level2-Folder3/SampleFile-Level2-Folder3-File3.txt
./root-20170603-200121/Level1-Folder1/SampleFile-Level1-Folder1-File2.txt
./root-20170603-200121/Level1-Folder1/SampleFile-Level1-Folder1-File4.txt
./root-20170603-200121/Level1-Folder1/Level2-Folder2
./root-20170603-200121/Level1-Folder1/Level2-Folder2/SampleFile-Level2-Folder2-File4.txt
./root-20170603-200121/Level1-Folder1/Level2-Folder2/SampleFile-Level2-Folder2-File2.txt
./root-20170603-200121/Level1-Folder1/Level2-Folder2/SampleFile-Level2-Folder2-File3.txt
./root-20170603-200121/Level1-Folder1/Level2-Folder2/SampleFile-Level2-Folder2-File1.txt
./root-20170603-200121/Level1-Folder1/Level2-Folder1
./root-20170603-200121/Level1-Folder1/Level2-Folder1/SampleFile-Level2-Folder1-File3.txt
./root-20170603-200121/Level1-Folder1/Level2-Folder1/SampleFile-Level2-Folder1-File2.txt
./root-20170603-200121/Level1-Folder1/Level2-Folder1/SampleFile-Level2-Folder1-File4.txt
./root-20170603-200121/Level1-Folder1/Level2-Folder1/SampleFile-Level2-Folder1-File1.txt
./root-20170603-200121/Level1-Folder1/SampleFile-Level1-Folder1-File1.txt
./root-20170603-200121/Level1-Folder1/SampleFile-Level1-Folder1-File3.txt

Conclusion:

By harnessing the power of Shell Script, developers can automate the creation of sample folder structures, streamlining their workflow and enhancing productivity.

Post a comment