From 918852661280fd3c432408796878d82404bc9014 Mon Sep 17 00:00:00 2001 From: desktop Date: Wed, 8 Nov 2023 16:47:12 +0300 Subject: [PATCH] name : add name_prefix & bugfixes. --- .README.md | 5 +- .linux/bash/module/name.sh | 173 ++++++++++++++++++++++++++----------- 2 files changed, 124 insertions(+), 54 deletions(-) diff --git a/.README.md b/.README.md index 55d4a9c..0dcd113 100644 --- a/.README.md +++ b/.README.md @@ -542,9 +542,10 @@ Command|Description `name [FILES]`|Replace all non-alphanumeric characters in file with underscores. `name_hash [FILES]`|Replace all file names with hashes, keep extension. `name_hash_check [FILES]`|Check all file hashes. -`name_series `|Rename files to Jellyfin's show format: `Episode S01E01.mkv`. -`name_manga `|Rename files to Kavita's manga format: `Name Vol.1 Ch.01.cbr`. +`name_series [FILES]`|Rename files to Jellyfin's show format: `Episode S01E01.mkv`. +`name_manga [FILES]`|Rename files to Kavita's manga format: `Name Vol.1 Ch.01.cbr`. `name_ext [FILES]`|Change file extensions. +`name_prefix [OLD] [NEW] [FILES]`|Change file prefix. ## Ncdu. diff --git a/.linux/bash/module/name.sh b/.linux/bash/module/name.sh index 3d1c078..f7f43cf 100644 --- a/.linux/bash/module/name.sh +++ b/.linux/bash/module/name.sh @@ -68,38 +68,44 @@ name_hash() # process. for target in "${targets[@]}"; do - # process only targets. - [[ -f "${target}" ]] || continue + # process only files. + if [[ -f "${target}" ]]; then + # increment count. + ((count++)) - # increment count. - ((count++)) + # extract extension. + local extension="${target##*.}" + if [[ "${extension}" = "${target}" ]]; then + extension="" + else + extension=".${extension}" + fi - # extract extension. - local extension="${target##*.}" - if [[ "${extension}" = "${target}" ]]; then - extension="" + # hash the new name. + local hash=$(sha1sum -- "${target}" | cut -d\ -f1) + new_name="${hash,,}${extension}" + + # prepare status. + local status="[${count}/${total}] ${target} -> ${new_name}" + + # check if same name. + if [[ "${target}" = "${new_name}" ]]; then + echo -e "${color_green}${status}: no change.${color_default}" + continue + fi + + # rename target. + mv -- "${target}" "${new_name}" &> /dev/null + + # show change. + echo -e "${status}" else - extension=".${extension}" + # Increment count. + ((count++)) + + # Report skip. + echo -e "${color_yellow}[${count}/${total}] ${target}: skipping directory.${color_default}" fi - - # hash the new name. - local hash=$(sha1sum -- "${target}" | cut -d\ -f1) - new_name="${hash,,}${extension}" - - # prepare status. - local status="[${count}/${total}] ${target} -> ${new_name}" - - # check if same name. - if [[ "${target}" = "${new_name}" ]]; then - echo -e "${color_green}${status}: no change.${color_default}" - continue - fi - - # rename target. - mv -- "${target}" "${new_name}" &> /dev/null - - # show change. - echo -e "${status}" done } @@ -115,12 +121,12 @@ name_hash_check() # all targets by default. if [[ "${targets}" = "" ]]; then targets=([^.]*) - total=${#target[@]} + total=${#targets[@]} fi # process. for target in "${targets[@]}"; do - # process only targets. + # process only files. if [[ -f "${target}" ]]; then # increment count. ((count++)) @@ -139,6 +145,12 @@ name_hash_check() echo -e "${color_red}${status}: failed.${color_default}" ((failed++)) fi + else + # Increment count. + ((count++)) + + # Report skip. + echo -e "${color_yellow}[${count}/${total}] ${target}: skipping directory.${color_default}" fi done @@ -153,13 +165,14 @@ name_hash_check() } # rename files for Jellyfin series, i.e. Episode S01E01.mkv -# usage: name_series +# usage: name_series [FILES] name_series() { - local season="${1}" # season number. - local targets="${@:2}" # target files. - local count=0 # processed counter. - local total=${#} # total to process. + local season="${1}" # Season number. + local targets=("${@:2}") # Target files. + local count=0 # Processed counter. + local episode=0 # Current episode count. + local total=${#} # Total to process. # error when no season number specified. if [[ "${season}" = "" ]]; then @@ -179,9 +192,10 @@ name_series() if [[ -f "${target}" ]]; then # increment episode number. ((count++)) + ((episode++)) # extract new name. - local new_name="Episode S${season}E$(printf %02d ${count}).${target##*.}" + local new_name="Episode S${season}E$(printf %02d ${episode}).${target##*.}" # prepare status. local status="[${count}/${total}] ${target} -> ${new_name}" @@ -189,19 +203,26 @@ name_series() # rename target. mv -- "${target}" "${new_name}" &> /dev/null echo -e "${status}" + else + # Increment count. + ((count++)) + + # Report skip. + echo -e "${color_yellow}[${count}/${total}] ${target}: skipping directory.${color_default}" fi done } # rename files for Kavita manga format. -# usage: name_manga +# usage: name_manga [FILES] name_manga() { - local season="${1}" # season number. - local targets="${@:2}" # target files. - local count=0 # processed counter. - local total=${#} # total to process. - local manga="${PWD##*/}" # manga name. + local season="${1}" # Season number. + local targets=("${@:2}") # Target files. + local count=0 # Processed counter. + local episode=0 # Current episode count. + local total=${#} # Total to process. + local manga="${PWD##*/}" # Manga name. # error when no season number specified. if [[ "${season}" = "" ]]; then @@ -221,9 +242,10 @@ name_manga() if [[ -f "${target}" ]]; then # increment episode number. ((count++)) + ((episode++)) # extract new name. - local new_name="${manga} Vol.${season} Ch.${count}.${target##*.}" + local new_name="${manga} Vol.${season} Ch.${episode}.${target##*.}" # prepare status. local status="[${count}/${total}] ${target} -> ${new_name}" @@ -231,6 +253,12 @@ name_manga() # rename target. mv -- "${target}" "${new_name}" &> /dev/null echo -e "${status}" + else + # Increment count. + ((count++)) + + # Report skip. + echo -e "${color_yellow}[${count}/${total}] ${target}: skipping directory.${color_default}" fi done } @@ -239,17 +267,14 @@ name_manga() # usage: name_ext [FILES] name_ext() { - local extension="${1}" # new extension. - local targets="${@:2}" # target file(s). - local count=0 # processed counter. - local total=${#} # total to process. - - # remove extension name from total files. - ((total--)) + local extension="${1}" # new extension. + local targets=("${@:2}") # target file(s). + local count=0 # processed counter. + local total=$((${#}-1)) # total to process. # error when no new extension specified. if [[ "${extension}" = "" ]]; then - echo "usage: name_ext [targets]" + echo "usage: name_ext [FILES]" return 1 fi @@ -269,14 +294,58 @@ name_ext() # extract new name. local new_name="${target%.*}"."${extension}" + # Prepare status. + local status="[${count}/${total}] ${target} -> ${new_name}" + # rename target. mv -- "${target}" "${new_name}" &> /dev/null # show change. - echo "[${count}/${total}] ${target} -> ${new_name}" + echo "${status}" + else + # Increment count. + ((count++)) + + # Report skip. + echo -e "${color_yellow}[${count}/${total}] ${target}: skipping directory.${color_default}" fi done } +# Change file's prefixes. +# Usage: name_prefix [OLD] [NEW] [FILES] +name_prefix() +{ + local old="${1}" # Old prefix. + local new="${2}" # New prefix. + local targets=("${@:3}") # Target files. + local count=0 # Total files processed. + local total=$((${#}-2)) # Total files to process. + + # All targets by default. + if [[ "${targets}" = "" ]]; then + targets=([^.]*) + total=${#targets[@]} + fi + + # Process. + for target in "${targets[@]}"; do + # Increment counter. + ((count++)) + + # Create new name. + local new_name="${new}${target#$old}" + + # Prepare status. + local status="[${count}/${total}] ${target} -> ${new_name}" + + # Rename. + mv -- "${target}" "${new_name}" &> /dev/null + + # Show change. + echo "${status}" + done +} + # export for parallel. export -f name name_hash name_hash_check name_ext