Skip to content

Instantly share code, notes, and snippets.

@xim
Created November 22, 2010 23:12
Show Gist options
  • Save xim/710905 to your computer and use it in GitHub Desktop.
Save xim/710905 to your computer and use it in GitHub Desktop.
A wrapper around RAR(1) for making archives on the form .rar .r00 etc.
#!/bin/bash
# Make rar multiparts with the extensions .rar .r00 etc.
# Many programs prefer / look for this, not the .partX.rar
# Usage: r00 [archive name OR file1] [file(s)]
# Enable evil expansion mode: +([0-9])
shopt -s extglob
for arg in "$@" ; do
if test "$arg" == "--help" -o "$arg" == "-h" ; then
echo "Usage:
`basename $0` [archive name OR file1] [file(s)]
Note: If only one argument is given, and this does not exist, adds current working directory to archive of that name" >&2
exit 0
fi
done
if ! test -e "$1" ; then
export RAR="${1%.rar}"
shift
else
export RAR="temp"
fi
rar a -m0 -v102400 "$RAR" "$@"
exitcode=$?
if test $exitcode -ne 0 ; then
echo >&2
echo "rar exited with code $exitcode. Not renaming." >&2
exit $exitcode
fi
echo >&2
echo "Renaming archives. If the rar process didn't result in any multiparts, you will see some error messages here. They can safely be ignored." >&2
echo >&2
# This is the evil expansion mode =)
mv "$RAR".part*(0)1.rar "$RAR".rar
for file in "$RAR".part* ; do
file2="${file%.rar}"
# Haha, This is ugly syntax. Coerce to decimal base 10.
mv "$file" "$RAR".r$((10#${file2#$RAR.part}-2))
done
# Fix the files called .r[0-9]
# TODO: We just make assumptions on file size here. If we add a really huge
# file, it will make files .rar, .r00, .r01, ..., .r100, .r101 etc.
for file in "$RAR".r[0-9] ; do
mv "$file" "$RAR.r0${file#$RAR.r}"
done
echo "Files renamed to $RAR.r*" >&2
echo >&2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment