Some useful Bash Scripts

A curated list of bash scripts to automate a few stuff to make my life (or could be yours) easy.

1. Update local GIT repositories

A bash script that iterates through directories and runs git pull in those that are Git repositories:

# Loop through each directory in the current directory
for dir in ./*; do
  # Check if it's a directory (avoid hidden files)
  if [[ -d "$dir" && ! "$dir" == .git ]]; then
    # Change directory to the current one
    pushd "$dir" >/dev/null 2>&1

    # Check if it's a Git repository (presence of .git directory)
    if [[ -d .git ]]; then
      echo "Updating $dir..."
      # Pull changes from remote repository
      git pull
    fi

    # Move back to the previous directory
    popd >/dev/null 2>&1
  fi
done

2. Kill port

killport () {
  PID=$(sudo lsof -t -i:$1)
  sudo kill -9 ${PID}
}

Usage:

$ killport 8080

3. Clean up node_modules to free disk space

npx npkill

Last updated on: 2024-06-28