Skip to content

Instantly share code, notes, and snippets.

@donnaken15
Last active October 30, 2023 19:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save donnaken15/3644e2ad328a5c727d689ccb865da963 to your computer and use it in GitHub Desktop.
Save donnaken15/3644e2ad328a5c727d689ccb865da963 to your computer and use it in GitHub Desktop.
fail shell script to compress/decompress files for more physical free space, might be entirely useless, screw it
#!/bin/sh
# CYGWIN REQUIRED!! (maybe)
#
# optimal compact
# by wesley
# BECAUSE SOMEHOW IT TAKES MORE SPACE
# sometimes
#
# might've just been created out of
# the false assumption that it does
# take up more space but not until
# the computer is rebooted because
# everything entered always
# remains compressed
echo "$(tput setaf 14)Optimal Compact$(tput setaf 15) - $(tput setaf 10)Wesley$(tput sgr0)"
echo "$(tput setaf 3)/!\\ Make sure there's no writing/deleting operations happening while this runs.$(tput sgr0)"
echo
[ $# -eq 0 ] && { echo "No paths specified."; exit 1; }
rc=$(tput sgr0); NL=$'\n';
#tc_bl=$(tput setaf 0)
#tc_dr=$(tput setaf 1)
#tc_dg=$(tput setaf 2)
#tc_dy=$(tput setaf 3)
#tc_db=$(tput setaf 4)
#tc_dp=$(tput setaf 5)
#tc_dc=$(tput setaf 6)
#tc_lg=$(tput setaf 7)
tc_gr=$(tput setaf 8)
tc_r=$(tput setaf 9)
tc_g=$(tput setaf 10)
#tc_y=$(tput setaf 11)
#tc_b=$(tput setaf 12)
#tc_p=$(tput setaf 13)
#tc_c=$(tput setaf 14)
tc_w=$(tput setaf 15)
# capture groups for compact command output
cpt="\s\?\([0-9]\+\)\s:\s*\([0-9]\+\)\s=\s[0-9]\+\.[0-9]\+\sto\s1\s\?\([ C]\)\s\?\(.\+\)\$"
# size of disk that the file is on
dsize() { echo $(df --output=avail -- "$1" | tail -1); } # prints kb
fs() { expr $(du -b -- "$1" | awk '{print $1}') / 1000; } # file size
fsod() { du -h -- "$1" | awk '{print $1}'; } # file size
optc() {
cinf=$(compact "$1"|sed -n 5p)
ic=$(echo $cinf|sed "s/$cpt/\3/") # is compressed
[ "$ic" = "C" ] && { ic="U";w="Unc"; } || { ic="C";w="C"; }
echo "${tc_w}${w}ompressing $1${rc}"
echo "${tc_w}File size: $(fs "$1")K, on disk: $(fsod "$1")${rc}"
# size before
b=$(dsize "$1")
compact /$ic "$1" > /dev/null
# size after
a=$(dsize "$1")
revert=0 # if 1, set back to compression state before
echo "${tc_gr}Is this file actually smaller?${rc}"
echo "${tc_gr}Free space:${rc}"
printf "${tc_w}Before: ${tc_gr}%10dK\n${tc_w}After : ${tc_gr}%10dK${rc}\n" $b $a
[ $a -gt $b ] && { s="+";c="${tc_g}"; } || { s="";c="${tc_r}"; }
printf "${tc_w}Diff : ${c}%10sK\n${rc}" "${s}$(expr $a - $b)"
if [ $b -gt $a ]; then
[ "$ic" = "C" ] && { echo "${tc_r}No.${rc}"; } || { echo "${tc_g}Yes!${rc}";revert=1; }
else
[ "$ic" = "C" ] && { echo "${tc_g}Yes!${rc}"; } || { echo "${tc_r}No.${rc}";revert=1; }
fi
while [ $revert -eq 1 ]; do
echo "Reverting compression state..."
[ "$ic" = "C" ] && { ic="U"; } || { ic="C"; }
b=$(dsize "$1")
compact /$ic "$1" > /dev/null
a=$(dsize "$1")
[ $b -gt $a ] && { [ "$ic" = "C" ] && echo "ACTUALLY NOT SMALLER!"; continue; } || break
done
echo
}
optc_d() {
find "$1" -mindepth 1 -type f | while IFS= read -r f; do optc "$f"; done
}
for f in "$@"; do
if [ -f "$f" ]; then
optc "$f"
elif [ -d "$f" ]; then
optc_d "$f"
else
echo "Invalid path or it does not exist: $f"
fi
done
echo Done.
@donnaken15
Copy link
Author

donnaken15 commented Jul 13, 2023

mainly written for bash, but then optimized for zsh (now dash)

@donnaken15
Copy link
Author

sometimes compressed files even after reboot will take less space when decompressing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment