2023-12-07 04:02:47 +03:00
|
|
|
# Rename files to strip all special characters.
|
|
|
|
# All files by default.
|
|
|
|
# Usage: name [FILES]
|
2023-12-07 01:44:42 +03:00
|
|
|
function name() {
|
2023-12-05 21:50:45 +03:00
|
|
|
local IFS=$'\n'
|
2023-12-05 23:56:04 +03:00
|
|
|
local targets=("${@}")
|
|
|
|
local count=0
|
|
|
|
local total=${#}
|
2023-12-05 21:50:45 +03:00
|
|
|
local failed=0
|
|
|
|
|
|
|
|
# all targets except hidden by default.
|
|
|
|
if [[ "${targets}" = "" ]]; then
|
|
|
|
targets=([^.]*)
|
|
|
|
total=${#targets[@]}
|
|
|
|
fi
|
|
|
|
|
|
|
|
# process.
|
|
|
|
for target in "${targets[@]}"; do
|
|
|
|
# increment counter.
|
|
|
|
((count++))
|
|
|
|
|
|
|
|
# parse new name.
|
|
|
|
local ext=""
|
|
|
|
local name="${target}"
|
|
|
|
|
|
|
|
# ext only for files.
|
|
|
|
if [[ -f "${target}" ]]; then
|
|
|
|
ext=".${target##*.}"
|
|
|
|
name="${target%.*}"
|
|
|
|
fi
|
2023-12-10 07:20:48 +03:00
|
|
|
|
2023-12-05 21:50:45 +03:00
|
|
|
# Files w/o extension support.
|
|
|
|
[[ "${ext#.}" = "${name}" ]] && ext=""
|
|
|
|
|
|
|
|
# Get new name.
|
|
|
|
local new_name=$(parse_simplify "${name}")${ext,,}
|
|
|
|
|
|
|
|
# status line.
|
|
|
|
local status="[${count}/${total}] ${target} -> ${new_name}"
|
2023-12-13 16:55:21 +03:00
|
|
|
echo -e "${color_bwhite}${status}${color_default}"
|
2023-12-05 21:50:45 +03:00
|
|
|
|
|
|
|
# check if same name.
|
|
|
|
[[ "${target}" = "${new_name}" ]] && continue
|
|
|
|
|
|
|
|
# check if target name already exists.
|
|
|
|
if [[ -f "${new_name}" ]]; then
|
|
|
|
echo -e "${color_bred}${status}: already exists!${color_default}"
|
|
|
|
((failed++))
|
|
|
|
fi
|
|
|
|
|
|
|
|
# rename target.
|
|
|
|
mv -- "${target}" "${new_name}"
|
|
|
|
|
|
|
|
if [[ ${?} != 0 ]]; then
|
|
|
|
echo -e "${color_bred}${status}: Failed.${color_default}"
|
|
|
|
((failed++))
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
if [[ ${failed} != 0 ]]; then
|
|
|
|
echo -e "${color_bred}Failed: ${failed}.${color_default}"
|
|
|
|
false
|
|
|
|
fi
|
2023-08-08 16:24:15 +03:00
|
|
|
}
|
|
|
|
|
2023-12-07 04:02:47 +03:00
|
|
|
# Rename all files to their hashes while keeping extensions.
|
|
|
|
# All files by default.
|
|
|
|
# Usage: name_hash [FILES]
|
2023-12-07 01:44:42 +03:00
|
|
|
function name_hash() {
|
2023-12-05 21:50:45 +03:00
|
|
|
local IFS=$'\n'
|
2023-12-05 23:56:04 +03:00
|
|
|
local targets=("${@}")
|
|
|
|
local count=0
|
|
|
|
local total=${#}
|
2023-12-05 21:50:45 +03:00
|
|
|
local failed=0
|
|
|
|
|
|
|
|
# all files except hidden by default.
|
|
|
|
if [[ "${targets}" = "" ]]; then
|
|
|
|
targets=($(ls --classify | grep -v /\$))
|
|
|
|
total=${#targets[@]}
|
|
|
|
fi
|
|
|
|
|
|
|
|
# process.
|
|
|
|
for target in "${targets[@]}"; do
|
|
|
|
# increment count.
|
|
|
|
((count++))
|
|
|
|
|
|
|
|
# extract extension.
|
|
|
|
local extension="${target##*.}"
|
|
|
|
if [[ "${extension}" = "${target}" ]]; then
|
|
|
|
extension=""
|
|
|
|
else
|
|
|
|
extension=".${extension}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# hash the new name.
|
2023-12-07 22:37:08 +03:00
|
|
|
local hash=$(pv "${target}" | sha1sum | cut -d\ -f1)
|
2023-12-05 21:50:45 +03:00
|
|
|
new_name="${hash,,}${extension,,}"
|
2023-12-10 07:20:48 +03:00
|
|
|
|
2023-12-05 21:50:45 +03:00
|
|
|
# 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}"
|
2023-12-13 16:55:21 +03:00
|
|
|
echo -e "${color_bwhite}${status}${color_default}"
|
2023-12-05 21:50:45 +03:00
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
|
|
|
# show change.
|
2023-12-13 16:55:21 +03:00
|
|
|
echo -e "${color_bwhite}${status}${color_default}"
|
2023-12-05 21:50:45 +03:00
|
|
|
|
|
|
|
# rename target.
|
|
|
|
mv -- "${target}" "${new_name}"
|
2023-12-10 07:20:48 +03:00
|
|
|
|
2023-12-05 21:50:45 +03:00
|
|
|
if [[ ${?} != 0 ]]; then
|
|
|
|
echo -e "${color_bred}${status}: Failed.${color_default}"
|
|
|
|
((failed++))
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
if [[ ${failed} != 0 ]]; then
|
|
|
|
echo -e "${color_bred}Failed: ${failed}.${color_default}"
|
|
|
|
false
|
|
|
|
fi
|
2023-08-08 16:24:15 +03:00
|
|
|
}
|
|
|
|
|
2023-12-07 04:02:47 +03:00
|
|
|
# Check hashes for previously renamed files.
|
|
|
|
# All files by default.
|
|
|
|
# Usage: name_hash_check [FILES]
|
2023-12-07 01:44:42 +03:00
|
|
|
function name_hash_check() {
|
2023-12-05 21:50:45 +03:00
|
|
|
local IFS=$'\n'
|
2023-12-07 04:02:47 +03:00
|
|
|
local targets=("${@}")
|
|
|
|
local total=${#}
|
|
|
|
local count=0
|
2023-12-05 21:50:45 +03:00
|
|
|
local failed=0
|
|
|
|
|
|
|
|
# all targets by default.
|
|
|
|
if [[ "${targets}" = "" ]]; then
|
|
|
|
targets=($(ls --classify | grep -v /\$))
|
|
|
|
total=${#targets[@]}
|
|
|
|
fi
|
|
|
|
|
|
|
|
# process.
|
|
|
|
for target in "${targets[@]}"; do
|
|
|
|
# increment count.
|
|
|
|
((count++))
|
|
|
|
|
|
|
|
# status info.
|
|
|
|
local status="[${count}/${total}] ${target}"
|
|
|
|
|
2023-12-13 16:55:21 +03:00
|
|
|
echo -e "${color_bwhite}${status}${color_default}"
|
2023-12-05 21:50:45 +03:00
|
|
|
|
|
|
|
# extract hashes.
|
|
|
|
local stored="${target%%.*}"
|
2023-12-07 22:37:08 +03:00
|
|
|
local actual=$(pv "${target}" | sha1sum | cut -d\ -f1)
|
2023-12-05 21:50:45 +03:00
|
|
|
|
|
|
|
# compare hashes.
|
|
|
|
if [[ "${stored}" != "${actual}" ]]; then
|
|
|
|
echo -e "${color_bred}${status}: Failed.${color_default}"
|
|
|
|
((failed++))
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
if [[ ${failed} != 0 ]]; then
|
|
|
|
echo -e "${color_bred}Failed: ${failed}.${color_default}"
|
|
|
|
false
|
|
|
|
fi
|
2023-08-08 16:24:15 +03:00
|
|
|
}
|
|
|
|
|
2023-12-07 05:11:26 +03:00
|
|
|
# Rename files for Jellyfin series, i.e. `Episode S01E01.mkv`
|
2023-12-07 04:02:47 +03:00
|
|
|
# All files by default.
|
|
|
|
# Usage: name_series <SEASON> [FILES]
|
2023-12-07 01:44:42 +03:00
|
|
|
function name_series() {
|
2023-12-05 21:50:45 +03:00
|
|
|
local IFS=$'\n'
|
2023-12-05 23:56:04 +03:00
|
|
|
local season="${1}"
|
|
|
|
local targets=("${@:2}")
|
|
|
|
local count=0
|
|
|
|
local episode=0
|
|
|
|
local total=${#}
|
2023-12-05 21:50:45 +03:00
|
|
|
local failed=0
|
|
|
|
|
|
|
|
# error when no season number specified.
|
|
|
|
if [[ "${season}" = "" ]]; then
|
|
|
|
echo "usage: name_series <SEASON> [FILES]"
|
|
|
|
return 2
|
|
|
|
fi
|
|
|
|
|
|
|
|
# all targets by default.
|
|
|
|
if [[ "${targets}" = "" ]]; then
|
|
|
|
targets=($(ls --classify | grep -v /\$))
|
|
|
|
total=${#targets[@]}
|
|
|
|
fi
|
|
|
|
|
|
|
|
# process.
|
|
|
|
for target in "${targets[@]}"; do
|
|
|
|
# increment episode number.
|
|
|
|
((count++))
|
|
|
|
((episode++))
|
|
|
|
|
|
|
|
# extract new name.
|
|
|
|
local new_name="Episode S${season}E$(printf %02d ${episode}).${target##*.}"
|
|
|
|
|
|
|
|
# prepare status.
|
|
|
|
local status="[${count}/${total}] ${target} -> ${new_name}"
|
2023-12-13 16:55:21 +03:00
|
|
|
echo -e "${color_bwhite}${status}${color_default}"
|
2023-12-05 21:50:45 +03:00
|
|
|
|
|
|
|
# Warning on no change.
|
|
|
|
[[ "${target}" = "${new_name}" ]] && continue
|
|
|
|
|
|
|
|
# rename target.
|
|
|
|
mv -- "${target}" "${new_name}"
|
2023-12-10 07:20:48 +03:00
|
|
|
|
2023-12-05 21:50:45 +03:00
|
|
|
if [[ ${?} != 0 ]]; then
|
|
|
|
echo -e "${color_bred}${status}: Failed.${color_default}"
|
|
|
|
((failed++))
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
if [[ ${failed} != 0 ]]; then
|
|
|
|
echo -e "${color_bred}Failed: ${failed}.${color_default}"
|
|
|
|
false
|
|
|
|
fi
|
2023-08-08 16:24:15 +03:00
|
|
|
}
|
|
|
|
|
2023-12-07 04:02:47 +03:00
|
|
|
# Rename files for Kavita manga format.
|
|
|
|
# All files by default.
|
|
|
|
# Usage: name_manga <SEASON> [FILES]
|
2023-12-07 01:44:42 +03:00
|
|
|
function name_manga() {
|
2023-12-05 21:50:45 +03:00
|
|
|
local IFS=$'\n'
|
2023-12-05 23:56:04 +03:00
|
|
|
local season="${1}"
|
|
|
|
local targets=("${@:2}")
|
|
|
|
local count=0
|
|
|
|
local episode=0
|
|
|
|
local total=${#}
|
|
|
|
local manga="${PWD##*/}"
|
2023-12-05 21:50:45 +03:00
|
|
|
local failed=0
|
|
|
|
|
|
|
|
# error when no season number specified.
|
|
|
|
if [[ "${season}" = "" ]]; then
|
|
|
|
echo "usage: name_manga <SEASON> [FILES]"
|
|
|
|
return 2
|
|
|
|
fi
|
|
|
|
|
|
|
|
# all targets by default.
|
|
|
|
if [[ "${targets}" = "" ]]; then
|
|
|
|
targets=($(ls --classify | grep -v /\$))
|
|
|
|
total=${#targets[@]}
|
|
|
|
fi
|
|
|
|
|
|
|
|
# process.
|
|
|
|
for target in "${targets[@]}"; do
|
|
|
|
# increment episode number.
|
|
|
|
((count++))
|
|
|
|
((episode++))
|
|
|
|
|
|
|
|
# extract new name.
|
|
|
|
local new_name="${manga} Vol.${season} Ch.${episode}.${target##*.}"
|
|
|
|
|
|
|
|
# prepare status.
|
|
|
|
local status="[${count}/${total}] ${target} -> ${new_name}"
|
|
|
|
echo -e "${status}"
|
|
|
|
|
|
|
|
# Warning on no change.
|
|
|
|
[[ "${target}" = "${new_name}" ]] && continue
|
|
|
|
|
|
|
|
# rename target.
|
|
|
|
mv -- "${target}" "${new_name}"
|
2023-12-10 07:20:48 +03:00
|
|
|
|
2023-12-05 21:50:45 +03:00
|
|
|
if [[ ${?} != 0 ]]; then
|
|
|
|
echo -e "${color_bred}${status}: Failed.${color_default}"
|
|
|
|
((failed++))
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
if [[ ${failed} != 0 ]]; then
|
|
|
|
echo -e "${color_bred}Failed: ${failed}.${color_default}"
|
|
|
|
false
|
|
|
|
fi
|
2023-08-08 16:24:15 +03:00
|
|
|
}
|
|
|
|
|
2023-12-07 04:02:47 +03:00
|
|
|
# Rename files with new extension.
|
|
|
|
# All files by default.
|
|
|
|
# Usage: name_ext <EXTENSION> [FILES]
|
2023-12-07 01:44:42 +03:00
|
|
|
function name_ext() {
|
2023-12-05 21:50:45 +03:00
|
|
|
local IFS=$'\n'
|
2023-12-05 23:56:04 +03:00
|
|
|
local extension="${1}"
|
|
|
|
local targets=("${@:2}")
|
|
|
|
local count=0
|
|
|
|
local total=$((${#}-1))
|
2023-12-05 21:50:45 +03:00
|
|
|
local failed=0
|
|
|
|
|
|
|
|
# error when no new extension specified.
|
|
|
|
if [[ "${extension}" = "" ]]; then
|
2023-12-07 04:31:18 +03:00
|
|
|
help name_ext
|
2023-12-05 21:50:45 +03:00
|
|
|
return 2
|
|
|
|
fi
|
|
|
|
|
|
|
|
# all targets by default.
|
|
|
|
if [[ "${targets}" = "" ]]; then
|
|
|
|
targets=($(ls --classify | grep -v /\$))
|
|
|
|
total=${#targets[@]}
|
|
|
|
fi
|
2023-12-10 07:20:48 +03:00
|
|
|
|
2023-12-05 21:50:45 +03:00
|
|
|
# process.
|
|
|
|
for target in "${targets[@]}"; do
|
|
|
|
# increment count.
|
|
|
|
((count++))
|
|
|
|
|
|
|
|
# extract new name.
|
|
|
|
local new_name="${target%.*}"."${extension}"
|
|
|
|
|
|
|
|
# Prepare status.
|
|
|
|
local status="[${count}/${total}] ${target} -> ${new_name}"
|
2023-12-13 16:55:21 +03:00
|
|
|
echo -e "${color_bwhite}${status}${color_default}"
|
2023-12-05 21:50:45 +03:00
|
|
|
|
|
|
|
# Warning on no change.
|
|
|
|
[[ "${target}" = "${new_name}" ]] && continue
|
|
|
|
|
|
|
|
# rename target.
|
|
|
|
mv -- "${target}" "${new_name}"
|
2023-12-10 07:20:48 +03:00
|
|
|
|
2023-12-05 21:50:45 +03:00
|
|
|
if [[ ${?} != 0 ]]; then
|
|
|
|
echo -e "${color_bred}${status}: Failed.${color_default}"
|
|
|
|
((failed++))
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
if [[ ${failed} != 0 ]]; then
|
|
|
|
echo -e "${color_bred}Failed: ${failed}.${color_default}"
|
|
|
|
false
|
|
|
|
fi
|
2023-08-08 16:24:15 +03:00
|
|
|
}
|
|
|
|
|
2023-12-07 04:02:47 +03:00
|
|
|
# Change file name prefix.
|
|
|
|
# All matching files by default.
|
2023-11-26 00:51:57 +03:00
|
|
|
# Usage: name_prefix <OLD> <NEW> [FILES]
|
2023-12-07 01:44:42 +03:00
|
|
|
function name_prefix() {
|
2023-12-05 21:50:45 +03:00
|
|
|
local IFS=$'\n'
|
2023-12-05 23:56:04 +03:00
|
|
|
local old="${1}"
|
|
|
|
local new="${2}"
|
|
|
|
local targets=("${@:3}")
|
|
|
|
local count=0
|
|
|
|
local total=$((${#}-2))
|
2023-12-05 21:50:45 +03:00
|
|
|
local failed=0
|
|
|
|
|
2023-12-07 04:02:47 +03:00
|
|
|
# All matching targets by default.
|
2023-12-05 21:50:45 +03:00
|
|
|
if [[ "${targets}" = "" ]]; then
|
|
|
|
targets=(${old}*)
|
|
|
|
total=${#targets[@]}
|
|
|
|
fi
|
2023-12-10 07:20:48 +03:00
|
|
|
|
2023-12-05 21:50:45 +03:00
|
|
|
# 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-12-13 16:55:21 +03:00
|
|
|
echo -e "${color_bwhite}${status}${color_default}"
|
2023-12-05 21:50:45 +03:00
|
|
|
|
|
|
|
# Warning on no change.
|
|
|
|
[[ "${target}" = "${new_name}" ]] && continue
|
2023-12-10 07:20:48 +03:00
|
|
|
|
2023-12-05 21:50:45 +03:00
|
|
|
# Rename.
|
|
|
|
mv -- "${target}" "${new_name}"
|
2023-12-10 07:20:48 +03:00
|
|
|
|
2023-12-05 21:50:45 +03:00
|
|
|
if [[ ${?} != 0 ]]; then
|
|
|
|
echo -e "${color_bred}${status}: Failed.${color_default}"
|
|
|
|
((failed++))
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
if [[ ${failed} != 0 ]]; then
|
|
|
|
echo -e "${color_bred}Failed: ${failed}.${color_default}"
|
|
|
|
false
|
|
|
|
fi
|
2023-11-08 16:47:12 +03:00
|
|
|
}
|
|
|
|
|
2023-12-07 04:02:47 +03:00
|
|
|
# Change file name postfix.
|
|
|
|
# All matching files by default.
|
2023-11-26 00:51:57 +03:00
|
|
|
# Usage: name_postfix <OLD> <NEW> [FILES]
|
2023-12-07 01:44:42 +03:00
|
|
|
function name_postfix() {
|
2023-12-05 21:50:45 +03:00
|
|
|
local IFS=$'\n'
|
2023-12-05 23:56:04 +03:00
|
|
|
local old="${1}"
|
|
|
|
local new="${2}"
|
|
|
|
local targets=("${@:3}")
|
|
|
|
local count=0
|
|
|
|
local total=$((${#}-2))
|
2023-12-05 21:50:45 +03:00
|
|
|
local failed=0
|
|
|
|
|
2023-12-07 04:02:47 +03:00
|
|
|
# All matching targets by default.
|
2023-12-05 21:50:45 +03:00
|
|
|
if [[ "${targets}" = "" ]]; then
|
|
|
|
targets=(*${old})
|
|
|
|
total=${#targets[@]}
|
|
|
|
fi
|
2023-12-10 07:20:48 +03:00
|
|
|
|
2023-12-05 21:50:45 +03:00
|
|
|
# Process.
|
|
|
|
for target in "${targets[@]}"; do
|
|
|
|
# Increment counter.
|
|
|
|
((count++))
|
|
|
|
|
|
|
|
# Create new name.
|
|
|
|
local new_name="${target%$old}${new}"
|
|
|
|
|
|
|
|
# Prepare status.
|
|
|
|
local status="[${count}/${total}] ${target} -> ${new_name}"
|
2023-12-13 16:55:21 +03:00
|
|
|
echo -e "${color_bwhite}${status}${color_default}"
|
2023-12-05 21:50:45 +03:00
|
|
|
|
|
|
|
# Warning on no change.
|
|
|
|
[[ "${target}" = "${new_name}" ]] && continue
|
2023-12-10 07:20:48 +03:00
|
|
|
|
2023-12-05 21:50:45 +03:00
|
|
|
# Rename.
|
|
|
|
mv -- "${target}" "${new_name}"
|
2023-12-10 07:20:48 +03:00
|
|
|
|
2023-12-05 21:50:45 +03:00
|
|
|
if [[ ${?} != 0 ]]; then
|
|
|
|
echo -e "${color_bred}${status}: Failed.${color_default}"
|
|
|
|
((failed++))
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
if [[ ${failed} != 0 ]]; then
|
|
|
|
echo -e "${color_bred}Failed: ${failed}.${color_default}"
|
|
|
|
false
|
|
|
|
fi
|
2023-11-26 00:51:57 +03:00
|
|
|
}
|
|
|
|
|
2023-11-30 06:07:01 +03:00
|
|
|
# Replace part of the name.
|
2023-12-07 04:02:47 +03:00
|
|
|
# All matching files by default.
|
|
|
|
# Usage: name_replace <OLD> <NEW> [FILES]
|
2023-12-07 01:44:42 +03:00
|
|
|
function name_replace() {
|
2023-12-05 21:50:45 +03:00
|
|
|
local IFS=$'\n'
|
2023-12-05 23:56:04 +03:00
|
|
|
local old="${1}"
|
|
|
|
local new="${2}"
|
|
|
|
local targets=("${@:3}")
|
|
|
|
local count=0
|
|
|
|
local total=$((${#}-2))
|
2023-12-05 21:50:45 +03:00
|
|
|
local failed=0
|
|
|
|
|
2023-12-07 04:02:47 +03:00
|
|
|
# All matching targets by default.
|
2023-12-05 21:50:45 +03:00
|
|
|
if [[ "${targets}" = "" ]]; then
|
|
|
|
targets=(*${old}*)
|
|
|
|
total=${#targets[@]}
|
|
|
|
fi
|
2023-12-10 07:20:48 +03:00
|
|
|
|
2023-12-05 21:50:45 +03:00
|
|
|
# Process.
|
|
|
|
for target in "${targets[@]}"; do
|
|
|
|
# Increment counter.
|
|
|
|
((count++))
|
|
|
|
|
|
|
|
# Create new name.
|
|
|
|
local new_name="${target//$old/$new}"
|
|
|
|
|
|
|
|
# Prepare status.
|
|
|
|
local status="[${count}/${total}] ${target} -> ${new_name}"
|
2023-12-13 16:55:21 +03:00
|
|
|
echo -e "${color_bwhite}${status}${color_default}"
|
2023-12-05 21:50:45 +03:00
|
|
|
|
|
|
|
# Warning on no change.
|
|
|
|
[[ "${target}" = "${new_name}" ]] && continue
|
2023-12-10 07:20:48 +03:00
|
|
|
|
2023-12-05 21:50:45 +03:00
|
|
|
# Rename.
|
|
|
|
mv -- "${target}" "${new_name}"
|
2023-12-10 07:20:48 +03:00
|
|
|
|
|
|
|
if [[ ${?} != 0 ]]; then
|
|
|
|
echo -e "${color_bred}${status}: Failed.${color_default}"
|
|
|
|
((failed++))
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
if [[ ${failed} != 0 ]]; then
|
|
|
|
echo -e "${color_bred}Failed: ${failed}.${color_default}"
|
|
|
|
false
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# Fix numbering for numbered files. I.e if there are 10 items and some of them start without zero, then append zero to it. 1..10 -> 01..10.
|
|
|
|
# Usage: name_fix_numbering [FILES]
|
|
|
|
function name_fix_numbering() {
|
|
|
|
local IFS=$'\n'
|
|
|
|
local targets=("${@}")
|
|
|
|
local count=0
|
|
|
|
local total=${#}
|
|
|
|
local failed=0
|
|
|
|
local power=0
|
|
|
|
|
|
|
|
# All files by default.
|
|
|
|
if [[ "${targets[*]}" = "" ]]; then
|
2023-12-10 07:30:24 +03:00
|
|
|
targets=($(ls | grep "^[0-9]"))
|
2023-12-10 07:20:48 +03:00
|
|
|
total=${#targets[@]}
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Count leading zeroes.
|
|
|
|
local highest=0
|
|
|
|
for target in "${targets[@]}"; do
|
2023-12-10 07:26:04 +03:00
|
|
|
# Check that starts with a digit.
|
|
|
|
[[ "${target}" =~ ^[0-9] ]] || continue
|
|
|
|
|
2023-12-10 07:20:48 +03:00
|
|
|
local digits=($(_parse_ints "${target}"))
|
|
|
|
local digit="${digits[0]}"
|
|
|
|
digit=$((10#${digit}))
|
|
|
|
|
|
|
|
[[ "${digit}" -gt "${highest}" ]] && highest="${digit}"
|
|
|
|
done
|
|
|
|
|
|
|
|
local i=${highest}
|
|
|
|
while [[ i -gt 0 ]]; do
|
|
|
|
((power++))
|
|
|
|
i=$((${i}/10))
|
|
|
|
done
|
|
|
|
|
|
|
|
# Process.
|
|
|
|
for target in "${targets[@]}"; do
|
|
|
|
((count++))
|
|
|
|
|
|
|
|
local status="[${count}/${total}] ${target}"
|
|
|
|
|
|
|
|
# Check that starts with a digit.
|
|
|
|
if [[ ! "${target}" =~ ^[0-9] ]]; then
|
2023-12-13 16:55:21 +03:00
|
|
|
echo -e "${color_bwhite}${status}${color_default}"
|
2023-12-10 07:20:48 +03:00
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Prepare new name.
|
|
|
|
local digits=($(_parse_ints "${target}"))
|
|
|
|
local digit="${digits[0]}"
|
|
|
|
digit=$((10#${digit}))
|
|
|
|
local new_name=$(printf "%0${power}d" "${digit}")"${target#${digits[0]}}"
|
|
|
|
|
|
|
|
status="${status} -> ${new_name}"
|
|
|
|
|
|
|
|
# Skip if the same.
|
|
|
|
if [[ "${target}" = "${new_name}" ]]; then
|
2023-12-13 16:55:21 +03:00
|
|
|
echo -e "${color_bwhite}${status}${color_default}"
|
2023-12-10 07:20:48 +03:00
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Check that file does not exist.
|
|
|
|
if [[ -e "${new_name}" ]]; then
|
|
|
|
echo -e "${color_bred}${status}: File exists!${color_default}"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2023-12-13 16:55:21 +03:00
|
|
|
echo -e "${color_bwhite}${status}${color_default}"
|
2023-12-10 07:20:48 +03:00
|
|
|
|
|
|
|
mv -- "${target}" "${new_name}"
|
|
|
|
|
2023-12-05 21:50:45 +03:00
|
|
|
if [[ ${?} != 0 ]]; then
|
|
|
|
echo -e "${color_bred}${status}: Failed.${color_default}"
|
|
|
|
((failed++))
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
if [[ ${failed} != 0 ]]; then
|
|
|
|
echo -e "${color_bred}Failed: ${failed}.${color_default}"
|
|
|
|
false
|
|
|
|
fi
|
2023-11-30 06:07:01 +03:00
|
|
|
}
|