bash : refactor some scripts.

This commit is contained in:
Dmitry Voronin 2023-10-29 21:26:22 +03:00
parent 644b335458
commit 40774fd82e
3 changed files with 156 additions and 153 deletions

View file

@ -2,29 +2,29 @@
archive() archive()
{ {
local target="$@" # target file(s). local target="$@" # target file(s).
local count=1 # processed count. local count=0 # processed count.
local total=$# # total to process. local total=$# # total to process.
# set dafult value to target all supported archives. # set dafult value to target all supported archives.
if [[ "$target" = "" ]]; then if [[ "${target}" = "" ]]; then
target="*" target="*"
total=$(ls | wc -l) total=$(ls | wc -l)
fi fi
# iterate each file. # iterate each file.
for file in $target; do for file in ${target}; do
# increment counter.
((count++))
# status info. # status info.
local status="[$count/$total] $file" local status="[${count}/${total}] ${file}"
echo "${status}" echo "${status}"
# create archive. # create archive.
tar -c "$file" | pv -s $(du -sb "$file" | awk '{print $1}') | xz -9e > "${file%/*}".tar.xz tar -c "${file}" | pv -s $(du -sb "${file}" | awk '{print $1}') | xz -9e > "${file%/*}".tar.xz
# append hash to file name. # append hash to file name.
mv "${file%/*}".tar.xz "${file%/*}"_$(sha1sum "${file%/*}".tar.xz | cut -d\ -f1).tar.xz mv "${file%/*}".tar.xz "${file%/*}"_$(sha1sum "${file%/*}".tar.xz | cut -d\ -f1).tar.xz
# increment counter.
((count++))
done done
} }
@ -32,29 +32,29 @@ archive()
archive_fast() archive_fast()
{ {
local target="$@" # target file(s). local target="$@" # target file(s).
local count=1 # processed count. local count=0 # processed count.
local total=$# # total to process. local total=$# # total to process.
# set dafult value to target all supported archives. # set dafult value to target all supported archives.
if [[ "$target" = "" ]]; then if [[ "${target}" = "" ]]; then
target="*" target="*"
total=$(ls | wc -l) total=$(ls | wc -l)
fi fi
# iterate each file. # iterate each file.
for file in $target; do for file in ${target}; do
# increment counter.
((count++))
# status info. # status info.
local status="[$count/$total] $file" local status="[${count}/${total}] ${file}"
echo "${status}" echo "${status}"
# create archive. # create archive.
tar -c "$file" | pv -s $(du -sb "$file" | awk '{print $1}') | gzip -1 > "${file%/*}".tar.gz tar -c "${file}" | pv -s $(du -sb "${file}" | awk '{print $1}') | gzip -1 > "${file%/*}".tar.gz
# append hash to file name. # append hash to file name.
mv "${file%/*}".tar.gz "${file%/*}"_$(sha1sum "${file%/*}".tar.gz | cut -d\ -f1).tar.gz mv "${file%/*}".tar.gz "${file%/*}"_$(sha1sum "${file%/*}".tar.gz | cut -d\ -f1).tar.gz
# increment counter.
((count++))
done done
} }
@ -62,29 +62,30 @@ archive_fast()
archive_check() archive_check()
{ {
local target="$@" # target file(s). local target="$@" # target file(s).
local saved # saved hash value. local count=0 # processed count.
local actual # actual file hash value.
local count=1 # processed count.
local total=$# # total to process. local total=$# # total to process.
local failed=0 # total failed checks. local failed=0 # total failed checks.
# set dafult value to target all supported archives. # set dafult value to target all supported archives.
if [[ "$target" = "" ]]; then if [[ "${target}" = "" ]]; then
target="*_*.tar.*" target="*_*.tar.*"
total=$(ls ${target} | wc -l) total=$(ls ${target} | wc -l)
fi fi
# iterate each file. # iterate each file.
for file in $target; do for file in ${target}; do
# increment counter.
((count++))
# status info. # status info.
local status="[$count/$total] $file" local status="[${count}/${total}] ${file}"
# extract hash from name. # extract hash from name.
saved="${file##*_}" local saved="${file##*_}"
saved="${saved%%.*}" saved="${saved%%.*}"
# calculate actual hash. # calculate actual hash.
actual="$(pv ${file} | sha1sum | cut -d\ -f1)" local actual="$(pv ${file} | sha1sum | cut -d\ -f1)"
# compare hashes, show error on mismatch. # compare hashes, show error on mismatch.
if [[ "${actual}" = "${saved}" ]]; then if [[ "${actual}" = "${saved}" ]]; then
@ -93,16 +94,15 @@ archive_check()
echo -e "${color_red}${status}: failed.${color_default}" echo -e "${color_red}${status}: failed.${color_default}"
((failed++)) ((failed++))
fi fi
# increment counter.
((count++))
done done
# report result. # report result.
if [[ ${failed} -gt 0 ]]; then if [[ ${count} -gt 1 ]]; then
echo -e "${color_bred}Items failed to validate: ${failed}.${color_default}" if [[ ${failed} -gt 0 ]]; then
else echo -e "${color_bred}Items failed to validate: ${failed}.${color_default}"
echo -e "${color_green}All successful.${color_default}" else
echo -e "${color_green}All successful.${color_default}"
fi
fi fi
} }
@ -110,39 +110,37 @@ archive_check()
unarchive() unarchive()
{ {
local target="$@" # target file(s). local target="$@" # target file(s).
local saved # saved hash value. local count=0 # processed count.
local actual # actual file hash value.
local count=1 # processed count.
local total=$# # total to process. local total=$# # total to process.
# set dafult value to target all supported archives. # set dafult value to target all supported archives.
if [[ "$target" = "" ]]; then if [[ "${target}" = "" ]]; then
target="*_*.tar.*" target="*_*.tar.*"
total=$(ls ${target} | wc -l) total=$(ls ${target} | wc -l)
fi fi
# iterate each file. # iterate each file.
for file in $target; do for file in ${target}; do
# increment counter.
((count++))
# status info. # status info.
local status="[$count/$total] $file" local status="[${count}/${total}] ${file}"
# extract hash from name. # extract hash from name.
saved="${file##*_}" local saved="${file##*_}"
saved="${saved%%.*}" saved="${saved%%.*}"
# calculate actual hash. # calculate actual hash.
actual="$(sha1sum $file | cut -d\ -f1)" local actual="$(sha1sum ${file} | cut -d\ -f1)"
# extract if hash matched or show error if not. # extract if hash matched or show error if not.
if [[ "$saved" = "$actual" ]]; then if [[ "${saved}" = "${actual}" ]]; then
echo "${status}: OK." echo "${status}: OK."
tar -xf "$file" tar -xf "${file}"
else else
echo "${status}: failed." echo "${status}: failed."
fi fi
# increment counter.
((count++))
done done
} }
@ -174,7 +172,7 @@ archive_rename()
# export everything, primarily for use with parallel.. # export everything, primarily for use with parallel..
export -f archive archive_fast archive_check unarchive export -f archive archive_fast archive_check unarchive
# autocompletes. # autocomplete.
_archive_list() _archive_list()
{ {
_autocomplete "$(ls *_*.tar.* 2> /dev/null)" _autocomplete "$(ls *_*.tar.* 2> /dev/null)"

View file

@ -3,21 +3,22 @@
name() name()
{ {
local files="$@" local files="$@"
local count=1 local count=0
local total=$# local total=$#
# all files except hidden by default. # all files except hidden by default.
if [[ "$files" = "" ]]; then if [[ "${files}" = "" ]]; then
files="[^.]*" files="[^.]*"
total=$(ls | wc -l) total=$(ls | wc -l)
fi fi
# process. # process.
for file in $files; do for file in ${files}; do
local new_name # increment count.
((count++))
# extract new name. # extract new name.
new_name=$(echo "$file" | \ local new_name=$(echo "${file}" | \
sed -e "s/ /_/g" \ sed -e "s/ /_/g" \
-e "s/[^[:alnum:]\._-]//g" \ -e "s/[^[:alnum:]\._-]//g" \
-e "s/_\+/_/g" -e "s/\.\+/\./g" -e "s/-\+/-/g" \ -e "s/_\+/_/g" -e "s/\.\+/\./g" -e "s/-\+/-/g" \
@ -27,29 +28,25 @@ name()
) )
# status line. # status line.
local status="[$count/$total] $file -> $new_name" local status="[${count}/${total}] ${file} -> ${new_name}"
# check if same name. # check if same name.
if [[ "$file" = "$new_name" ]]; then if [[ "${file}" = "${new_name}" ]]; then
echo -e "${color_green}$status: no change.${color_default}" echo -e "${color_green}${status}: no change.${color_default}"
((count++))
continue continue
fi fi
# check if file name already exists. # check if file name already exists.
if [[ -f "$new_name" ]]; then if [[ -f "${new_name}" ]]; then
echo -e "${color_red}$status: already exists!${color_default}" echo -e "${color_red}${status}: already exists!${color_default}"
return 1 return 1
fi fi
# rename file. # rename file.
mv -- "$file" "$new_name" &> /dev/null mv -- "${file}" "${new_name}" &> /dev/null
# show change. # show change.
echo "$status" echo "${status}"
# increment count.
((count++))
done done
} }
@ -58,45 +55,48 @@ name()
name_hash() name_hash()
{ {
local files="$@" local files="$@"
local count=1 local count=0
local total=$# local total=$#
# all files except hidden by default. # all files except hidden by default.
if [[ "$files" = "" ]]; then if [[ "${files}" = "" ]]; then
files="[^.]*" files="[^.]*"
total=$(ls -p | grep -v / | wc -l) total=$(ls -p | grep -v / | wc -l)
fi fi
# process. # process.
for file in $files; do for file in ${files}; do
local extension
local new_name
# process only files. # process only files.
if [[ -f "$file" ]]; then if [[ -f "${file}" ]]; then
# extract new name. # increment count.
extension="${file##*.}" ((count++))
if [ -f "$file" ] && [ "$extension" != "$file" ]; then
new_name="${file%$extension}" # extract extension.
else local extension="${file##*.}"
new_name="$file" if [[ "${extension}" = "${file}" ]]; then
extension="" extension=""
fi
new_name=$(sha1sum -- "$file" | cut -d\ -f1)
if [[ "$extension" != "" ]]; then
new_name="${new_name,,}.$extension"
else else
new_name="${new_name,,}" extension=".${extension}"
fi
# hash the new name.
local hash=$(sha1sum -- "${file}" | cut -d\ -f1)
new_name="${hash,,}${extension}"
# prepare status.
local status="[${count}/${total}] ${file} -> ${new_name}"
# check if same name.
if [[ "${file}" = "${new_name}" ]]; then
echo -e "${color_green}${status}: no change.${color_default}"
continue
fi fi
# rename file. # rename file.
mv -- "$file" "$new_name" &> /dev/null mv -- "${file}" "${new_name}" &> /dev/null
# show change. # show change.
echo "[$count/$total] $file -> $new_name" echo -e "${status}"
# increment count.
((count++))
fi fi
done done
} }
@ -105,166 +105,170 @@ name_hash()
# usage: name_hash_check [FILES] # usage: name_hash_check [FILES]
name_hash_check() name_hash_check()
{ {
local stored
local actual
local files="$@" local files="$@"
local count=1 local count=0
local total=$# local total=$#
local failed=0 local failed=0
# all files by default. # all files by default.
if [[ "$files" = "" ]]; then if [[ "${files}" = "" ]]; then
files="[^.]*" files="[^.]*"
total=$(ls -p | grep -v / | wc -l) total=$(ls -p | grep -v / | wc -l)
fi fi
# process. # process.
for file in $files; do for file in ${files}; do
# process only files. # process only files.
if [[ -f "$file" ]]; then if [[ -f "${file}" ]]; then
# increment count.
((count++))
# status info. # status info.
local status="[$count/$total] $file" local status="[${count}/${total}] ${file}"
# extract hashes. # extract hashes.
stored="${file%%.*}" local stored="${file%%.*}"
actual=$(sha1sum -- "$file" | cut -d\ -f1) local actual=$(sha1sum -- "${file}" | cut -d\ -f1)
# compare hashes. # compare hashes.
if [[ "$stored" = "$actual" ]]; then if [[ "${stored}" = "${actual}" ]]; then
echo -e "${status}: OK." echo -e "${status}: OK."
else else
echo -e "${color_red}${status}: failed.${color_default}" echo -e "${color_red}${status}: failed.${color_default}"
((failed++)) ((failed++))
fi fi
# increment count.
((count++))
fi fi
done done
# report result. # report result.
if [[ ${failed} -gt 0 ]]; then if [[ ${count} -gt 1 ]]; then
echo -e "${color_bred}Items failed to validate: ${failed}.${color_default}" if [[ ${failed} -gt 0 ]]; then
else echo -e "${color_bred}Items failed to validate: ${failed}.${color_default}"
echo -e "${color_green}All successful.${color_default}" else
echo -e "${color_green}All successful.${color_default}"
fi
fi fi
} }
# 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]
name_series() name_series()
{ {
local season="$1" local season="${1}"
local files="${@:2}" local files="${@:2}"
local count=1 local count=0
local total=$#
# error when no season number specified. # error when no season number specified.
if [[ "$season" = "" ]]; then if [[ "${season}" = "" ]]; then
echo "usage: name_series SEASON" echo "usage: name_series [SEASON]"
return 1 return 1
fi fi
# all files by default. # all files by default.
if [[ "$files" = "" ]]; then if [[ "${files}" = "" ]]; then
files="[^.]*" files="[^.]*"
total=$(ls -p | grep -v / | wc -l)
fi fi
# process. # process.
for file in $files; do for file in ${files}; do
local new_name
# process only files. # process only files.
if [[ -f "$file" ]]; then if [[ -f "${file}" ]]; then
# extract new name.
new_name="Episode S${season}E$(printf %02d $count).${file##*.}"
# rename file.
mv -- "$file" "$new_name" &> /dev/null
echo "$file -> $new_name"
# increment episode number. # increment episode number.
((count++)) ((count++))
# extract new name.
local new_name="Episode S${season}E$(printf %02d ${count}).${file##*.}"
# prepare status.
local status="[${count}/${total}] ${file} -> ${new_name}"
# rename file.
mv -- "${file}" "${new_name}" &> /dev/null
echo -e "${status}"
fi fi
done done
} }
# rename files for Kavita manga format. # rename files for Kavita manga format.
# usage: name_manga SEASON # usage: name_manga [SEASON]
name_manga() name_manga()
{ {
local season="$1" local season="$1"
local files="${@:2}" local files="${@:2}"
local count=1 local count=0
local total=$#
local manga="${PWD##*/}" local manga="${PWD##*/}"
# error when no season number specified. # error when no season number specified.
if [[ "$season" = "" ]]; then if [[ "${season}" = "" ]]; then
echo "usage: name_manga SEASON" echo "usage: name_manga [SEASON]"
return 1 return 1
fi fi
# all files by default. # all files by default.
if [[ "$files" = "" ]]; then if [[ "${files}" = "" ]]; then
files="[^.]*" files="[^.]*"
total=$(ls -p | grep -v / | wc -l)
fi fi
# process. # process.
for file in $files; do for file in ${files}; do
local new_name
# process only files. # process only files.
if [[ -f "$file" ]]; then if [[ -f "${file}" ]]; then
# extract new name.
new_name="${manga} Vol.${season} Ch.${count}.${file##*.}"
# rename file.
mv -- "$file" "$new_name" &> /dev/null
echo "$file -> $new_name"
# increment episode number. # increment episode number.
((count++)) ((count++))
# extract new name.
local new_name="${manga} Vol.${season} Ch.${count}.${file##*.}"
# prepare status.
local status="[${count}/${total}] ${file} -> ${new_name}"
# rename file.
mv -- "${file}" "${new_name}" &> /dev/null
echo -e "${status}"
fi fi
done done
} }
# rename files for new extension. # rename files for new extension.
# usage: name_ext EXTENSION [FILES] # usage: name_ext [EXTENSION] [FILES]
name_ext() name_ext()
{ {
local extension="$1" local extension="$1"
local files="${@:2}" local files="${@:2}"
local count=1 local count=0
local total=$# local total=$#
# error when no new extension specified. # error when no new extension specified.
if [[ "$extension" = "" ]]; then if [[ "${extension}" = "" ]]; then
echo "usage: name_ext EXTENSION [FILES]" echo "usage: name_ext [EXTENSION] [FILES]"
return 1 return 1
fi fi
# all files by default. # all files by default.
if [[ "$files" = "" ]]; then if [[ "${files}" = "" ]]; then
files="[^.]*" files="[^.]*"
total=$(ls -p | grep -v / | wc -l) total=$(ls -p | grep -v / | wc -l)
fi fi
# process. # process.
for file in $files; do for file in ${files}; do
local new_name
# process only files. # process only files.
if [[ -f "$file" ]]; then if [[ -f "${file}" ]]; then
# extract new name.
new_name="${file%.*}"."$extension"
# rename file.
mv -- "$file" "$new_name" &> /dev/null
# show change.
echo "[$count/$total] $file -> $new_name"
# increment count. # increment count.
((count++)) ((count++))
# extract new name.
local new_name="${file%.*}"."${extension}"
# rename file.
mv -- "${file}" "${new_name}" &> /dev/null
# show change.
echo "[${count}/${total}] ${file} -> ${new_name}"
fi fi
done done
} }

View file

@ -2,9 +2,10 @@
# usage: recursive [COMMAND] # usage: recursive [COMMAND]
recursive() recursive()
{ {
local IFS=$'\n'
local current="${PWD}" local current="${PWD}"
local dirs=$(find -type d) local dirs=$(find -type d)
local total=$(echo ${dirs} | wc -w) local total=$(find -type d | wc -l) # TODO: don't call find twice. won't work with "echo ${dirs}".
local count=0 local count=0
for dir in ${dirs}; do for dir in ${dirs}; do