fastdu, and arhiver improvements

This commit is contained in:
Ville Rantanen
2016-02-16 13:34:31 +02:00
parent a48183e19d
commit f131284caa
4 changed files with 59 additions and 12 deletions

1
bin/fastdu Symbolic link
View File

@@ -0,0 +1 @@
../files/fastdu

View File

@@ -7,32 +7,39 @@ function helpexit() {
echo " -n No compression. Exit after tar archive"
exit
}
function listfolders() {
find "$1" -mindepth 1 -maxdepth 1 -type d | sort | tr '\n' '\000'
}
function fsize() {
echo -en "\e[2K\r"
[[ -z "$2" ]] || echo -n "$2 -> "
ls -sh "$1" | xargs echo -n
}
[[ "$1" = "-h" ]] && helpexit
echo 'Existing files will be overwritten!'
echo Archive the following folders:
find . -mindepth 1 -maxdepth 1 -type d | tr \\n " "
listfolders . | tr "[:cntrl:]" " "
echo ''
echo '<ctrl-c> to quit'
read foo
find . -mindepth 1 -maxdepth 1 -type d -print0 | while read -r -d "" d; do
listfolders . | while read -r -d "" d; do
echo -n $d
tar cvf "$d.tar" $d > "$d.tar.txt" &
tar cvf "$d.tar" "$d" > "$d.tar.lst" &
PID=$!
DU=$( nice fastdu -h "$d" & )
DUPID=$!
while kill -0 "$PID" &>/dev/null; do
[[ -f "$d.tar" ]] && fsize "$d.tar" "$DU"
for i in {1..5}; do
sleep $i; kill -0 "$PID" &>/dev/null || break 2
done
fsize "$d.tar"
done
fsize "$d.tar"
fsize "$d.tar" "$DU"
kill -0 "$DUPID" &>/dev/null || kill "$DUPID" &>/dev/null
echo ''
done
[[ "$1" = "-n" ]] && {
@@ -43,17 +50,17 @@ done
read foo
}
find . -mindepth 1 -maxdepth 1 -type d -print0 | while read -r -d "" d; do
ls -sh "$d.tar"
gzip "$d.tar"
listfolders . | while read -r -d "" d; do
DU=$( fastdu -h "$d.tar" )
gzip -f "$d.tar" &
PID=$!
while kill -0 "$PID" >/dev/null 2>&1; do
[[ -f "$d.tar.gz" ]] && fsize "$d.tar.gz" "$DU"
for i in {1..5}; do
sleep $i; kill -0 "$PID" &>/dev/null || break 2
done
fsize "$d.tar.gz"
done
fsize "$d.tar.gz"
fsize "$d.tar.gz" "$DU"
echo ''
done

39
files/fastdu Executable file
View File

@@ -0,0 +1,39 @@
#!/bin/bash
function helpexit() {
echo "Display file/folder sizes like 'du', but faster and actual byte sizes"
echo "Returns only the size as number"
echo " -h Human readable units"
echo " --help This help"
exit
}
function filesize {
# Return a human readable size from integer of bytes
# Usage: filesize 10000
[ "$1" = "0" ] && {
echo "0 B"
return 0
}
awk 'BEGIN{ x = '$1'
split("B KB MB GB TB PB EB ZB",type)
for(i=7;y < 1;i--)
y = x / (2^(10*i))
str=int(y*10)/10 " " type[i+2]
if (x==0) { str = "0 B" }
print str
}' || return $?
}
[[ -z "$1" ]] && what="."
[[ "$1" = "-h" ]] && { HUMAN=1; shift 1; }
[[ "$1" = "--help" ]] && helpexit
SIZE=$( find $what "$@" -type f -printf %s"\n" | awk '{ sum += $1 } END { print int(sum)+0 }' )
[[ "$HUMAN" -eq 1 ]] && {
filesize "$SIZE"
exit
}
echo $SIZE

2
rc
View File

@@ -13,7 +13,7 @@ export PATH
function _self_update() {
touch "$TOOLSPATH"/.lastupdate
pushd "$TOOLSPATH" > /dev/null
hg pull -u > /dev/null 2>&1
timeout 5 hg pull -u > /dev/null 2>&1
popd > /dev/null
}