move compress/extract functions from lfrc to its own script compress.sh

chromebook
Alex 2022-05-27 15:43:58 -05:00
parent 42cc8dc3a5
commit 27672016e0
2 changed files with 86 additions and 31 deletions

View File

@ -88,40 +88,11 @@ cmd delete ${{
# extract the current file with the right command # extract the current file with the right command
# (xkcd link: https://xkcd.com/1168/) # (xkcd link: https://xkcd.com/1168/)
cmd extract ${{ cmd extract ${{
set -f compress.sh -e "$fx"
DIR="$(basename "$f" | cut -f 1 -d '.')"
mkdir "$DIR"
case $f in
*.tar.bz|*.tar.bz2|*.tbz|*.tbz2) tar xjvf "$f" --directory="$DIR";;
*.tar.gz|*.tgz) tar xzvf "$f" --directory="$DIR";;
*.tar.xz|*.txz) tar xJvf "$f" --directory="$DIR";;
*.zip) unzip "$f" -d "$DIR";;
*.rar) unrar x "$f" "$DIR";;
*.7z | *.crx) 7z x "$f" -o"$DIR";;
esac
}} }}
cmd compress ${{ cmd compress ${{
set -f compress.sh -c "$fx"
NAME="$(basename "$fx")"
if ! [ -d "$NAME" ]; then
NAME="$(basename "$PWD")"
mkdir "$NAME"
cp -r $fx "$NAME"
fi
echo "enter compression type [tar.gz, zip, 7z(default)]: "
read TYPE
case "$TYPE" in
tar.gz) tar czf "$NAME".tar.gz "$NAME";;
zip) zip -r "$NAME".zip "$NAME";;
*) 7z a "$NAME".7z "$NAME";;
esac
! [ -d "$fx" ] && rm -rf "$NAME"
}} }}
map c compress $fx map c compress $fx

84
.local/scripts/compress.sh Executable file
View File

@ -0,0 +1,84 @@
#!/bin/sh
mkname() {
FILES="$( echo "$@" | head -1 - )"
NAME="$(echo "$FILES" | awk '{print $1}' | xargs -0 basename | cut -f 1 -d '.')"
#C=0 #create counter variable in case of infinite loop
#while :
#do
# case "$NAME" in
# *.*) NAME="$(echo "$NAME" | cut -f 2 -d '.')" ;; #trim file extensions
# #.*) NAME="$(echo "$NAME" | sed 's/^\.//g')" ;; #trim leading periods
# /*) NAME="$(echo "$NAME" | sed 's/^\///g')" ;; #trim leading slashes
# *) break;; #break if all above conditions are gone
# esac
#
# C=$((C + 1))
# [ $C -gt 10 ] && break #if counter limit reached break loop
#done
echo "$NAME"
}
compress() {
set -f
FILES="$(echo "$@" | xargs -0)"
DIRNAME="$(basename "$FILES")"
if ! [ -d "$NAME" ]; then
DIRNAME="$(mkname "$@")"
echo "making directory $DIRNAME"
mkdir "$DIRNAME"
for f in $FILES
do
cp -r "$f" "$DIRNAME"
done
DELFLAG="TRUE" #set delete flag if we created a temp directory
fi
echo "enter compression type [tar.gz, zip, 7z(default)]: "
read TYPE
case "$TYPE" in
tar.gz) tar czf "$DIRNAME".tar.gz "$DIRNAME";;
zip) zip -r "$DIRNAME".zip "$DIRNAME";;
*) 7z a "$DIRNAME".7z "$DIRNAME";;
esac
[ "$DELFLAG" ] && rm -rf "$DIRNAME"
}
extract() {
set -f
FILE="$(echo "$1" | xargs)" #use xargs to trim whitespace
DIR="$(basename "$FILE" | cut -f 1 -d '.')"
mkdir "$DIR"
case $FILE in
*.tar.bz|*.tar.bz2|*.tbz|*.tbz2) tar xjvf "$FILE" --directory="$DIR";;
*.tar.gz|*.tgz) tar xzvf "$FILE" --directory="$DIR";;
*.tar.xz|*.txz) tar xJvf "$FILE" --directory="$DIR";;
*.zip) unzip "$FILE" -d "$DIR";;
*.rar) unrar x "$FILE" "$DIR";;
*.7z | *.crx) 7z x "$FILE" -o"$DIR";;
esac
}
help () {
echo "script to compress/extract files using tar/zip/rar"
echo " -c [ files/directories ] => compress input files/directories"
echo " -e [ file ] => extract input file to a subdirectory of the files name"
}
#get all input except first 2 chars as input for compress/extract
ARGS="$( echo "$@" | sed '0,/^../{s/^..//}' )"
if [ "$1" = "-c" ]; then
compress "$ARGS"
elif [ "$1" = "-e" ]; then
extract "$ARGS"
else
help
fi