Skip to content

Instantly share code, notes, and snippets.

@mateusreis
Last active April 22, 2026 17:53
Show Gist options
  • Select an option

  • Save mateusreis/be91d8c5d37f070e1d8bb6924545ee49 to your computer and use it in GitHub Desktop.

Select an option

Save mateusreis/be91d8c5d37f070e1d8bb6924545ee49 to your computer and use it in GitHub Desktop.
Lists comic files across folders and subfolders
#!/usr/bin/env bash
# list_comics.sh — Lists comics files across folders and subfolders
# Usage: ./list_comics.sh [directory] [output.txt]
# --- Config ---
COMICS_DIR="${1:-/mnt/comics}"
OUTPUT_FILE="${2:-comics_list.txt}"
EXTENSIONS=("cbr" "cbz" "cb7" "cbt" "pdf" "epub" "djvu")
# --- Validation ---
if [[ ! -d "$COMICS_DIR" ]]; then
echo "Error: directory not found: $COMICS_DIR"
echo "Usage: $0 [directory] [output.txt]"
exit 1
fi
# --- Build extension pattern for find ---
find_args=()
for ext in "${EXTENSIONS[@]}"; do
find_args+=(-o -iname "*.${ext}")
done
# Remove the leading "-o" from the array
find_args=("${find_args[@]:1}")
# --- Output file header ---
{
echo "======================================================"
echo " COMICS LIBRARY"
echo " Directory: $(realpath "$COMICS_DIR")"
echo " Generated: $(date '+%Y-%m-%d %H:%M:%S')"
echo "======================================================"
echo ""
} > "$OUTPUT_FILE"
# --- Process folder by folder ---
TOTAL_FILES=0
CURRENT_FOLDER=""
while IFS= read -r -d '' file; do
folder=$(dirname "$file")
name=$(basename "$file")
folder_rel="${folder#$COMICS_DIR/}"
# Print folder header when folder changes
if [[ "$folder" != "$CURRENT_FOLDER" ]]; then
CURRENT_FOLDER="$folder"
echo "" >> "$OUTPUT_FILE"
echo "📁 $folder_rel" >> "$OUTPUT_FILE"
echo "------------------------------------------------------" >> "$OUTPUT_FILE"
fi
echo " $name" >> "$OUTPUT_FILE"
((TOTAL_FILES++))
done < <(find "$COMICS_DIR" \( "${find_args[@]}" \) -type f -print0 | sort -z)
# --- Footer ---
{
echo ""
echo "======================================================"
echo " TOTAL FILES: $TOTAL_FILES"
echo "======================================================"
} >> "$OUTPUT_FILE"
echo "✅ Done! $TOTAL_FILES file(s) listed in: $OUTPUT_FILE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment