This repository has been archived on 2024-03-04. You can view files and clone it, but cannot push or open issues or pull requests.
linux/.linux/bash/module/name.sh

274 lines
6.4 KiB
Bash
Raw Normal View History

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.
local new_name=$(parse_simplify "${target}")
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-10-29 21:26:22 +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
echo -e "${color_red}${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-10-29 21:26:22 +03:00
echo "${status}"
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
# process only targets.
[[ -f "${target}" ]] || continue
2023-10-29 21:26:22 +03:00
2023-10-30 03:49:10 +03:00
# increment count.
((count++))
2023-10-29 21:26:22 +03:00
2023-10-30 03:49:10 +03:00
# extract extension.
local extension="${target##*.}"
if [[ "${extension}" = "${target}" ]]; then
extension=""
else
extension=".${extension}"
fi
2023-08-08 16:24:15 +03:00
2023-10-30 03:49:10 +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-10-30 03:49:10 +03:00
# check if same name.
if [[ "${target}" = "${new_name}" ]]; then
echo -e "${color_green}${status}: no change.${color_default}"
continue
2023-08-08 16:24:15 +03:00
fi
2023-10-30 03:49:10 +03:00
# rename target.
mv -- "${target}" "${new_name}" &> /dev/null
# show change.
echo -e "${status}"
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=([^.]*)
total=${#target[@]}
2023-08-08 16:24:15 +03:00
fi
2023-08-08 16:24:15 +03:00
# 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++))
# status info.
2023-10-30 03:49:10 +03:00
local status="[${count}/${total}] ${target}"
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
echo -e "${status}: OK."
2023-08-08 16:24:15 +03:00
else
echo -e "${color_red}${status}: failed.${color_default}"
((failed++))
2023-08-08 16:24:15 +03:00
fi
fi
done
# 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
fi
2023-08-08 16:24:15 +03:00
}
# rename files for Jellyfin series, i.e. Episode S01E01.mkv
2023-10-30 03:49:10 +03:00
# usage: name_series <SEASON>
2023-08-08 16:24:15 +03:00
name_series()
{
2023-10-30 03:49:10 +03:00
local season="${1}" # season number.
local targets="${@:2}" # target files.
local count=0 # processed counter.
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-10-30 03:49:10 +03:00
echo "usage: name_series <SEASON>"
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-08-08 16:24:15 +03:00
# extract new name.
2023-10-30 03:49:10 +03:00
local new_name="Episode S${season}E$(printf %02d ${count}).${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-10-30 03:49:10 +03:00
# rename target.
mv -- "${target}" "${new_name}" &> /dev/null
2023-10-29 21:26:22 +03:00
echo -e "${status}"
2023-08-08 16:24:15 +03:00
fi
done
}
# rename files for Kavita manga format.
2023-10-30 03:49:10 +03:00
# usage: name_manga <SEASON>
2023-08-08 16:24:15 +03:00
name_manga()
{
2023-10-30 03:49:10 +03:00
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.
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-10-30 03:49:10 +03:00
echo "usage: name_manga <SEASON>"
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-08-08 16:24:15 +03:00
# extract new name.
2023-10-30 03:49:10 +03:00
local new_name="${manga} Vol.${season} Ch.${count}.${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-10-30 03:49:10 +03:00
# rename target.
mv -- "${target}" "${new_name}" &> /dev/null
2023-10-29 21:26:22 +03:00
echo -e "${status}"
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-10-30 03:49:10 +03:00
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--))
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-10-30 03:49:10 +03:00
echo "usage: name_ext <EXTENSION> [targets]"
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-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-10-30 03:49:10 +03:00
echo "[${count}/${total}] ${target} -> ${new_name}"
2023-08-08 16:24:15 +03:00
fi
done
}
# export for parallel.
2023-10-23 01:00:15 +03:00
export -f name name_hash name_hash_check name_ext