bash : add progress counter to archive.
This commit is contained in:
parent
f0c4f3fef8
commit
fa4c85ade8
|
@ -2,22 +2,29 @@
|
||||||
archive()
|
archive()
|
||||||
{
|
{
|
||||||
local target="$@" # target file(s).
|
local target="$@" # target file(s).
|
||||||
|
local count=1
|
||||||
|
local total=$#
|
||||||
|
|
||||||
# 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)
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# iterate each file.
|
# iterate each file.
|
||||||
for file in $target; do
|
for file in $target; do
|
||||||
# print archive name.
|
# status info.
|
||||||
echo "$file"
|
local status="[$count/$total] $file"
|
||||||
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,22 +32,29 @@ archive()
|
||||||
archive_fast()
|
archive_fast()
|
||||||
{
|
{
|
||||||
local target="$@" # target file(s).
|
local target="$@" # target file(s).
|
||||||
|
local count=1
|
||||||
|
local total=$#
|
||||||
|
|
||||||
# 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)
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# iterate each file.
|
# iterate each file.
|
||||||
for file in $target; do
|
for file in $target; do
|
||||||
# print archive name.
|
# status info.
|
||||||
echo "$file"
|
local status="[$count/$total] $file"
|
||||||
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,14 +64,20 @@ archive_check()
|
||||||
local target="$@" # target file(s).
|
local target="$@" # target file(s).
|
||||||
local saved # saved hash value.
|
local saved # saved hash value.
|
||||||
local actual # actual file hash value.
|
local actual # actual file hash value.
|
||||||
|
local count=1
|
||||||
|
local total=$#
|
||||||
|
|
||||||
# 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)
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# iterate each file.
|
# iterate each file.
|
||||||
for file in $target; do
|
for file in $target; do
|
||||||
|
# status info.
|
||||||
|
local status="[$count/$total] $file"
|
||||||
|
|
||||||
# extract hash from name.
|
# extract hash from name.
|
||||||
saved="${file##*_}"
|
saved="${file##*_}"
|
||||||
saved="${saved%%.*}"
|
saved="${saved%%.*}"
|
||||||
|
@ -67,10 +87,13 @@ archive_check()
|
||||||
|
|
||||||
# compare hashes, show error on mismatch.
|
# compare hashes, show error on mismatch.
|
||||||
if [[ "$actual" = "$saved" ]]; then
|
if [[ "$actual" = "$saved" ]]; then
|
||||||
echo "$file: OK."
|
echo "${status}: OK."
|
||||||
else
|
else
|
||||||
echo "$file: failed."
|
echo "${status}: failed."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# increment counter.
|
||||||
|
((count++))
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -80,14 +103,20 @@ unarchive()
|
||||||
local target="$@" # target file(s).
|
local target="$@" # target file(s).
|
||||||
local saved # saved hash value.
|
local saved # saved hash value.
|
||||||
local actual # actual file hash value.
|
local actual # actual file hash value.
|
||||||
|
local count=1
|
||||||
|
local total=$#
|
||||||
|
|
||||||
# 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)
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# iterate each file.
|
# iterate each file.
|
||||||
for file in $target; do
|
for file in $target; do
|
||||||
|
# status info.
|
||||||
|
local status="[$count/$total] $file"
|
||||||
|
|
||||||
# extract hash from name.
|
# extract hash from name.
|
||||||
saved="${file##*_}"
|
saved="${file##*_}"
|
||||||
saved="${saved%%.*}"
|
saved="${saved%%.*}"
|
||||||
|
@ -97,11 +126,14 @@ unarchive()
|
||||||
|
|
||||||
# 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 "$file: OK."
|
echo "${status}: OK."
|
||||||
tar -xf "$file"
|
tar -xf "$file"
|
||||||
else
|
else
|
||||||
echo "$file: failed."
|
echo "${status}: failed."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# increment counter.
|
||||||
|
((count++))
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in a new issue