Skip to content

Instantly share code, notes, and snippets.

@dzogrim
Created July 26, 2026 16:59
Show Gist options
  • Select an option

  • Save dzogrim/497dd5e901be2fcdaa06272b1d84930c to your computer and use it in GitHub Desktop.

Select an option

Save dzogrim/497dd5e901be2fcdaa06272b1d84930c to your computer and use it in GitHub Desktop.
folderizeFolders.sh - published from local Scripts source of truth
#!/usr/bin/env bash
# ==============================================================================
# Script : folderizeFolders.sh
# Description : Reorganizes the immediate subdirectories of the current working
# directory by moving each one into a companion directory whose
# name is suffixed with an underscore.
#
# Example:
#
# ./Movies
#
# becomes:
#
# ./Movies_/Movies
#
# Hidden directories and application bundles ending in ".app"
# are excluded from processing.
#
# Warning:
# This script performs real filesystem modifications after interactive
# confirmation. Although an APPLY argument and dry-run helpers are present,
# dry-run mode is not currently implemented.
#
# Requirements:
# - Bash
# - GNU find, available as "gfind"
# - GNU mv, available as "gmv"
#
# Usage:
# ./folderizeFolders.sh
# ./folderizeFolders.sh APPLY
#
# Notes:
# - Only first-level directories are processed.
# - Run the script from the directory whose subdirectories must be reorganized.
# - Review or back up the target directory before execution.
# ==============================================================================
# shellcheck disable=SC2034
readonly VERSION="0.1.0 (2015-12-21)"
# ===============================================
# Configuration
# ===============================================
NAME="${0##*/}"
#-- Customized colors
export cRST='\033[0m' \
cRed='\033[31;1m' cDRed='\033[31m' cRedBG='\033[41;1m' cDRedBG='\033[41m' \
cGreen='\033[32;1m' cDGreen='\033[32m' cGreenBG='\033[42;1m' cDGreenBG='\033[42m' \
cBlue='\033[34;1m' cDBlue='\033[34m' cBlueBG='\033[44;1m' cDBlueBG='\033[44m' \
cYellow='\033[33;1m' cDYellow='\033[33m' cYellowBG='\033[43;1m' cDYellowBG='\033[43m' \
cMagenta='\033[35;1m' cDMagenta='\033[35m' cMagentaBG='\033[45;1m' cDMagentaBG='\033[45m' \
cCyan='\033[36;1m' cDCyan='\033[36m' cCyanBG='\033[46;1m' cDCyanBG='\033[46m' \
cWhite='\033[37;1m' cBright='\033[37;1m' cGrey='\033[37m' cGreyBG='\033[47m' \
cWhiteBG='\033[47;1m' cBrightBG='\033[47;1m'
# Colored 'DRYRUN' prefix
DRYRUN="${cYellow}[DRYRUN]${cRST} "
# Find 'echo' program in PATH or use Mac Ports default location
ECHO="$( which echo )"
ECHO="${ECHO:-/opt/local/libexec/gnubin/echo}"
# Bash version check for 'echo'
[ -n "${BASH_VERSION}" ] \
&& ECHO="echo -e" \
|| ECHO="echo"
# ===============================================
# Functions
# ===============================================
LogINFO()
{
${ECHO} " ${cGreen}*${cRST} $*"
}
# ===============================================
LogWARN()
{
${ECHO} " ${cYellow}*${cRST} $*" >&2
}
# ===============================================
LogERROR()
{
${ECHO} " ${cRed}* [ERROR]${cRST} $*" >&2
}
# ===============================================
checkTools()
{
local misstools
# Check presence of necessary tools
for entry in "${@}"
do
if ! which ${entry} >/dev/null 2>&1
then
misstools="${misstools} ${entry}"
fi
done
[ -z "${misstools}" ] \
&& return \
|| {
LogERROR "Required tools could not be found: \n ${misstools}"
exit 1
}
}
# ===============================================
notDryRun()
{
[ -z "${DRYRUN}" ]
}
# ===============================================
showDryRun()
{
[ -n "${DRYRUN}" ] \
&& ${ECHO} "\n ${cRedBG}${cYellow} DRY RUN MODE : Need to APPLY ${cRST}"
}
# ===============================================
disableDryRun()
{
DRYRUN=''
}
# ===============================================
# Main
# ===============================================
#-- Checking for programs on the system
checkTools gfind gmv
[ "$1" = "APPLY" ] && disableDryRun
#-- DryRun not added yet
echo ""
read -r -p "The DryRun mode is not yet functionnal, do you want to proceed? (y/N) " RESP
if [ "${RESP}" = "y" ]; then
LogWARN "OK then... We will proceed in deletion of useless files in CCC Archive directory."
else
LogERROR "Quit."
exit 1
fi
#-- Tasks to be refactored
gfind ./ -maxdepth 1 -type d -not -name '.*' -not -name '*.app' \
-print0 | while read -d $'\0' i
do
racineCommune="$( echo "${i}" | awk '{print $1}' )"
if [ "${racineCommune}" != "." ]
then
LogINFO "${i} > ${racineCommune}_"
mkdir -p "${racineCommune}_"
mv "${i}" "${racineCommune}_"
else
LogERROR "* Error with ${racineCommune}"
fi
done
#-- Special : Dryrun info
showDryRun
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment