Skip to content
ZNZN Hub

$ run organize-files

working on it…

✓ all done

  • Sorts into 9 type folders
  • Asks before touching sub-folders
  • Never overwrites, renames clashes
  • Skips hidden folders, apps and partial downloads
Install

Up and running in a minute

  1. 01

    Download the version for your computer, or copy it from the source below and save it with the same file name.

  2. 02

    Move the file into the folder you want to clean up. On macOS, run chmod +x Organize-Files-Mac.command once in Terminal, then right-click and choose Open the first time.

  3. 03

    Double-click it and answer the questions. On Windows, if SmartScreen warns, click More info, then Run anyway.

Source

Read it, copy it, run it

The full script is below. Copy it in one click or download the file. Nothing is minified or hidden.

Organize-Files-Mac.commandBash · 299 linesDownload
#!/bin/bash
# Organize Files (Mac)
# Is file ko kisi bhi folder mein rakh kar double-click karein.
# Us folder ki files type ke hisaab se folders mein chali jayengi
# (Images, Videos, Audio, Documents, Archives, Installers, Code, Fonts, Others).
# Jo folder pehle se nahi bana, woh khud ban jayega.
# Agar folder mein aur (extra) folders bhi hon to script poochta hai:
#   - unki files bhi organize karni hain ya nahi
#   - sab folders, ya sirf kuch chune hue
#   - andar ke sub-folders bhi ya nahi
#   - kaam ke baad khaali folders delete karne hain ya nahi
# Agar same naam ki file pehle se ho to naya naam "name (1).ext" rakha jata hai.
# Hidden folders aur Mac apps/packages (.app, .photoslibrary waghera) ko haath nahi lagaya jata.

cd "$(dirname "$0")" || { echo "Folder nahi khul saka."; exit 1; }

SELF="$(basename "$0")"
OTHER_SCRIPT="Organize-Files-Windows.bat"
CATEGORY_FOLDERS="images videos audio documents archives installers code fonts others"
BUNDLE_EXTS="app bundle framework plugin kext appex xpc prefpane qlgenerator mdimporter saver xcodeproj xcworkspace playground xcassets lproj photoslibrary musiclibrary tvlibrary imovielibrary fcpbundle logicx band rtfd pages numbers key pkg mpkg sparsebundle"

PRUNE=( -name '.*' )
for e in $BUNDLE_EXTS; do
  PRUNE+=( -o -iname "*.$e" )
done

lower() {
  printf '%s' "$1" | tr '[:upper:]' '[:lower:]'
}

category_for() {
  case "$1" in
    jpg|jpeg|png|gif|bmp|tif|tiff|webp|heic|heif|svg|ico|raw|cr2|cr3|nef|arw|dng|orf|rw2|psd|ai|eps|avif)
      echo "Images" ;;
    mp4|mov|avi|mkv|wmv|flv|webm|m4v|mpg|mpeg|3gp|3g2|mts|m2ts|vob|ogv)
      echo "Videos" ;;
    mp3|wav|aac|flac|ogg|oga|m4a|wma|aiff|aif|opus|amr|mid|midi)
      echo "Audio" ;;
    pdf|doc|docx|txt|rtf|odt|xls|xlsx|xlsm|csv|ods|ppt|pptx|odp|pages|numbers|key|md|epub|mobi|tex)
      echo "Documents" ;;
    zip|rar|7z|tar|gz|tgz|bz2|xz|zst|iso)
      echo "Archives" ;;
    dmg|pkg|exe|msi|apk|aab|deb|rpm|appimage|ipa)
      echo "Installers" ;;
    py|js|ts|jsx|tsx|html|htm|css|scss|json|xml|yml|yaml|sh|bat|ps1|java|kt|kts|c|cpp|h|hpp|cs|go|rb|php|swift|sql|rs|dart|vue)
      echo "Code" ;;
    ttf|otf|woff|woff2)
      echo "Fonts" ;;
    *)
      echo "Others" ;;
  esac
}

extension_of() {
  case "$1" in
    *.*) lower "${1##*.}" ;;
    *) printf '' ;;
  esac
}

