Skip to content

Instantly share code, notes, and snippets.

@fridim
Last active September 28, 2015 17:28
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 fridim/1471937 to your computer and use it in GitHub Desktop.
Save fridim/1471937 to your computer and use it in GitHub Desktop.
a small script to create archive (no option, can pass a file, a folder, a link ..)
#!/bin/sh
# Most of the time, you just want a .tar.gz of a folder, regardless of
# where you are. This is a small script that does it and put the result
# in /tmp. If you have xsel installed, it copied to your clipboard the
# destination file.
set -e
file="$1"
if [ ! -e "$file" ]; then
echo "File does not exist." >&2
exit -1
fi
if [ -L "$file" ]; then
file=$(readlink -f "$file")
fi
orig_size=$(du -sh "$file"|cut -f 1)
if [ -d "$file" ]; then
cd "$(dirname "$1")" || exit 1
filename="$(mktemp -d)/$(date +%Y%m%d)_$(basename "$file").tar.gz"
tar czf "$filename" "$(basename $file)"
else
filename="$(mktemp -d)/$(date +%Y%m%d)_$(basename "$file").gz"
gzip -c "$file" > "$filename"
fi
echo "$orig_size ---> $(du -sh "$filename"|cut -f 1)"
echo
echo "Your tarball : $filename"
if command -v xsel>/dev/null; then
echo -n "$filename"|xsel
fi
exit 0
@flagos
Copy link

flagos commented Dec 14, 2011 via email

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