Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm looking for a flexible bash script to do the following:

  1. Convert .rar, .tar, .tar.gz, .bz2, .7z archives to .zip format
  2. Keep all folder structures and filenames as source archive.
  3. Convert it quietly, outputs "error" on failure and outputs "encrypted" on password protected archived.

Thanks in advance.

share|improve this question
I don't know if this will help, but IZarc can convert some archives from one format to another, and I think it has a command-line version. Not sure if the command-line version does conversion though. – user3914 Nov 27 '09 at 7:40
up vote 3 down vote accepted

I think you'll want to use a case statement to choose how to unpack the input archive based on the filename (or perhaps use file to base it on the content instead). Unpack the input archive to a temporary directory, piping stdout/stdin to /dev/null or a file. Then run zip on the contents of the temporary directory, saving to a filename provided on the commandline. Remove the temporary directory.

Something like this (UNTESTED):

# Add syntax checking here
tempdir=`mktemp -d`
case "$infile" in
        tar -C "$tempdir" -xzf "$infile" 2>/dev/null
        tar -C "$tempdir" -xf "$infile" 2>/dev/null
... # Add handling for other input formats here
        echo "Unrecognized input format" >&2
if [ $? -ne 0 ]; then
    echo "Error processing input file $infile" >&2 # or just echo "error"
    rm -rf "$tempdir"
    exit 1
(cd "$tempdir" && zip "$outfile" .)
rm -rf "$tempdir"

You'll need to determine what errors you get from tar, etc when an achive is "encrypted", and update the error messages appropriately to match what you're after. But this should give you a reasonable starting point.

share|improve this answer

Use tgz2zip. It's like retracile's script, but finished up.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.