should_skip() {
  local name="$1" ext="$2"
  [ "$name" = "$SELF" ] && return 0
  [ "$name" = "$OTHER_SCRIPT" ] && return 0
  [ "$name" = $'Icon\r' ] && return 0
  case "$ext" in
    crdownload|part|partial|download|tmp) return 0 ;;
  esac
  return 1
}

is_category_folder() {
  case " $CATEGORY_FOLDERS " in
    *" $(lower "$1") "*) return 0 ;;
  esac
  return 1
}

is_bundle() {
  local ext
  ext="$(extension_of "$1")"
  [ -n "$ext" ] || return 1
  case " $BUNDLE_EXTS " in
    *" $ext "*) return 0 ;;
  esac
  return 1
}

# Folder ki organize hone wali files null-separated print karta hai.
# $2 = yes ho to andar ke sub-folders bhi.
list_files() {
  local dir="$1" recursive="$2" f base
  local depth=()
  [ "$recursive" = "yes" ] || depth=( -maxdepth 1 )
  while IFS= read -r -d '' f; do
    base="$(basename "$f")"
    should_skip "$base" "$(extension_of "$base")" && continue
    printf '%s\0' "$f"
  done < <(find "$dir" -mindepth 1 "${depth[@]}" \( "${PRUNE[@]}" \) -prune -o -type f -print0)
}

count_files() {
  list_files "$1" "$2" | tr -cd '\0' | wc -c | tr -d ' '
}

unique_destination() {
  local dir="$1" name="$2" base ext candidate n=1
  case "$name" in
    *.*) base="${name%.*}"; ext=".${name##*.}" ;;
    *) base="$name"; ext="" ;;
  esac
  candidate="$dir/$name"
  while [ -e "$candidate" ]; do
    candidate="$dir/$base ($n)$ext"
    n=$((n + 1))
  done
  printf '%s' "$candidate"
}

ask_yes_no() {
  local answer
  read -r -p "$1" answer
  case "$(lower "$answer")" in
    "") [ "$2" = "yes" ] ;;
    y*|h*) return 0 ;;
    *) return 1 ;;
  esac
}

already_selected() {
  local s
  for s in "${SELECTED[@]}"; do
    [ "$s" = "$1" ] && return 0
  done
  return 1
}

# Folder khaali ho (sirf .DS_Store bacha ho tab bhi) to delete karta hai.
remove_if_empty() {
  local dir="$1" rest
  rest="$(ls -A "$dir" 2>/dev/null | grep -v -x -e '.DS_Store' -e $'Icon\r')"
  [ -n "$rest" ] && return 1
  rm -f "$dir/.DS_Store" "$dir/Icon"$'\r' 2>/dev/null
  rmdir "$dir" 2>/dev/null
}

finish() {
  echo
  read -r -p "Band karne ke liye Enter dabayein... " _
  exit 0
}

EXTRA=()
for d in */; do
  d="${d%/}"
  [ -d "$d" ] || continue
  [ -L "$d" ] && continue
  is_category_folder "$d" && continue
  is_bundle "$d" && continue
  EXTRA+=("$d")
done

TOP_COUNT="$(count_files "." "no")"

echo "=============================================="
echo "  Organize Files"
echo "=============================================="
echo "Folder: $PWD"
echo
echo "Is folder ki seedhi files: $TOP_COUNT"

SELECTED=()
RECURSIVE="yes"
CLEANUP="no"

