74 lines
2.9 KiB
Markdown
74 lines
2.9 KiB
Markdown
# Kavita Sorter Zsh Function
|
|
|
|
This zsh function helps organize files in the current directory into `Chapter #N` folders. It automatically detects existing chapter folders and creates the next sequential one.
|
|
|
|
```zsh
|
|
function kavitasort {
|
|
local chapter_dir_prefix="Chapter #"
|
|
local next_chapter_num=1
|
|
local existing_chapter_nums=()
|
|
|
|
# Find existing "Chapter #" directories and extract their numbers
|
|
for dir in "${chapter_dir_prefix}"*(N); do
|
|
if [[ -d "$dir" && "$dir" =~ "${chapter_dir_prefix}"([0-9]+) ]]; then
|
|
existing_chapter_nums+=("${MATCH[1]}")
|
|
fi
|
|
done
|
|
|
|
# Determine the next chapter number
|
|
if (( ${#existing_chapter_nums[@]} > 0 )); then
|
|
# Sort numbers in descending order to easily find the max
|
|
IFS=$'\n' existing_chapter_nums=($(sort -nr <<<"${existing_chapter_nums[*]}"))
|
|
unset IFS
|
|
next_chapter_num=$(( ${existing_chapter_nums[1]} + 1 ))
|
|
fi
|
|
|
|
local new_chapter_dir="${chapter_dir_prefix}${next_chapter_num}"
|
|
|
|
# Create the new chapter directory if it doesn't exist
|
|
if [[ ! -d "$new_chapter_dir" ]]; then
|
|
echo "Creating directory: $new_chapter_dir"
|
|
mkdir "$new_chapter_dir"
|
|
else
|
|
echo "Directory $new_chapter_dir already exists. This should not happen with sequential numbering. Aborting."
|
|
return 1
|
|
fi
|
|
|
|
# Move all files (and only files) from the current directory into the new chapter directory
|
|
local files_moved=0
|
|
for file in *; do
|
|
if [[ -f "$file" ]]; then # Check if it's a regular file
|
|
echo "Moving '$file' to '$new_chapter_dir/'"
|
|
mv "$file" "$new_chapter_dir/"
|
|
files_moved=$((files_moved + 1))
|
|
fi
|
|
done
|
|
|
|
if (( files_moved > 0 )); then
|
|
echo "Moved $files_moved files to $new_chapter_dir."
|
|
else
|
|
echo "No files found to move in the current directory."
|
|
fi
|
|
}
|
|
```
|
|
|
|
### Usage:
|
|
|
|
1. **Add to your `.zshrc`:**
|
|
To make this function permanently available, add the code block above to your `~/.zshrc` file.
|
|
After adding, run `source ~/.zshrc` or open a new terminal session.
|
|
|
|
2. **Run the function:**
|
|
Navigate to the directory where your comic/manga files are located and simply run:
|
|
```bash
|
|
kavitasort
|
|
```
|
|
|
|
### How it works:
|
|
|
|
* It first scans the current directory for any folders starting with "Chapter #".
|
|
* It extracts the numerical part from these folder names (e.g., "1" from "Chapter #1").
|
|
* If no such folders exist, it defaults to creating "Chapter #1".
|
|
* If they exist, it finds the highest number and increments it by one to create the next chapter folder (e.g., if "Chapter #1" and "Chapter #2" exist, it will create "Chapter #3").
|
|
* It then iterates through all items in the current directory and moves only the regular files (ignoring subdirectories) into the newly created chapter folder.
|
|
* It includes checks to ensure the new directory is created and provides feedback on moved files. |