Home » Development, LAMP, Open Source, Productivity, Ubuntu

Extract and Compress Files with ease from Command Line in Linux

28 July 2012 7,543 views One Comment Permanent Link

Function to Extract a Compressed File.

The Function auto detects the Compression method and runs the appropriate Command to Extract the File.

function extract()
{
     if [ -f $1 ] ; then
         case $1 in
             *.tar.bz2)   tar xjf $1        ;;
             *.tar.gz)    tar xzf $1     ;;
             *.bz2)       bunzip2 $1       ;;
             *.rar)       rar x $1     ;;
             *.gz)        gunzip $1     ;;
             *.tar)       tar xf $1        ;;
             *.taz)       tar -xvf $1        ;;
             *.tbz2)      tar xjf $1      ;;
             *.tgz)       tar xzf $1       ;;
             *.zip)       unzip $1     ;;
             *.Z)         uncompress $1  ;;
             *.7z)        7z x $1    ;;
             *)           echo "'$1' cannot be extracted via extract()" ;;
         esac
     else
         echo "'$1' is not a valid file"
     fi
}

To Extract a File just enter the command

extract file-path

Source: http://ubuntuforums.org/showthread.php?t=679762

Function to Compress File/Folder

This Script compresses the File Folder Passed as the first argument and the method extension passed as the second argument. If no Compression method is passed it defaults to tar.bz2 You can edit the code below to overide this behaviour.

function compress()
{
    dirPriorToExe=`pwd`
    dirName=`dirname $1`
    baseName=`basename $1`
 
    if [ -f $1 ] ; then
        echo "It was a file change directory to $dirName"
        cd $dirName
        case $2 in
          tar.bz2)
                    tar cjf $baseName.tar.bz2 $baseName
                    ;;
          tar.gz)
                    tar czf $baseName.tar.gz $baseName
                    ;;
          gz)
                    gzip $baseName
                    ;;
          tar)
                    tar -cvvf $baseName.tar $baseName
                    ;;
          zip)
                    zip -r $baseName.zip $baseName
                    ;;
            *)
                    echo "Method not passed compressing using tar.bz2"
                    tar cjf $baseName.tar.bz2 $baseName
                    ;;
        esac
        echo "Back to Directory $dirPriorToExe"
        cd $dirPriorToExe
    else
        if [ -d $1 ] ; then
            echo "It was a Directory change directory to $dirName"
            cd $dirName
            case $2 in
                tar.bz2)
                        tar cjf $baseName.tar.bz2 $baseName
                        ;;
                tar.gz)
                        tar czf $baseName.tar.gz $baseName
                        ;;
                gz)
                        gzip -r $baseName
                        ;;
                tar)
                        tar -cvvf $baseName.tar $baseName
                        ;;
                zip)
                        zip -r $baseName.zip $baseName
                        ;;
                *)
                    echo "Method not passed compressing using tar.bz2"
                tar cjf $baseName.tar.bz2 $baseName
                        ;;
            esac
            echo "Back to Directory $dirPriorToExe"
            cd $dirPriorToExe
        else
            echo "'$1' is not a valid file/folder"
        fi
    fi
    echo "Done"
    echo "###########################################"
}

To Compress a File just enter the command

compress file-path

Copy paste these Functions to your ~/.bashrc file or another file which is included through ~/.bashrc to make these functions available at all times.

pixel Extract and Compress Files with ease from Command Line in Linux

PR: 0

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...
  • http://www.facebook.com/asiru Asiru Nasir

    Thanks for this tutorial friend….. worth for me……