Skip to content

Instantly share code, notes, and snippets.

@NecRaul
Created November 30, 2025 03:16
Show Gist options
  • Select an option

  • Save NecRaul/887424d3eeccb0dbd2e7f5b834a406c5 to your computer and use it in GitHub Desktop.

Select an option

Save NecRaul/887424d3eeccb0dbd2e7f5b834a406c5 to your computer and use it in GitHub Desktop.
Jellyfin_sort_folders_by_name
#!/bin/bash
JELLYFIN_URL="${JELLYFIN_URL:-http://localhost:8096}"
CLIENT="${CLIENT:-emby}"
# You can get it by going to the network tab in Web Developer Tools,
# Refreshing and searching for Users or Items endpoints
# Example: API_BASE/Users/USER_ID/Items
USER_ID="${USER_ID:-}"
# Same as the previous example but it will be in the Authorization
# field of the request labeled "Token"
# Example: MediaBrowser Client="MEDIABROWSER", Device="DEVICE", DeviceId="DEVICE_ID", Version="VERSION", Token="TOKEN"
TOKEN="${TOKEN:-}"
# You can get it by visiting library's main/first page and looking at the URL
# Example: http://localhost:8096/web/#/list.html?parentId=PARENT_ID&serverId=SERVER_ID
FIRST_PARENT_ID="${FIRST_PARENT_ID:-}"
declare -a ALL_IDS
declare -a PARENT_IDS=("$FIRST_PARENT_ID")
while [ ${#PARENT_IDS[@]} -gt 0 ]; do
NEXT_IDS=()
for PARENT_ID in "${PARENT_IDS[@]}"; do
RESPONSE=$(curl -s "$JELLYFIN_URL/Users/$USER_ID/Items?ParentId=$PARENT_ID" \
-H "Accept: application/json" \
-H "Authorization: MediaBrowser Client=\"Jellyfin Web\", Token=$TOKEN")
IDS=$(echo "$RESPONSE" | jq -r '.Items[] | select(.IsFolder == true) | .Id')
for ID in $IDS; do
if [[ ! " ${ALL_IDS[*]} " =~ " $ID " ]]; then
ALL_IDS+=("$ID")
NEXT_IDS+=("$ID")
fi
done
done
PARENT_IDS=("${NEXT_IDS[@]}")
done
ALL_IDS=("$FIRST_PARENT_ID" "${ALL_IDS[@]}")
custom_prefs=$(jq -n '{
chromecastVersion: "stable",
skipForwardLength: "30000",
skipBackLength: "10000",
enableNextVideoInfoOverlay: "False",
tvhome: null,
dashboardTheme: null
}')
for id in "${ALL_IDS[@]}"; do
custom_prefs=$(echo "$custom_prefs" | jq --arg key1 "items-$id-Folder-sortby" --arg val1 "SortName" \
--arg key2 "items-$id-Folder-sortorder" --arg val2 "Ascending" \
'. + {($key1): $val1, ($key2): $val2}')
done
payload=$(jq -n \
--arg id "$USER_ID" \
--arg sortBy "SortName" \
--arg scrollDirection "Horizontal" \
--arg sortOrder "Ascending" \
--arg client "$CLIENT" \
--argjson showBackdrop true \
--argjson rememberIndexing false \
--argjson rememberSorting false \
--argjson showSidebar false \
--argjson primaryImageHeight 250 \
--argjson primaryImageWidth 250 \
--argjson prefs "$custom_prefs" \
'{
Id: $id,
SortBy: $sortBy,
RememberIndexing: $rememberIndexing,
PrimaryImageHeight: $primaryImageHeight,
PrimaryImageWidth: $primaryImageWidth,
CustomPrefs: $prefs,
ScrollDirection: $scrollDirection,
ShowBackdrop: $showBackdrop,
RememberSorting: $rememberSorting,
SortOrder: $sortOrder,
ShowSidebar: $showSidebar,
Client: $client
}')
curl -s -X POST "$JELLYFIN_URL/DisplayPreferences/usersettings?userId=$USER_ID&client=$CLIENT" \
-H "Content-Type: application/json" \
-H "Authorization: MediaBrowser Client=\"Jellyfin Web\", Token=$TOKEN" \
--data-binary "$payload"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment