Back to emre.ca

A Random Walk Down GPT-4: Discovering the Wonders of Bash Scripting

Bash scripting can elevate your developer workflow by automating tasks, organizing projects, and saving time.

April 9, 2023 • Workflow Optimization • Scripting • GPT-4


A vintage Franco-Belgian comic art of an olive-skinned young man with glasses, exploring a beautiful alien world, bright sunny weather, detailed landscape --ar 16:9 --q 2 --s 750 --v 5 Caption: A vintage Franco-Belgian comic art of an olive-skinned young man with glasses, exploring a beautiful alien world, bright sunny weather, detailed landscape —ar 16:9 —q 2 —s 750 —v 5

An Inspiring Genie

As an amateur digital archivist, I often find myself working on metadata processing, categorization, and conversion for old Turkish magazines that I want to preserve for future generations.

I’m currently dealing with a huge backlog, but you can check out the magazines I’ve processed and uploaded so far on Archive.org@emreca

At one point, I needed a command-line solution to convert .rar archives to .zip archives. I figured there must be a package similar to imagemagick, ffmpeg, and so on. Instead of searching Google for a Homebrew package, I decided to ask GPT-4 for help. It recommended the following command:

brew install unar zip

But GPT-4 didn’t stop there—it also provided me with a script to perform the conversion:

#!/bin/bash

for rarfile in *.rar; do
  # Extract the RAR file
  unar -quiet "$rarfile"

  # Get the base name of the RAR file without the extension
  base_name="${rarfile%.*}"

  # Create a ZIP file with the same base name
  zip -q -r "$base_name.zip" "$base_name"

  # Remove the extracted folder
  rm -rf "$base_name"
done

I’d heard of bash scripting before and had been meaning to learn it, but I didn’t realize it could handle tasks like this. This experience piqued my interest, and now I’m eager to discover what else is possible with bash scripting.

File Management is Officially Fun

After getting a taste of the possibilities, I asked ChatGPT to generate scripts for various tasks:

#!/bin/bash

for zipfile in *.zip; do
  # Get the base name of the ZIP file without the extension
  base_name="${zipfile%.*}"

  # Rename the ZIP file with the .cbz extension
  mv "$zipfile" "$base_name.cbz"
done
#!/bin/bash

for cbrfile in *.cbr; do
  # Get the base name of the CBR file without the extension
  base_name="${cbrfile%.*}"

  # Rename the CBR file with the .rar extension
  mv "$cbrfile" "$base_name.rar"
done
#!/bin/bash

for folder in */; do
  # Remove the trailing slash from the folder name
  folder_name="${folder%/}"

  # Create a ZIP file with the same name as the folder
  zip -r "$folder_name.zip" "$folder_name"
done

Tackling the Little Black Holes Known as node_modules

After experiencing the power of the scripts I shared earlier, I decided to investigate how others utilize the superpower of bash scripting. I browsed through Reddit and Google, only to discover that most scripting enthusiasts are system admins and Linux aficionados. Their use cases didn’t quite align with my needs as a front-end developer working at a creative agency.

During a typical work day, I sometimes need to switch between as many as eight different projects, each requiring a pull from GitHub. I initially tried to keep my projects folder organized, but it eventually became unmanageable, and I stopped trying. This led to more than 50 projects accumulating in the folder, each with its own little black hole known as a node_modules folder.

My projects folder had ballooned to an astounding 24GB—and that’s just for work-related files!

While searching Google, I came across this website: Bash One-Liners

It inspired me to create a script that would remove the node_modules folders scattered throughout my projects folder.

I asked GPT-4 to write a script for me (I didn’t use the one on the website because it had 0 points—though it’s probably fine).

The following script recursively removes node_modules folders:

#!/bin/bash

find . -type d -name "node_modules" -exec rm -rf {} +

echo "All node_modules folders have been removed."

After deleting all node_modules folders, the projects folder size is decreased to 13GB!

Determining Which Projects to Delete Entirely

In addition to the node_modules folders, I also wanted to delete any projects that I no longer needed. However, this proved challenging due to occasional uncommitted changes that can easily go unnoticed. Some projects are left waiting for client feedback and can be forgotten, leaving these uncommitted changes behind. Deleting them might cause unwanted issues.

With the insights I gained in previous sections, I asked GPT-4 to provide me with a script that detects uncommitted changes and compiles a list in a text file containing the names of all projects with uncommitted changes.

#!/bin/bash

GITHUB_DIR="$HOME/Documents/GitHub"
OUTPUT_FILE="$GITHUB_DIR/uncommitted_repos.txt"

# Clear or create the output file
echo "" > "$OUTPUT_FILE"

# Function to check for uncommitted changes
check_uncommitted_changes() {
  git -C "$1" diff --quiet --cached --exit-code
  local staged_changes=$?

  git -C "$1" diff --quiet --exit-code
  local unstaged_changes=$?

  if [ $staged_changes -ne 0 ] || [ $unstaged_changes -ne 0 ]; then
    echo "$(basename "$1")" >> "$OUTPUT_FILE"
  fi
}

# Loop through subdirectories in the GitHub folder
for dir in "$GITHUB_DIR"/*; do
  if [ -d "$dir" ] && [ -d "$dir/.git" ]; then
    check_uncommitted_changes "$dir"
  fi
done

echo "Uncommitted repos have been listed in $OUTPUT_FILE."

Using this script, I was able to investigate the uncommitted changes, then commit or restore them as needed.

Next, I asked ChatGPT to create a script that lists all the projects within the projects folder in descending order based on their respective sizes.

#!/bin/bash
# Usage: ./list_folders.sh /path/to/folder

target_folder=$1

if [ -z "$target_folder" ]; then
  echo "Please provide a folder path as an argument."
  exit 1
fi

if [ ! -d "$target_folder" ]; then
  echo "The provided path is not a directory or does not exist."
  exit 1
fi

du -sh "$target_folder"/* | sort -rh

Armed with this information, I could delete the largest projects and substantially reduce the size of my projects folder.

Thanks to these scripts, my projects folder shrank from a whopping 24GB to a manageable 4GB.


While all these tasks could be accomplished using NodeJS, I find that using bash is more versatile. It’s quicker to create and execute the scripts. Of course, you could also consider running these bash scripts through a NodeJS project, but I found that to be unnecessary for my use case.

I believe that bash scripting is a valuable skill worth investing time in. It can unlock new superpowers, helping you become a more efficient and productive developer—perhaps even a 10x developer!


In conclusion, I was so inspired and impressed by my experience with bash scripting that I decided to ask ChatGPT to write a blog post based on our entire conversation.

Thanks for reading, and I hope you enjoyed it!