bash : add progress counter to archive.

This commit is contained in:
Dmitry Voronin 2023-10-03 06:50:40 +03:00
parent f0c4f3fef8
commit fa4c85ade8

View file

@ -2,22 +2,29 @@
archive()
{
local target="$@" # target file(s).
local count=1
local total=$#
# set dafult value to target all supported archives.
if [[ "$target" = "" ]]; then
target="*"
total=$(ls | wc -l)
fi
# iterate each file.
for file in $target; do
# print archive name.
echo "$file"
# status info.
local status="[$count/$total] $file"
echo "${status}"
# create archive.
tar -c "$file" | pv -s $(du -sb "$file" | awk '{print $1}') | xz -9e > "${file%/*}".tar.xz
# append hash to file name.
mv "${file%/*}".tar.xz "${file%/*}"_$(sha1sum "${file%/*}".tar.xz | cut -d\ -f1).tar.xz
# increment counter.
((count++))
done
}
@ -25,22 +32,29 @@ archive()
archive_fast()
{
local target="$@" # target file(s).
local count=1
local total=$#
# set dafult value to target all supported archives.
if [[ "$target" = "" ]]; then
target="*"
total=$(ls | wc -l)
fi
# iterate each file.
for file in $target; do
# print archive name.
echo "$file"
# status info.
local status="[$count/$total] $file"
echo "${status}"
# create archive.
tar -c "$file" | pv -s $(du -sb "$file" | awk '{print $1}') | gzip -1 > "${file%/*}".tar.gz
# append hash to file name.
mv "${file%/*}".tar.gz "${file%/*}"_$(sha1sum "${file%/*}".tar.gz | cut -d\ -f1).tar.gz
# increment counter.
((count++))
done
}
@ -50,14 +64,20 @@ archive_check()
local target="$@" # target file(s).
local saved # saved hash value.
local actual # actual file hash value.
local count=1
local total=$#
# set dafult value to target all supported archives.
if [[ "$target" = "" ]]; then
target="*_*.tar.*"
total=$(ls ${target} | wc -l)
fi
# iterate each file.
for file in $target; do
# status info.
local status="[$count/$total] $file"
# extract hash from name.
saved="${file##*_}"
saved="${saved%%.*}"
@ -67,10 +87,13 @@ archive_check()
# compare hashes, show error on mismatch.
if [[ "$actual" = "$saved" ]]; then
echo "$file: OK."
echo "${status}: OK."
else
echo "$file: failed."
echo "${status}: failed."
fi
# increment counter.
((count++))
done
}
@ -80,14 +103,20 @@ unarchive()
local target="$@" # target file(s).
local saved # saved hash value.
local actual # actual file hash value.
local count=1
local total=$#
# set dafult value to target all supported archives.
if [[ "$target" = "" ]]; then
target="*_*.tar.*"
total=$(ls ${target} | wc -l)
fi
# iterate each file.
for file in $target; do
# status info.
local status="[$count/$total] $file"
# extract hash from name.
saved="${file##*_}"
saved="${saved%%.*}"
@ -97,11 +126,14 @@ unarchive()
# extract if hash matched or show error if not.
if [[ "$saved" = "$actual" ]]; then
echo "$file: OK."
echo "${status}: OK."
tar -xf "$file"
else
echo "$file: failed."
echo "${status}: failed."
fi
# increment counter.
((count++))
done
}