I love 7-zip. I use it for all of my file compression needs, exclusively. Unfortunately, in Linux, it’s a bit of a PITA. Graphical utilities that support it are unreliable, and the command line utility is needlessly complicated unless you use the default options, which are suboptimal. So here’s a handy little script I wrote to simplify its usage in my daily life.

Basically, all you have to do is type “7zip target” and the script will apply maximum LZMA2 compression to the file or directory as “./target.7z“. You can also use simple wildcards like “7zip *“, or specify multiple files like “7zip file_1 file_2 file_3…“, and it will create one archive per argument. If you specify an output directory via -o, it will save the archives there. Spaces are supported if you use quotation marks.

Prerequisite softwares are 7z and Bash. Paste this code into a text file named “7zip” in /usr/local/bin (or someplace else that you can get to it), then chmod it executable (755).

 

#!/bin/bash

# Original script by Walter Heitman Jr, first published on http://techblog.shanock.com

function help {
        echo "Syntax: $0 -[x][s][o output_directory] target(s)"
        echo " Parameters come first. Wildcards, spaces (use quotes) allowed in target."
        echo " -x to extract."
        echo " -o to place results in specified directory."
        echo " -s to extract each file to a directory of its own name."
        echo " Compression: Target name is used as archive name, suffixed with .7z."
        echo " Compression: One archive will be created per target."
        exit 1
}

# If no arguments are passed, call the "help" function.
if [ -z "$1" ]; then
        help
fi

# Define some variables
OUTPUTPATH="./"
OUTPUTDIR=
EXTRACT=0
SELFDIR=0

while getopts "hxso:" OPTION
do
        case $OPTION in
                h)
                        help
                        ;;
                x)
                        EXTRACT=1
                        shift $(($OPTIND - 1))
                        ;;
                s)
                        SELFDIR=1
                        shift $(($OPTIND - 1))
                        ;;
                o)
                        # Get and format absolute directory name with readlink
                        OUTPUTPATH=$(readlink -f "$OPTARG")/
                        shift $(($OPTIND - 1))
                        ;;
                \?)
                        help
                        ;;
        esac
done

# Create output directory if it doesn't exist
if [ ! -d "$OUTPUTPATH" ]; then mkdir "$OUTPUTPATH"; fi

if [ $EXTRACT == "1" ]; then
        # Extraction work loop
        for ZTARGET in "$@"
        do
                if [ $SELFDIR == "1" ]; then OUTPUTDIR="$(basename "${ZTARGET%.*}")"; fi
                /usr/bin/7z x -o"$OUTPUTPATH$OUTPUTDIR" "$ZTARGET"
        done
else
        # Compression work loop
        for ZTARGET in "$@"
        do
                /usr/bin/7z a -m0=lzma2 -mx=9 "$OUTPUTPATH$(basename "$ZTARGET").7z" "$ZTARGET"
        done
fi