Skip to content

Instantly share code, notes, and snippets.

@victorgabrielbs
Created December 18, 2025 02:07
Show Gist options
  • Select an option

  • Save victorgabrielbs/67b0cb9f6a0c9deeefdd997c1f7b718a to your computer and use it in GitHub Desktop.

Select an option

Save victorgabrielbs/67b0cb9f6a0c9deeefdd997c1f7b718a to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Script de compactação baseado em desc
# Uso: comp <formato> <arquivo1> [arquivo2] ... [diretorio1] ...
# Exemplo: comp tar.gz arquivo.txt diretorio/
if [ $# -lt 2 ]; then
echo "Uso: comp <formato> <arquivo1> [arquivo2] ... [diretorio1] ..."
echo "Formatos suportados: tar.bz2, tar.gz, bz2, gz, tar, tbz2, tbz, tgz, zip, 7z, tar.xz, tar.zst, xz, zst, lz, lzma"
exit 1
fi
format=$1
shift
files="$@"
if [ -z "$files" ]; then
echo "Erro: Nenhum arquivo ou diretório especificado"
exit 1
fi
case $format in
*.tar.bz2|tar.bz2)
if [ $# -eq 1 ] && [ -d "$1" ]; then
tar cjf "${1%/}.tar.bz2" "$1"
else
tar cjf "archive.tar.bz2" "$@"
fi
;;
*.tar.gz|tar.gz)
if [ $# -eq 1 ] && [ -d "$1" ]; then
tar czf "${1%/}.tar.gz" "$1"
else
tar czf "archive.tar.gz" "$@"
fi
;;
*.bz2|bz2)
for file in "$@"; do
if [ -f "$file" ]; then
bzip2 "$file"
else
echo "'$file' não é um arquivo válido para bz2"
fi
done
;;
*.gz|gz)
for file in "$@"; do
if [ -f "$file" ]; then
gzip "$file"
else
echo "'$file' não é um arquivo válido para gz"
fi
done
;;
*.tar|tar)
if [ $# -eq 1 ] && [ -d "$1" ]; then
tar cf "${1%/}.tar" "$1"
else
tar cf "archive.tar" "$@"
fi
;;
*.tbz2|tbz2)
if [ $# -eq 1 ] && [ -d "$1" ]; then
tar cjf "${1%/}.tbz2" "$1"
else
tar cjf "archive.tbz2" "$@"
fi
;;
*.tbz|tbz)
if [ $# -eq 1 ] && [ -d "$1" ]; then
tar cjf "${1%/}.tbz" "$1"
else
tar cjf "archive.tbz" "$@"
fi
;;
*.tgz|tgz)
if [ $# -eq 1 ] && [ -d "$1" ]; then
tar czf "${1%/}.tgz" "$1"
else
tar czf "archive.tgz" "$@"
fi
;;
*.zip|zip)
zip -r "archive.zip" "$@"
;;
*.7z|7z)
7z a "archive.7z" "$@"
;;
*.tar.xz|tar.xz)
if [ $# -eq 1 ] && [ -d "$1" ]; then
tar cJf "${1%/}.tar.xz" "$1"
else
tar cJf "archive.tar.xz" "$@"
fi
;;
*.tar.zst|tar.zst)
if [ $# -eq 1 ] && [ -d "$1" ]; then
tar --zstd -cf "${1%/}.tar.zst" "$1"
else
tar --zstd -cf "archive.tar.zst" "$@"
fi
;;
*.xz|xz)
for file in "$@"; do
if [ -f "$file" ]; then
xz "$file"
else
echo "'$file' não é um arquivo válido para xz"
fi
done
;;
*.zst|zst)
for file in "$@"; do
if [ -f "$file" ]; then
zstd "$file"
elif [ -d "$file" ]; then
tar --zstd -cf "${file%/}.tar.zst" "$file"
else
echo "'$file' não é um arquivo ou diretório válido"
fi
done
;;
*.lz|lz)
for file in "$@"; do
if [ -f "$file" ]; then
lzip "$file"
else
echo "'$file' não é um arquivo válido para lz"
fi
done
;;
*.lzma|lzma)
for file in "$@"; do
if [ -f "$file" ]; then
lzma "$file"
else
echo "'$file' não é um arquivo válido para lzma"
fi
done
;;
*)
echo "'$format' não é um formato de compactação suportado"
echo "Formatos suportados: tar.bz2, tar.gz, bz2, gz, tar, tbz2, tbz, tgz, zip, 7z, tar.xz, tar.zst, xz, zst, lz, lzma"
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment