From 179c61a5b6534a03dd0c12703de3cf0771e1f152 Mon Sep 17 00:00:00 2001 From: Jens Date: Sat, 7 Feb 2026 14:57:32 +0100 Subject: [PATCH] Add Kavita Sort desktop entry and initial zsh function documentation --- KavitaSorter/KavitaSort.desktop | 10 +++++ KavitaSorter/idea.md | 11 +++++ KavitaSorter/solution.md | 74 +++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 KavitaSorter/KavitaSort.desktop create mode 100644 KavitaSorter/idea.md create mode 100644 KavitaSorter/solution.md diff --git a/KavitaSorter/KavitaSort.desktop b/KavitaSorter/KavitaSort.desktop new file mode 100644 index 0000000..f074d4b --- /dev/null +++ b/KavitaSorter/KavitaSort.desktop @@ -0,0 +1,10 @@ +[Desktop Entry] +Type=Service +ServiceTypes=KonqPopupMenu/Plugin +MimeType=inode/directory; +Actions=kavitaSort; + +[Desktop Action kavitaSort] +Name=Kavita Sort +Icon=folder-sync +Exec=konsole --workdir %f -e zsh -ic 'kavita_sort' \ No newline at end of file diff --git a/KavitaSorter/idea.md b/KavitaSorter/idea.md new file mode 100644 index 0000000..9516c56 --- /dev/null +++ b/KavitaSorter/idea.md @@ -0,0 +1,11 @@ +Create an zsh function, it should in the current folder + +when there is no subfolder + - create an folder named Chapter #1 + - move all files and only files to the newly created folder + +when there is already an folder with naming Pattern Chapter#{number} + - increment the {number} in the Foldername by 1 and create an new folder named Chapter #[newnumber} + - move all Files and only files to the newly created folder, do not touch files in other folders then the current one + + diff --git a/KavitaSorter/solution.md b/KavitaSorter/solution.md new file mode 100644 index 0000000..d9fe13e --- /dev/null +++ b/KavitaSorter/solution.md @@ -0,0 +1,74 @@ +# 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. \ No newline at end of file