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/.config/bash/module/name.sh

480 lines
11 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()
{
local IFS=$'\n'
2023-10-30 03:49:10 +03:00
local targets=("${@}") # target file(s).
local count=0 # processed count.
local total=${#} # total to process.
2023-11-24 01:19:49 +03:00
local failed=0
2023-10-30 03:49:10 +03:00
# 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.
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-11-24 01:19:49 +03:00
echo -e "${status}"
2023-08-08 16:24:15 +03:00
# check if same name.
2023-11-24 01:19:49 +03:00
[[ "${target}" = "${new_name}" ]] && continue
2023-08-08 16:24:15 +03:00
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-11-24 01:19:49 +03:00
((failed++))
2023-08-08 16:24:15 +03:00
fi
2023-10-30 03:49:10 +03:00
# rename target.
2023-11-24 01:19:49 +03:00
mv -- "${target}" "${new_name}"
2023-08-08 16:24:15 +03:00
2023-11-24 01:19:49 +03:00
if [[ ${?} != 0 ]]; then
echo -e "${color_bred}${status}: Failed.${color_default}"
((failed++))
fi
2023-08-08 16:24:15 +03:00
done
2023-11-24 01:19:49 +03:00
if [[ ${failed} != 0 ]]; then
echo -e "${color_bred}Failed: ${failed}.${color_default}"
false
fi
2023-08-08 16:24:15 +03:00
}
# rename all files to their hashes while keeping extensions.
# usage: name_hash [FILES]
name_hash()
{
local IFS=$'\n'
2023-10-30 03:49:10 +03:00
local targets=("${@}") # target file(s).
local count=0 # processed counter.
local total=${#} # total to process.
2023-11-24 01:19:49 +03:00
local failed=0
2023-10-30 03:49:10 +03:00
2023-11-24 01:19:49 +03:00
# all files except hidden by default.
2023-10-30 03:49:10 +03:00
if [[ "${targets}" = "" ]]; then
2023-11-24 01:19:49 +03:00
targets=($(ls --classify | grep -v /\$))
2023-10-30 03:49:10 +03:00
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-24 01:19:49 +03:00
# increment count.
((count++))
# extract extension.
local extension="${target##*.}"
if [[ "${extension}" = "${target}" ]]; then
extension=""
2023-11-08 16:47:12 +03:00
else
2023-11-24 01:19:49 +03:00
extension=".${extension}"
fi
# hash the new name.
2023-11-28 01:55:36 +03:00
local hash=$(pv "${target}" | sha1sum | cut -d\ -f1)
2023-11-24 01:19:49 +03:00
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}"
echo -e "${status}"
continue
fi
# show change.
echo -e "${status}"
2023-10-30 03:49:10 +03:00
2023-11-24 01:19:49 +03:00
# rename target.
mv -- "${target}" "${new_name}"
if [[ ${?} != 0 ]]; then
echo -e "${color_bred}${status}: Failed.${color_default}"
((failed++))
2023-11-08 16:47:12 +03:00
fi
2023-08-08 16:24:15 +03:00
done
2023-11-24 01:19:49 +03:00
if [[ ${failed} != 0 ]]; then
echo -e "${color_bred}Failed: ${failed}.${color_default}"
false
fi
2023-08-08 16:24:15 +03:00
}
# check hashes for renamed files.
# usage: name_hash_check [FILES]
name_hash_check()
{
local IFS=$'\n'
2023-10-30 03:49:10 +03:00
local targets=("${@}") # target file(s).
local total=${#} # total to process.
local count=0 # processed counter.
2023-11-24 01:19:49 +03:00
local failed=0
2023-10-30 03:49:10 +03:00
# all targets by default.
if [[ "${targets}" = "" ]]; then
2023-11-24 01:19:49 +03:00
targets=($(ls --classify | grep -v /\$))
2023-11-08 16:47:12 +03:00
total=${#targets[@]}
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
2023-11-24 01:19:49 +03:00
# increment count.
((count++))
2023-11-08 16:47:12 +03:00
2023-11-24 01:19:49 +03:00
# status info.
local status="[${count}/${total}] ${target}"
echo -e "${status}"
# extract hashes.
local stored="${target%%.*}"
local actual=$(pv "${target}" | sha1sum | cut -d\ -f1)
# compare hashes.
if [[ "${stored}" != "${actual}" ]]; then
echo -e "${color_bred}${status}: Failed.${color_default}"
((failed++))
2023-08-08 16:24:15 +03:00
fi
done
2023-11-24 01:19:49 +03:00
if [[ ${failed} != 0 ]]; then
echo -e "${color_bred}Failed: ${failed}.${color_default}"
false
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()
{
local IFS=$'\n'
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-11-24 01:19:49 +03:00
local failed=0
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-11-20 18:39:04 +03:00
return 2
2023-08-08 16:24:15 +03:00
fi
2023-10-30 03:49:10 +03:00
# all targets by default.
if [[ "${targets}" = "" ]]; then
2023-11-24 01:19:49 +03:00
targets=($(ls --classify | grep -v /\$))
2023-10-30 03:49:10 +03:00
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-24 01:19:49 +03:00
# increment episode number.
((count++))
((episode++))
2023-08-08 16:24:15 +03:00
2023-11-24 01:19:49 +03:00
# extract new name.
local new_name="Episode S${season}E$(printf %02d ${episode}).${target##*.}"
2023-11-08 20:31:00 +03:00
2023-11-24 01:19:49 +03:00
# prepare status.
local status="[${count}/${total}] ${target} -> ${new_name}"
echo -e "${status}"
2023-11-08 20:31:00 +03:00
2023-11-24 01:19:49 +03:00
# Warning on no change.
[[ "${target}" = "${new_name}" ]] && continue
2023-11-08 16:47:12 +03:00
2023-11-24 01:19:49 +03:00
# rename target.
mv -- "${target}" "${new_name}"
if [[ ${?} != 0 ]]; then
echo -e "${color_bred}${status}: Failed.${color_default}"
((failed++))
2023-08-08 16:24:15 +03:00
fi
done
2023-11-24 01:19:49 +03:00
if [[ ${failed} != 0 ]]; then
echo -e "${color_bred}Failed: ${failed}.${color_default}"
false
fi
2023-08-08 16:24:15 +03:00
}
# 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()
{
local IFS=$'\n'
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-11-24 01:19:49 +03:00
local failed=0
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-11-20 18:39:04 +03:00
return 2
2023-08-08 16:24:15 +03:00
fi
2023-10-30 03:49:10 +03:00
# all targets by default.
if [[ "${targets}" = "" ]]; then
2023-11-24 01:19:49 +03:00
targets=($(ls --classify | grep -v /\$))
2023-10-30 03:49:10 +03:00
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-24 01:19:49 +03:00
# increment episode number.
((count++))
((episode++))
2023-08-08 16:24:15 +03:00
2023-11-24 01:19:49 +03:00
# extract new name.
local new_name="${manga} Vol.${season} Ch.${episode}.${target##*.}"
2023-11-08 20:31:00 +03:00
2023-11-24 01:19:49 +03:00
# prepare status.
local status="[${count}/${total}] ${target} -> ${new_name}"
echo -e "${status}"
2023-11-08 20:31:00 +03:00
2023-11-24 01:19:49 +03:00
# Warning on no change.
[[ "${target}" = "${new_name}" ]] && continue
2023-11-08 16:47:12 +03:00
2023-11-24 01:19:49 +03:00
# rename target.
mv -- "${target}" "${new_name}"
if [[ ${?} != 0 ]]; then
echo -e "${color_bred}${status}: Failed.${color_default}"
((failed++))
2023-08-08 16:24:15 +03:00
fi
done
2023-11-24 01:19:49 +03:00
if [[ ${failed} != 0 ]]; then
echo -e "${color_bred}Failed: ${failed}.${color_default}"
false
fi
2023-08-08 16:24:15 +03:00
}
# 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()
{
local IFS=$'\n'
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-11-24 01:19:49 +03:00
local failed=0
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-24 01:19:49 +03:00
echo "Usage: name_ext <EXTENSION> [FILES]"
2023-11-20 18:39:04 +03:00
return 2
2023-08-08 16:24:15 +03:00
fi
2023-10-30 03:49:10 +03:00
# all targets by default.
if [[ "${targets}" = "" ]]; then
2023-11-24 01:19:49 +03:00
targets=($(ls --classify | grep -v /\$))
2023-10-30 03:49:10 +03:00
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-24 01:19:49 +03:00
# increment count.
((count++))
2023-11-08 16:47:12 +03:00
2023-11-24 01:19:49 +03:00
# extract new name.
local new_name="${target%.*}"."${extension}"
2023-11-08 20:31:00 +03:00
2023-11-24 01:19:49 +03:00
# Prepare status.
local status="[${count}/${total}] ${target} -> ${new_name}"
echo -e "${status}"
2023-08-08 16:24:15 +03:00
2023-11-24 01:19:49 +03:00
# Warning on no change.
[[ "${target}" = "${new_name}" ]] && continue
2023-11-08 16:47:12 +03:00
2023-11-24 01:19:49 +03:00
# rename target.
mv -- "${target}" "${new_name}"
if [[ ${?} != 0 ]]; then
echo -e "${color_bred}${status}: Failed.${color_default}"
((failed++))
2023-08-08 16:24:15 +03:00
fi
done
2023-11-24 01:19:49 +03:00
if [[ ${failed} != 0 ]]; then
echo -e "${color_bred}Failed: ${failed}.${color_default}"
false
fi
2023-08-08 16:24:15 +03:00
}
2023-11-26 00:51:57 +03:00
# Change file prefixes.
# Usage: name_prefix <OLD> <NEW> [FILES]
2023-11-08 16:47:12 +03:00
name_prefix()
{
local IFS=$'\n'
2023-11-08 16:47:12 +03:00
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.
2023-11-24 01:19:49 +03:00
local failed=0
2023-11-08 16:47:12 +03:00
# All targets by default.
if [[ "${targets}" = "" ]]; then
targets=(${old}*)
2023-11-08 16:47:12 +03:00
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-24 01:19:49 +03:00
echo -e "${status}"
2023-11-08 20:31:00 +03:00
# Warning on no change.
2023-11-24 01:19:49 +03:00
[[ "${target}" = "${new_name}" ]] && continue
2023-11-08 16:47:12 +03:00
# Rename.
2023-11-24 01:19:49 +03:00
mv -- "${target}" "${new_name}"
if [[ ${?} != 0 ]]; then
echo -e "${color_bred}${status}: Failed.${color_default}"
((failed++))
fi
2023-11-08 16:47:12 +03:00
done
2023-11-24 01:19:49 +03:00
if [[ ${failed} != 0 ]]; then
echo -e "${color_bred}Failed: ${failed}.${color_default}"
false
fi
2023-11-08 16:47:12 +03:00
}
2023-11-26 00:51:57 +03:00
# Change file postfix.
# Usage: name_postfix <OLD> <NEW> [FILES]
name_postfix()
{
local IFS=$'\n'
local old="${1}" # Old postfix.
local new="${2}" # New postfix.
local targets=("${@:3}") # Target files.
local count=0 # Total files processed.
local total=$((${#}-2)) # Total files to process.
local failed=0
# All targets by default.
if [[ "${targets}" = "" ]]; then
targets=(*${old})
2023-11-26 00:51:57 +03:00
total=${#targets[@]}
fi
# Process.
for target in "${targets[@]}"; do
# Increment counter.
((count++))
# Create new name.
local new_name="${target%$old}${new}"
2023-11-26 00:51:57 +03:00
# Prepare status.
local status="[${count}/${total}] ${target} -> ${new_name}"
echo -e "${status}"
# Warning on no change.
[[ "${target}" = "${new_name}" ]] && continue
# Rename.
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-11-30 06:07:01 +03:00
# Replace part of the name.
name_replace()
{
local IFS=$'\n'
local old="${1}" # Old postfix.
local new="${2}" # New postfix.
local targets=("${@:3}") # Target files.
local count=0 # Total files processed.
local total=$((${#}-2)) # Total files to process.
local failed=0
# All targets by default.
if [[ "${targets}" = "" ]]; then
targets=(*${old}*)
total=${#targets[@]}
fi
# 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}"
echo -e "${status}"
# Warning on no change.
[[ "${target}" = "${new_name}" ]] && continue
# Rename.
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
# export for parallel.
2023-10-23 01:00:15 +03:00
export -f name name_hash name_hash_check name_ext