From db41c5eb825dadccf3c463a29af6c138d175d433 Mon Sep 17 00:00:00 2001 From: desktop Date: Sun, 10 Dec 2023 04:31:03 +0300 Subject: [PATCH] archive : Add archive_rm. --- .config/bash/module/archive.sh | 57 +++++++++++++++++++++++++++++----- .doc/bash.md | 19 ++++++------ 2 files changed, 59 insertions(+), 17 deletions(-) diff --git a/.config/bash/module/archive.sh b/.config/bash/module/archive.sh index a27e55a..be0bef6 100644 --- a/.config/bash/module/archive.sh +++ b/.config/bash/module/archive.sh @@ -30,7 +30,7 @@ function archive() { # create archive. tar -c "${target}" | pv -s $(/usr/bin/du -sb "${target}" | awk '{print $1}') | xz -9e > "${name}".txz - + # append hash to target name. mv "${name}".txz "${name}"_${date}-$(pv "${name}".txz | sha1sum | cut -d\ -f1).txz @@ -78,7 +78,7 @@ function archive_fast() { # create archive. tar -c "${target}" | pv -s $(/usr/bin/du -sb "${target}" | awk '{print $1}') | gzip -1 > "${name}".tgz - + # append hash to target name. mv "${name}".tgz "${name}"_${date}-$(pv "${name}".tgz | sha1sum | cut -d\ -f1).tgz @@ -201,7 +201,7 @@ function unarchive() { local count=0 local total=${#} local failed=0 - + # set dafult value to target all supported archives. if [[ "${targets}" = "" ]]; then targets=($(ls | grep -E ${_archive_pattern})) @@ -220,7 +220,7 @@ function unarchive() { # extract hash from name. local data=($(_archive_parse "${target}")) local saved="${data[2]}" - + # calculate actual hash. local actual=$(pv "${target}" | sha1sum | cut -d\ -f1) @@ -259,7 +259,7 @@ function unarchive() { fi } -# Rename archives. +# Rename archives. # If no name specified, it simplifies archive's name. # If no archives specified, apply to all archives. # Usage: archive_name [ARCHIVE] [NAME] @@ -281,7 +281,7 @@ function archive_name() { for target in "${targets[@]}"; do # iterate counter. ((count++)) - + # simplify name by default. if [[ "${name}" = "" || ${count} -gt 1 ]]; then name="${target%_*}" @@ -350,7 +350,7 @@ function archive_convert() { for target in "${targets[@]}"; do local compression="${target##*.}" local new_compression - + case "${compression}" in "gz") new_compression="tgz" @@ -368,6 +368,47 @@ function archive_convert() { done } +# Delete specified or all archive files. +# Usage: archive_rm [FILES] +function archive_rm() { + local IFS=$'\n' + local targets=("${@}") + local total=${#} + local count=0 + local failed=0 + + # set dafult value to target all supported archives. + if [[ "${targets}" = "" ]]; then + targets=($(ls | grep -E ${_archive_pattern})) + total=${#targets[@]} + fi + + # iterate each target. + for target in "${targets[@]}"; do + # increment counter. + ((count++)) + + # status info. + local status="[${count}/${total}] ${target}" + echo -e "${status}" + + # Delete archive. + rm -- "${target}" + + # Show error. + if [[ ${?} != 0 ]]; then + ((failed++)) + echo -e "${color_bred}${status}: Failed.${color_default}" + fi + done + + # Show error. + if [[ ${failed} != 0 ]]; then + echo -e "${color_bred}Failed: ${failed}.${color_default}" + false + fi +} + # Parse archive file name to get: name, date, hash and format. # Usage: _archive_parse function _archive_parse() { @@ -414,5 +455,5 @@ function _archive_date() { date +%Y%m%d%H%M } -complete -o filenames -F _archive_grep archive_check unarchive +complete -o filenames -F _archive_grep archive_check unarchive archive_rm complete -o filenames -F _archive_name archive_name diff --git a/.doc/bash.md b/.doc/bash.md index 3174c22..81b37c6 100644 --- a/.doc/bash.md +++ b/.doc/bash.md @@ -25,8 +25,9 @@ Command|Description `archive_check [FILES]`|Check archives integrity. Checks all archives by default. `archive_prune [NAME]`|Delete old versions of archives. All archives by default. `unarchive [FILES]`|Extract previously created archive with checksum validation. -`archive_name [ARCHIVE] [NAME]`|Rename archives. If no name specified, it simplifies archive's name. If no archives specified, apply to all archives. +`archive_name [ARCHIVE] [NAME]`|Rename archives. If no name specified, it simplifies archive's name. If no archives specified, apply to all archives. `archive_convert`|Convert old archives to a new format. *TODO: remove me after some time when there won't be any old archives.* +`archive_rm [FILES]`|Delete specified or all archive files. ## Battery. @@ -107,12 +108,6 @@ Command|Description `df`|Show only physical drives info. `du [DIRS]`|Show combined size in SI. Current dir by default. -## Doc. - -Command|Description ----|--- -`doc_bash`|Generate a markdown file with all the help info. Outputs to `~/.doc/bash.md`. - ## Docker. Command|Description @@ -133,6 +128,12 @@ Command|Description `dcpu [SERVICES]`|Docker compose pull & up specified services. `dcul [SERVICES]`|Docker compose up & attach to logs for specified services. +## Doc. + +Command|Description +---|--- +`doc_bash`|Generate a markdown file with all the help info. Outputs to `~/.doc/bash.md`. + ## Ffmpeg. Command|Description @@ -268,7 +269,7 @@ Command|Description Command|Description ---|--- -`ps `|Find process and filter. +`ps [PROCESS]`|Find process and filter. ## Recursive. @@ -344,7 +345,7 @@ Command|Description Command|Description ---|--- -`vdl [LINK]`|Download video from URL. When no [LINK] specified, it tries to update previously downloaded link. +`vdl [LINK]`|Download video from URL. When no `[LINK]` specified, it tries to update previously downloaded link. `vdl_vk `|Temporary fix for vk downloads. `vdl_file `|Download all videos from file with links.