name : add name_prefix & bugfixes.

This commit is contained in:
Dmitry Voronin 2023-11-08 16:47:12 +03:00
parent 2c55a5e61e
commit 9188526612
2 changed files with 124 additions and 54 deletions

View file

@ -542,9 +542,10 @@ Command|Description
`name [FILES]`|Replace all non-alphanumeric characters in file with underscores. `name [FILES]`|Replace all non-alphanumeric characters in file with underscores.
`name_hash [FILES]`|Replace all file names with hashes, keep extension. `name_hash [FILES]`|Replace all file names with hashes, keep extension.
`name_hash_check [FILES]`|Check all file hashes. `name_hash_check [FILES]`|Check all file hashes.
`name_series <SEASON>`|Rename files to Jellyfin's show format: `Episode S01E01.mkv`. `name_series <SEASON> [FILES]`|Rename files to Jellyfin's show format: `Episode S01E01.mkv`.
`name_manga <SEASON>`|Rename files to Kavita's manga format: `Name Vol.1 Ch.01.cbr`. `name_manga <SEASON> [FILES]`|Rename files to Kavita's manga format: `Name Vol.1 Ch.01.cbr`.
`name_ext <EXTENSION> [FILES]`|Change file extensions. `name_ext <EXTENSION> [FILES]`|Change file extensions.
`name_prefix [OLD] [NEW] [FILES]`|Change file prefix.
## Ncdu. ## Ncdu.

View file

@ -68,9 +68,8 @@ name_hash()
# process. # process.
for target in "${targets[@]}"; do for target in "${targets[@]}"; do
# process only targets. # process only files.
[[ -f "${target}" ]] || continue if [[ -f "${target}" ]]; then
# increment count. # increment count.
((count++)) ((count++))
@ -100,6 +99,13 @@ name_hash()
# show change. # show change.
echo -e "${status}" echo -e "${status}"
else
# Increment count.
((count++))
# Report skip.
echo -e "${color_yellow}[${count}/${total}] ${target}: skipping directory.${color_default}"
fi
done done
} }
@ -115,12 +121,12 @@ name_hash_check()
# all targets by default. # all targets by default.
if [[ "${targets}" = "" ]]; then if [[ "${targets}" = "" ]]; then
targets=([^.]*) targets=([^.]*)
total=${#target[@]} total=${#targets[@]}
fi fi
# process. # process.
for target in "${targets[@]}"; do for target in "${targets[@]}"; do
# process only targets. # process only files.
if [[ -f "${target}" ]]; then if [[ -f "${target}" ]]; then
# increment count. # increment count.
((count++)) ((count++))
@ -139,6 +145,12 @@ name_hash_check()
echo -e "${color_red}${status}: failed.${color_default}" echo -e "${color_red}${status}: failed.${color_default}"
((failed++)) ((failed++))
fi fi
else
# Increment count.
((count++))
# Report skip.
echo -e "${color_yellow}[${count}/${total}] ${target}: skipping directory.${color_default}"
fi fi
done done
@ -153,13 +165,14 @@ name_hash_check()
} }
# rename files for Jellyfin series, i.e. Episode S01E01.mkv # rename files for Jellyfin series, i.e. Episode S01E01.mkv
# usage: name_series <SEASON> # usage: name_series <SEASON> [FILES]
name_series() name_series()
{ {
local season="${1}" # season number. local season="${1}" # Season number.
local targets="${@:2}" # target files. local targets=("${@:2}") # Target files.
local count=0 # processed counter. local count=0 # Processed counter.
local total=${#} # total to process. local episode=0 # Current episode count.
local total=${#} # Total to process.
# error when no season number specified. # error when no season number specified.
if [[ "${season}" = "" ]]; then if [[ "${season}" = "" ]]; then
@ -179,9 +192,10 @@ name_series()
if [[ -f "${target}" ]]; then if [[ -f "${target}" ]]; then
# increment episode number. # increment episode number.
((count++)) ((count++))
((episode++))
# extract new name. # 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. # prepare status.
local status="[${count}/${total}] ${target} -> ${new_name}" local status="[${count}/${total}] ${target} -> ${new_name}"
@ -189,19 +203,26 @@ name_series()
# rename target. # rename target.
mv -- "${target}" "${new_name}" &> /dev/null mv -- "${target}" "${new_name}" &> /dev/null
echo -e "${status}" echo -e "${status}"
else
# Increment count.
((count++))
# Report skip.
echo -e "${color_yellow}[${count}/${total}] ${target}: skipping directory.${color_default}"
fi fi
done done
} }
# rename files for Kavita manga format. # rename files for Kavita manga format.
# usage: name_manga <SEASON> # usage: name_manga <SEASON> [FILES]
name_manga() name_manga()
{ {
local season="${1}" # season number. local season="${1}" # Season number.
local targets="${@:2}" # target files. local targets=("${@:2}") # Target files.
local count=0 # processed counter. local count=0 # Processed counter.
local total=${#} # total to process. local episode=0 # Current episode count.
local manga="${PWD##*/}" # manga name. local total=${#} # Total to process.
local manga="${PWD##*/}" # Manga name.
# error when no season number specified. # error when no season number specified.
if [[ "${season}" = "" ]]; then if [[ "${season}" = "" ]]; then
@ -221,9 +242,10 @@ name_manga()
if [[ -f "${target}" ]]; then if [[ -f "${target}" ]]; then
# increment episode number. # increment episode number.
((count++)) ((count++))
((episode++))
# extract new name. # extract new name.
local new_name="${manga} Vol.${season} Ch.${count}.${target##*.}" local new_name="${manga} Vol.${season} Ch.${episode}.${target##*.}"
# prepare status. # prepare status.
local status="[${count}/${total}] ${target} -> ${new_name}" local status="[${count}/${total}] ${target} -> ${new_name}"
@ -231,6 +253,12 @@ name_manga()
# rename target. # rename target.
mv -- "${target}" "${new_name}" &> /dev/null mv -- "${target}" "${new_name}" &> /dev/null
echo -e "${status}" echo -e "${status}"
else
# Increment count.
((count++))
# Report skip.
echo -e "${color_yellow}[${count}/${total}] ${target}: skipping directory.${color_default}"
fi fi
done done
} }
@ -240,16 +268,13 @@ name_manga()
name_ext() name_ext()
{ {
local extension="${1}" # new extension. local extension="${1}" # new extension.
local targets="${@:2}" # target file(s). local targets=("${@:2}") # target file(s).
local count=0 # processed counter. local count=0 # processed counter.
local total=${#} # total to process. local total=$((${#}-1)) # total to process.
# remove extension name from total files.
((total--))
# error when no new extension specified. # error when no new extension specified.
if [[ "${extension}" = "" ]]; then if [[ "${extension}" = "" ]]; then
echo "usage: name_ext <EXTENSION> [targets]" echo "usage: name_ext <EXTENSION> [FILES]"
return 1 return 1
fi fi
@ -269,14 +294,58 @@ name_ext()
# extract new name. # extract new name.
local new_name="${target%.*}"."${extension}" local new_name="${target%.*}"."${extension}"
# Prepare status.
local status="[${count}/${total}] ${target} -> ${new_name}"
# rename target. # rename target.
mv -- "${target}" "${new_name}" &> /dev/null mv -- "${target}" "${new_name}" &> /dev/null
# show change. # 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 fi
done 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 for parallel.
export -f name name_hash name_hash_check name_ext export -f name name_hash name_hash_check name_ext