2023-08-08 16:24:15 +03:00
|
|
|
# rename files to strip all special characters.
|
|
|
|
# usage: name [FILES]
|
|
|
|
name()
|
|
|
|
{
|
2023-10-30 03:49:10 +03:00
|
|
|
local targets=("${@}") # target file(s).
|
|
|
|
local count=0 # processed count.
|
|
|
|
local total=${#} # total to process.
|
|
|
|
|
|
|
|
# all targets except hidden by default.
|
|
|
|
if [[ "${targets}" = "" ]]; then
|
|
|
|
targets=([^.]*)
|
|
|
|
total=${#targets[@]}
|
2023-08-08 16:24:15 +03:00
|
|
|
fi
|
|
|
|
|
|
|
|
# process.
|
2023-10-30 03:49:10 +03:00
|
|
|
for target in "${targets[@]}"; do
|
|
|
|
# increment counter.
|
2023-10-29 21:26:22 +03:00
|
|
|
((count++))
|
2023-08-08 16:24:15 +03:00
|
|
|
|
2023-10-30 03:49:10 +03:00
|
|
|
# parse new name.
|
2023-11-08 02:37:05 +03:00
|
|
|
local ext=""
|
|
|
|
local name="${target}"
|
|
|
|
|
|
|
|
# ext only for files.
|
|
|
|
if [[ -f "${target}" ]]; then
|
|
|
|
ext=".${target##*.}"
|
|
|
|
name="${target%.*}"
|
|
|
|
fi
|
2023-11-08 20:31:00 +03:00
|
|
|
|
|
|
|
# Files w/o extension support.
|
|
|
|
[[ "${ext#.}" = "${name}" ]] && ext=""
|
2023-11-08 02:37:05 +03:00
|
|
|
|
2023-11-08 20:31:00 +03:00
|
|
|
# Get new name.
|
2023-11-08 02:37:05 +03:00
|
|
|
local new_name=$(parse_simplify "${name}")${ext}
|
2023-08-08 16:24:15 +03:00
|
|
|
|
|
|
|
# status line.
|
2023-10-30 03:49:10 +03:00
|
|
|
local status="[${count}/${total}] ${target} -> ${new_name}"
|
2023-08-08 16:24:15 +03:00
|
|
|
|
|
|
|
# check if same name.
|
2023-10-30 03:49:10 +03:00
|
|
|
if [[ "${target}" = "${new_name}" ]]; then
|
2023-11-10 11:53:20 +03:00
|
|
|
echo -e "${color_green}${status}: No change.${color_default}"
|
2023-08-08 16:24:15 +03:00
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
2023-10-30 03:49:10 +03:00
|
|
|
# check if target name already exists.
|
2023-10-29 21:26:22 +03:00
|
|
|
if [[ -f "${new_name}" ]]; then
|
2023-11-08 20:31:00 +03:00
|
|
|
echo -e "${color_bred}${status}: already exists!${color_default}"
|
2023-08-08 16:24:15 +03:00
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2023-10-30 03:49:10 +03:00
|
|
|
# rename target.
|
|
|
|
mv -- "${target}" "${new_name}" &> /dev/null
|
2023-08-08 16:24:15 +03:00
|
|
|
|
|
|
|
# show change.
|
2023-11-10 00:18:50 +03:00
|
|
|
echo -e "${color_default}${status}${color_default}"
|
2023-08-08 16:24:15 +03:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
# rename all files to their hashes while keeping extensions.
|
|
|
|
# usage: name_hash [FILES]
|
|
|
|
name_hash()
|
|
|
|
{
|
2023-10-30 03:49:10 +03:00
|
|
|
local targets=("${@}") # target file(s).
|
|
|
|
local count=0 # processed counter.
|
|
|
|
local total=${#} # total to process.
|
|
|
|
|
|
|
|
# all targets except hidden by default.
|
|
|
|
if [[ "${targets}" = "" ]]; then
|
|
|
|
targets=([^.]*)
|
|
|
|
total=${#targets[@]}
|
2023-08-08 16:24:15 +03:00
|
|
|
fi
|
|
|
|
|
|
|
|
# process.
|
2023-10-30 03:49:10 +03:00
|
|
|
for target in "${targets[@]}"; do
|
2023-11-08 16:47:12 +03:00
|
|
|
# process only files.
|
|
|
|
if [[ -f "${target}" ]]; then
|
|
|
|
# increment count.
|
|
|
|
((count++))
|
2023-10-29 21:26:22 +03:00
|
|
|
|
2023-11-08 16:47:12 +03:00
|
|
|
# extract extension.
|
|
|
|
local extension="${target##*.}"
|
|
|
|
if [[ "${extension}" = "${target}" ]]; then
|
|
|
|
extension=""
|
|
|
|
else
|
|
|
|
extension=".${extension}"
|
|
|
|
fi
|
2023-10-29 21:26:22 +03:00
|
|
|
|
2023-11-08 16:47:12 +03:00
|
|
|
# hash the new name.
|
|
|
|
local hash=$(sha1sum -- "${target}" | cut -d\ -f1)
|
|
|
|
new_name="${hash,,}${extension}"
|
|
|
|
|
|
|
|
# prepare status.
|
|
|
|
local status="[${count}/${total}] ${target} -> ${new_name}"
|
2023-08-08 16:24:15 +03:00
|
|
|
|
2023-11-08 16:47:12 +03:00
|
|
|
# check if same name.
|
|
|
|
if [[ "${target}" = "${new_name}" ]]; then
|
2023-11-10 11:53:20 +03:00
|
|
|
echo -e "${color_green}${status}: No change.${color_default}"
|
2023-11-08 16:47:12 +03:00
|
|
|
continue
|
|
|
|
fi
|
2023-08-08 16:24:15 +03:00
|
|
|
|
2023-11-08 16:47:12 +03:00
|
|
|
# rename target.
|
|
|
|
mv -- "${target}" "${new_name}" &> /dev/null
|
2023-10-30 03:49:10 +03:00
|
|
|
|
2023-11-08 16:47:12 +03:00
|
|
|
# show change.
|
2023-11-10 00:18:50 +03:00
|
|
|
echo -e "${color_default}${status}${color_default}"
|
2023-11-08 16:47:12 +03:00
|
|
|
else
|
|
|
|
# Increment count.
|
|
|
|
((count++))
|
2023-10-30 03:49:10 +03:00
|
|
|
|
2023-11-08 16:47:12 +03:00
|
|
|
# Report skip.
|
2023-11-10 11:53:20 +03:00
|
|
|
echo -e "${color_green}[${count}/${total}] ${target}: Skipping directory.${color_default}"
|
2023-11-08 16:47:12 +03:00
|
|
|
fi
|
2023-08-08 16:24:15 +03:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
# check hashes for renamed files.
|
|
|
|
# usage: name_hash_check [FILES]
|
|
|
|
name_hash_check()
|
|
|
|
{
|
2023-10-30 03:49:10 +03:00
|
|
|
local targets=("${@}") # target file(s).
|
|
|
|
local total=${#} # total to process.
|
|
|
|
local count=0 # processed counter.
|
|
|
|
local failed=0 # failed to process counter.
|
|
|
|
|
|
|
|
# all targets by default.
|
|
|
|
if [[ "${targets}" = "" ]]; then
|
|
|
|
targets=([^.]*)
|
2023-11-08 16:47:12 +03:00
|
|
|
total=${#targets[@]}
|
2023-08-08 16:24:15 +03:00
|
|
|
fi
|
2023-10-26 18:54:05 +03:00
|
|
|
|
2023-08-08 16:24:15 +03:00
|
|
|
# process.
|
2023-10-30 03:49:10 +03:00
|
|
|
for target in "${targets[@]}"; do
|
2023-11-08 16:47:12 +03:00
|
|
|
# process only files.
|
2023-10-30 03:49:10 +03:00
|
|
|
if [[ -f "${target}" ]]; then
|
2023-10-29 21:26:22 +03:00
|
|
|
# increment count.
|
|
|
|
((count++))
|
|
|
|
|
2023-10-03 08:51:19 +03:00
|
|
|
# status info.
|
2023-10-30 03:49:10 +03:00
|
|
|
local status="[${count}/${total}] ${target}"
|
2023-10-03 08:51:19 +03:00
|
|
|
|
2023-08-08 16:24:15 +03:00
|
|
|
# extract hashes.
|
2023-10-30 03:49:10 +03:00
|
|
|
local stored="${target%%.*}"
|
|
|
|
local actual=$(sha1sum -- "${target}" | cut -d\ -f1)
|
2023-08-08 16:24:15 +03:00
|
|
|
|
|
|
|
# compare hashes.
|
2023-10-29 21:26:22 +03:00
|
|
|
if [[ "${stored}" = "${actual}" ]]; then
|
2023-11-10 00:18:50 +03:00
|
|
|
echo -e "${color_default}${status}: Validation OK.${color_default}"
|
2023-08-08 16:24:15 +03:00
|
|
|
else
|
2023-11-08 20:31:00 +03:00
|
|
|
echo -e "${color_bred}${status}: Validation failed.${color_default}"
|
2023-10-03 08:51:19 +03:00
|
|
|
((failed++))
|
2023-08-08 16:24:15 +03:00
|
|
|
fi
|
2023-11-08 16:47:12 +03:00
|
|
|
else
|
|
|
|
# Increment count.
|
|
|
|
((count++))
|
|
|
|
|
|
|
|
# Report skip.
|
2023-11-10 11:53:20 +03:00
|
|
|
echo -e "${color_green}[${count}/${total}] ${target}: Skipping directory.${color_default}"
|
2023-08-08 16:24:15 +03:00
|
|
|
fi
|
|
|
|
done
|
2023-10-03 08:51:19 +03:00
|
|
|
|
2023-10-26 18:54:05 +03:00
|
|
|
# report result.
|
2023-10-29 21:26:22 +03:00
|
|
|
if [[ ${count} -gt 1 ]]; then
|
|
|
|
if [[ ${failed} -gt 0 ]]; then
|
|
|
|
echo -e "${color_bred}Items failed to validate: ${failed}.${color_default}"
|
|
|
|
else
|
|
|
|
echo -e "${color_green}All successful.${color_default}"
|
|
|
|
fi
|
2023-10-03 08:51:19 +03:00
|
|
|
fi
|
2023-08-08 16:24:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
# rename files for Jellyfin series, i.e. Episode S01E01.mkv
|
2023-11-08 16:47:12 +03:00
|
|
|
# usage: name_series <SEASON> [FILES]
|
2023-08-08 16:24:15 +03:00
|
|
|
name_series()
|
|
|
|
{
|
2023-11-08 16:47:12 +03:00
|
|
|
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.
|
2023-08-08 16:24:15 +03:00
|
|
|
|
|
|
|
# error when no season number specified.
|
2023-10-29 21:26:22 +03:00
|
|
|
if [[ "${season}" = "" ]]; then
|
2023-11-08 20:31:00 +03:00
|
|
|
echo "usage: name_series <SEASON> [FILES]"
|
2023-08-08 16:24:15 +03:00
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2023-10-30 03:49:10 +03:00
|
|
|
# all targets by default.
|
|
|
|
if [[ "${targets}" = "" ]]; then
|
|
|
|
targets=([^.]*)
|
|
|
|
total=${#targets[@]}
|
2023-08-08 16:24:15 +03:00
|
|
|
fi
|
|
|
|
|
|
|
|
# process.
|
2023-10-30 03:49:10 +03:00
|
|
|
for target in "${targets[@]}"; do
|
|
|
|
# process only targets.
|
|
|
|
if [[ -f "${target}" ]]; then
|
2023-10-29 21:26:22 +03:00
|
|
|
# increment episode number.
|
|
|
|
((count++))
|
2023-11-08 16:47:12 +03:00
|
|
|
((episode++))
|
2023-10-29 21:26:22 +03:00
|
|
|
|
2023-08-08 16:24:15 +03:00
|
|
|
# extract new name.
|
2023-11-08 16:47:12 +03:00
|
|
|
local new_name="Episode S${season}E$(printf %02d ${episode}).${target##*.}"
|
2023-08-08 16:24:15 +03:00
|
|
|
|
2023-10-29 21:26:22 +03:00
|
|
|
# prepare status.
|
2023-10-30 03:49:10 +03:00
|
|
|
local status="[${count}/${total}] ${target} -> ${new_name}"
|
2023-08-08 16:24:15 +03:00
|
|
|
|
2023-11-08 20:31:00 +03:00
|
|
|
# Warning on no change.
|
|
|
|
if [[ "${target}" = "${new_name}" ]]; then
|
2023-11-10 11:53:20 +03:00
|
|
|
echo -e "${color_green}${status}: No change.${color_default}"
|
2023-11-08 20:31:00 +03:00
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
2023-10-30 03:49:10 +03:00
|
|
|
# rename target.
|
|
|
|
mv -- "${target}" "${new_name}" &> /dev/null
|
2023-11-08 20:31:00 +03:00
|
|
|
|
|
|
|
# Report status.
|
2023-11-10 00:18:50 +03:00
|
|
|
echo -e "${color_default}${status}${color_default}"
|
2023-11-08 16:47:12 +03:00
|
|
|
else
|
|
|
|
# Increment count.
|
|
|
|
((count++))
|
|
|
|
|
|
|
|
# Report skip.
|
2023-11-10 11:53:20 +03:00
|
|
|
echo -e "${color_green}[${count}/${total}] ${target}: Skipping directory.${color_default}"
|
2023-08-08 16:24:15 +03:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
# rename files for Kavita manga format.
|
2023-11-08 16:47:12 +03:00
|
|
|
# usage: name_manga <SEASON> [FILES]
|
2023-08-08 16:24:15 +03:00
|
|
|
name_manga()
|
|
|
|
{
|
2023-11-08 16:47:12 +03:00
|
|
|
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.
|
2023-08-08 16:24:15 +03:00
|
|
|
|
|
|
|
# error when no season number specified.
|
2023-10-29 21:26:22 +03:00
|
|
|
if [[ "${season}" = "" ]]; then
|
2023-11-08 20:31:00 +03:00
|
|
|
echo "usage: name_manga <SEASON> [FILES]"
|
2023-08-08 16:24:15 +03:00
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2023-10-30 03:49:10 +03:00
|
|
|
# all targets by default.
|
|
|
|
if [[ "${targets}" = "" ]]; then
|
|
|
|
targets=([^.]*)
|
|
|
|
total=${#targets[@]}
|
2023-08-08 16:24:15 +03:00
|
|
|
fi
|
|
|
|
|
|
|
|
# process.
|
2023-10-30 03:49:10 +03:00
|
|
|
for target in "${targets[@]}"; do
|
|
|
|
# process only targets.
|
|
|
|
if [[ -f "${target}" ]]; then
|
2023-10-29 21:26:22 +03:00
|
|
|
# increment episode number.
|
|
|
|
((count++))
|
2023-11-08 16:47:12 +03:00
|
|
|
((episode++))
|
2023-10-29 21:26:22 +03:00
|
|
|
|
2023-08-08 16:24:15 +03:00
|
|
|
# extract new name.
|
2023-11-08 16:47:12 +03:00
|
|
|
local new_name="${manga} Vol.${season} Ch.${episode}.${target##*.}"
|
2023-08-08 16:24:15 +03:00
|
|
|
|
2023-10-29 21:26:22 +03:00
|
|
|
# prepare status.
|
2023-10-30 03:49:10 +03:00
|
|
|
local status="[${count}/${total}] ${target} -> ${new_name}"
|
2023-08-08 16:24:15 +03:00
|
|
|
|
2023-11-08 20:31:00 +03:00
|
|
|
# Warning on no change.
|
|
|
|
if [[ "${target}" = "${new_name}" ]]; then
|
2023-11-10 11:53:20 +03:00
|
|
|
echo -e "${color_green}${status}: No change.${color_default}"
|
2023-11-08 20:31:00 +03:00
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
2023-10-30 03:49:10 +03:00
|
|
|
# rename target.
|
|
|
|
mv -- "${target}" "${new_name}" &> /dev/null
|
2023-11-08 20:31:00 +03:00
|
|
|
|
|
|
|
# Show status.
|
2023-11-10 00:18:50 +03:00
|
|
|
echo -e "${color_default}${status}${color_default}"
|
2023-11-08 16:47:12 +03:00
|
|
|
else
|
|
|
|
# Increment count.
|
|
|
|
((count++))
|
|
|
|
|
|
|
|
# Report skip.
|
2023-11-10 11:53:20 +03:00
|
|
|
echo -e "${color_green}[${count}/${total}] ${target}: Skipping directory.${color_default}"
|
2023-08-08 16:24:15 +03:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
# rename files for new extension.
|
2023-10-30 03:49:10 +03:00
|
|
|
# usage: name_ext <EXTENSION> [FILES]
|
2023-08-08 16:24:15 +03:00
|
|
|
name_ext()
|
|
|
|
{
|
2023-11-08 16:47:12 +03:00
|
|
|
local extension="${1}" # new extension.
|
|
|
|
local targets=("${@:2}") # target file(s).
|
|
|
|
local count=0 # processed counter.
|
|
|
|
local total=$((${#}-1)) # total to process.
|
2023-08-08 16:24:15 +03:00
|
|
|
|
|
|
|
# error when no new extension specified.
|
2023-10-29 21:26:22 +03:00
|
|
|
if [[ "${extension}" = "" ]]; then
|
2023-11-08 16:47:12 +03:00
|
|
|
echo "usage: name_ext <EXTENSION> [FILES]"
|
2023-08-08 16:24:15 +03:00
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2023-10-30 03:49:10 +03:00
|
|
|
# all targets by default.
|
|
|
|
if [[ "${targets}" = "" ]]; then
|
|
|
|
targets=([^.]*)
|
|
|
|
total=${#targets[@]}
|
2023-08-08 16:24:15 +03:00
|
|
|
fi
|
|
|
|
|
|
|
|
# process.
|
2023-10-30 03:49:10 +03:00
|
|
|
for target in "${targets[@]}"; do
|
|
|
|
# process only targets.
|
|
|
|
if [[ -f "${target}" ]]; then
|
2023-10-29 21:26:22 +03:00
|
|
|
# increment count.
|
|
|
|
((count++))
|
|
|
|
|
2023-08-08 16:24:15 +03:00
|
|
|
# extract new name.
|
2023-10-30 03:49:10 +03:00
|
|
|
local new_name="${target%.*}"."${extension}"
|
2023-08-08 16:24:15 +03:00
|
|
|
|
2023-11-08 16:47:12 +03:00
|
|
|
# Prepare status.
|
|
|
|
local status="[${count}/${total}] ${target} -> ${new_name}"
|
|
|
|
|
2023-11-08 20:31:00 +03:00
|
|
|
# Warning on no change.
|
|
|
|
if [[ "${target}" = "${new_name}" ]]; then
|
2023-11-10 11:53:20 +03:00
|
|
|
echo -e "${color_green}${status}: No change.${color_default}"
|
2023-11-08 20:31:00 +03:00
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
2023-10-30 03:49:10 +03:00
|
|
|
# rename target.
|
|
|
|
mv -- "${target}" "${new_name}" &> /dev/null
|
2023-08-08 16:24:15 +03:00
|
|
|
|
|
|
|
# show change.
|
2023-11-10 00:18:50 +03:00
|
|
|
echo -e "${color_default}${status}${color_default}"
|
2023-11-08 16:47:12 +03:00
|
|
|
else
|
|
|
|
# Increment count.
|
|
|
|
((count++))
|
|
|
|
|
|
|
|
# Report skip.
|
2023-11-10 11:53:20 +03:00
|
|
|
echo -e "${color_green}[${count}/${total}] ${target}: Skipping directory.${color_default}"
|
2023-08-08 16:24:15 +03:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2023-11-08 16:47:12 +03:00
|
|
|
# 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}"
|
2023-11-08 20:31:00 +03:00
|
|
|
|
|
|
|
# Warning on no change.
|
|
|
|
if [[ "${target}" = "${new_name}" ]]; then
|
2023-11-10 11:53:20 +03:00
|
|
|
echo -e "${color_green}${status}: No change.${color_default}"
|
2023-11-08 20:31:00 +03:00
|
|
|
continue
|
|
|
|
fi
|
2023-11-08 16:47:12 +03:00
|
|
|
|
|
|
|
# Rename.
|
|
|
|
mv -- "${target}" "${new_name}" &> /dev/null
|
|
|
|
|
|
|
|
# Show change.
|
2023-11-10 00:18:50 +03:00
|
|
|
echo -e "${color_default}${status}${color_default}"
|
2023-11-08 16:47:12 +03:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2023-08-08 16:24:15 +03:00
|
|
|
# export for parallel.
|
2023-10-23 01:00:15 +03:00
|
|
|
export -f name name_hash name_hash_check name_ext
|