if [ ${#EXTRA[@]} -gt 0 ]; then
  echo
  echo "Is folder mein yeh extra folders bhi hain:"
  i=1
  for d in "${EXTRA[@]}"; do
    printf '  %2d) %s  (%s files)\n' "$i" "$d" "$(count_files "$d" "yes")"
    i=$((i + 1))
  done
  echo
  echo "In folders ki files bhi organize karni hain?"
  echo "  N = Nahi, sirf is folder ki seedhi files (default)"
  echo "  A = Haan, sab folders"
  echo "  S = Kuch folders main khud chunun"
  read -r -p "Choice [N/A/S]: " choice

  case "$(lower "$choice")" in
    a*|h*)
      SELECTED=("${EXTRA[@]}")
      ;;
    s*)
      read -r -p "Folder numbers likhein (jaise: 1 3 4 ya 1,3): " numbers
      set -f
      for n in $(printf '%s' "$numbers" | tr ',' ' '); do
        case "$n" in
          *[!0-9]*)
            echo "  \"$n\" number nahi hai, chhod diya."
            continue ;;
        esac
        if [ "$n" -ge 1 ] && [ "$n" -le ${#EXTRA[@]} ]; then
          pick="${EXTRA[$((n - 1))]}"
          already_selected "$pick" || SELECTED+=("$pick")
        else
          echo "  $n list mein nahi hai, chhod diya."
        fi
      done
      set +f
      ;;
  esac

  if [ ${#SELECTED[@]} -gt 0 ]; then
    echo
    echo "Chune gaye folders:"
    for d in "${SELECTED[@]}"; do
      echo "  - $d"
    done
    echo
    ask_yes_no "In folders ke andar wale sub-folders ki files bhi? [Y/n]: " "yes" || RECURSIVE="no"
    ask_yes_no "Kaam ke baad jo folders khaali ho jayein, unhein delete kar dein? [Y/n]: " "yes" && CLEANUP="yes"
  else
    echo "Koi extra folder nahi chuna, sirf seedhi files organize hongi."
  fi
fi

FILES=()
while IFS= read -r -d '' f; do
  FILES+=("$f")
done < <(
  list_files "." "no"
  for d in "${SELECTED[@]}"; do
    list_files "$d" "$RECURSIVE"
  done
)

echo
if [ ${#FILES[@]} -eq 0 ]; then
  echo "Organize karne ke liye koi file nahi mili."
  finish
fi

echo "Total ${#FILES[@]} files type ke hisaab se folders mein move hongi."
read -r -p "Shuru karne ke liye Enter dabayein (cancel karna ho to window band kar dein)... " _
echo

moved=0
failed=0
for f in "${FILES[@]}"; do
  f="${f#./}"
  name="$(basename "$f")"
  category="$(category_for "$(extension_of "$name")")"

  if [ -e "$category" ] && [ ! -d "$category" ]; then
    echo "  SKIP  $f  (\"$category\" naam ki file pehle se hai, folder nahi ban sakta)"
    failed=$((failed + 1))
    continue
  fi
  if [ ! -d "$category" ] && ! mkdir "$category"; then
    echo "  FAIL  $f  (folder \"$category\" nahi ban saka)"
    failed=$((failed + 1))
    continue
  fi

  dest="$(unique_destination "$category" "$name")"
  if mv -n "$f" "$dest"; then
    echo "  $category/  <-  $f"
    moved=$((moved + 1))
  else
    echo "  FAIL  $f"
    failed=$((failed + 1))
  fi
done

removed=0
if [ "$CLEANUP" = "yes" ]; then
  for d in "${SELECTED[@]}"; do
    if [ "$RECURSIVE" = "yes" ]; then
      DIRS=()
      while IFS= read -r -d '' sub; do
        DIRS+=("$sub")
      done < <(find "$d" -mindepth 1 \( "${PRUNE[@]}" \) -prune -o -type d -print0)
      i=$((${#DIRS[@]} - 1))
      while [ $i -ge 0 ]; do
        remove_if_empty "${DIRS[$i]}" && removed=$((removed + 1))
        i=$((i - 1))
      done
    fi
    remove_if_empty "$d" && removed=$((removed + 1))
  done
fi

echo
echo "Ho gaya: $moved files move hui."
[ $removed -gt 0 ] && echo "$removed khaali folders delete kiye."
[ $failed -gt 0 ] && echo "$failed files move nahi ho sakin (upar dekhein)."
finish
Overview

What Organize Files does

A double-click folder cleaner for macOS and Windows. Put the script inside any folder (Downloads, Desktop, a phone backup) and run it: files are sorted into Images, Videos, Audio, Documents, Archives, Installers, Code, Fonts and Others, creating each folder only when it is needed. If the folder has sub-folders, it lists them with file counts and asks whether to organize none, all or a few you pick, whether to go into nested sub-folders, and whether to delete folders that end up empty. Nothing is overwritten: a clash becomes "name (1).ext". Hidden folders, .git, macOS app bundles and photo libraries, Windows shortcuts and junctions, half-finished downloads and the script itself are always left alone. Prompts are in Roman Urdu.

Release history

  1. v1.0.0

    Organize Files added to the hub

    Drop one file into a messy folder, double-click it, and every file lands in a folder by type.