Improvements

This commit is contained in:
2025-11-27 04:22:46 +00:00
parent dba00da901
commit 97ba5eac6a
7 changed files with 141 additions and 10 deletions
+110
View File
@@ -0,0 +1,110 @@
# CLAUDE.md
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
## Repository Overview
This repository contains bash scripts for image processing operations, specifically for:
- Resizing large JPEG images using ImageMagick
- Converting RAR archives to ZIP format
- Processing images inside ZIP archives
- Recursive directory operations
## Architecture
### Script Organization
The scripts follow a modular pattern with three tiers:
1. **Core scripts** - Perform operations in current directory only:
- `images_resize.sh` - Resizes JPEGs in current directory
- `rar2zip.sh` - Converts RAR to ZIP (takes file arguments)
- `zip_images_resize.sh` - Resizes JPEGs inside ZIP files (takes file arguments)
2. **Recursive scripts** - Traverse directories and call core scripts:
- `images_resize_recursive.sh` - Calls `~/images_resize.sh` in each subdirectory
- `zip_images_resize_recursive.sh` - Finds all ZIPs and calls `zip_images_resize.sh`
- `zip_images_recursive.sh` - Recursively creates ZIPs from JPEGs in directory trees
3. **Wrapper scripts** - Execute core scripts on specific paths:
- `rar2zip_images.sh` - Runs `rar2zip.sh` on `/media/rewt/media/images/`
- `rar2zip_incomplete.sh` - Runs `rar2zip.sh` on `/media/rewt/media/incomplete/`
### Key Design Patterns
**Script Path Resolution**: Recursive and wrapper scripts reference scripts using relative paths with `SCRIPT_DIR`:
```bash
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
"$SCRIPT_DIR/images_resize.sh"
```
**ImageMagick Path**: Uses custom ImageMagick installation at `~/Applications/magick`
**Temporary Storage**: Uses RAM disk (`/dev/shm/`) for temporary file operations to improve performance
**Cleanup Handling**: Scripts implement trap-based cleanup functions to remove temp directories on exit/error
**File Safety Checks**: Before deleting originals, scripts verify output files exist and are non-empty
## Common Commands
### Resize Images
In current directory:
```bash
./images_resize.sh
```
Recursively through subdirectories:
```bash
./images_resize_recursive.sh
```
Image resize parameters: `-resize 8000000@@\>` (max 8MP), `-quality 85%`, `-sampling-factor 4:2:0`
### Convert RAR to ZIP
Single/multiple files:
```bash
./rar2zip.sh file1.rar file2.rar
```
All RARs in specific paths:
```bash
./rar2zip_images.sh # /media/rewt/media/images/
./rar2zip_incomplete.sh # /media/rewt/media/incomplete/
```
Compression: Uses 7z with `-mx=9` (maximum compression)
### Process ZIP Archives
Resize images inside ZIP files:
```bash
./zip_images_resize.sh archive1.zip archive2.zip
```
Recursively process all ZIPs:
```bash
./zip_images_resize_recursive.sh
```
Create ZIPs from directory structure (preserves hierarchy in filename):
```bash
./zip_images_recursive.sh
```
## Dependencies
- ImageMagick (`~/Applications/magick`) - For image resizing
- `7z` - For ZIP compression
- `unrar` or `rar` - For RAR extraction
- `numfmt` - For human-readable file size output
- `/dev/shm/` - RAM disk for temporary storage
## Important Notes
- Scripts modify files in-place (originals are deleted after successful processing)
- File modification times are preserved using `touch -r`
- Scripts use absolute paths via `realpath` for file handling
- Recursive scripts use `find` with `-print0` and null-delimited reading for safe filename handling
+8 -3
View File
@@ -1,9 +1,14 @@
#!/bin/bash #!/bin/bash
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
find . -mindepth 1 -type d -print0 | while IFS= read -r -d '' f find . -mindepth 1 -type d -print0 | while IFS= read -r -d '' f
do do
pushd "$f" if pushd "$f" > /dev/null; then
~/images_resize.sh "$SCRIPT_DIR/images_resize.sh"
popd popd > /dev/null
else
echo "Error: Failed to change to directory $f"
fi
done done
+5
View File
@@ -5,6 +5,11 @@
# #
# Usage: rar2zip.sh file [file ...] # Usage: rar2zip.sh file [file ...]
if [ $# -eq 0 ]; then
echo "Usage: $0 file.rar [file2.rar ...]"
exit 1
fi
echo "Converting RARs to ZIPs" echo "Converting RARs to ZIPs"
# Use RAM disk for temporary files. # Use RAM disk for temporary files.
+2 -1
View File
@@ -1,2 +1,3 @@
#!/bin/bash #!/bin/bash
find /media/rewt/media/images/ -name '*.rar' -exec ~/rar2zip.sh '{}' \; SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
find /media/rewt/media/images/ -name '*.rar' -exec "$SCRIPT_DIR/rar2zip.sh" '{}' \;
+2 -1
View File
@@ -1,2 +1,3 @@
#!/bin/bash #!/bin/bash
find /media/rewt/media/incomplete/ -name '*.rar' -exec ~/rar2zip.sh '{}' \; SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
find /media/rewt/media/incomplete/ -name '*.rar' -exec "$SCRIPT_DIR/rar2zip.sh" '{}' \;
+3 -1
View File
@@ -28,8 +28,10 @@ if [ -n "$jpgs" ]; then
fi fi
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
find . -mindepth 1 -maxdepth 1 -type d -print0 | while IFS= read -r -d '' subd; do find . -mindepth 1 -maxdepth 1 -type d -print0 | while IFS= read -r -d '' subd; do
pushd "${subd}" pushd "${subd}"
~/zip_images_recursive.sh "${directoryTree}" "$SCRIPT_DIR/zip_images_recursive.sh" "${directoryTree}"
popd popd
done done
+11 -4
View File
@@ -5,6 +5,11 @@
# #
# Usage: xxx.sh file [file ...] # Usage: xxx.sh file [file ...]
if [ $# -eq 0 ]; then
echo "Usage: $0 file.zip [file2.zip ...]"
exit 1
fi
echo "Resizing large JPGs in ZIPs" echo "Resizing large JPGs in ZIPs"
# Use RAM disk for temporary files. # Use RAM disk for temporary files.
@@ -23,10 +28,12 @@ CURRENT_FILE=0
for INFILE in "$@"; do for INFILE in "$@"; do
CURRENT_FILE=$((CURRENT_FILE + 1)) CURRENT_FILE=$((CURRENT_FILE + 1))
# Convert to absolute path
INFILE="$(realpath "$INFILE")"
echo "Processing file $CURRENT_FILE of $TOTAL_FILES: $INFILE" echo "Processing file $CURRENT_FILE of $TOTAL_FILES: $INFILE"
# Absolute path to old file
#OLDFILE=$(realpath "${INFILE}") OUTFILE="$(realpath "${INFILE%.zip}-compress.zip")"
OUTFILE="${INFILE%.zip}-compress.zip"
# Remove output file if it already exists # Remove output file if it already exists
if [ -e "${OUTFILE}" ]; then if [ -e "${OUTFILE}" ]; then
@@ -81,7 +88,7 @@ for INFILE in "$@"; do
if [ ! -s "$OUTFILE" ]; then if [ ! -s "$OUTFILE" ]; then
echo "Error: Output file is empty or does not exist. Keeping original file." echo "Error: Output file is empty or does not exist. Keeping original file."
cleanup_temp cleanup_temp
continue exit 1
fi fi
rm "$INFILE" rm "$INFILE"