LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   create directories based on the name of the archived files (https://www.linuxquestions.org/questions/linux-newbie-8/create-directories-based-on-the-name-of-the-archived-files-918183/)

kurokuon 12-11-2011 10:53 AM

create directories based on the name of the archived files
 
I want to extract multiple tar files, but it gets disorganized if I just extract them to my current folder and I don't want to create each folder individually. I know I can use file-roller on the GUI, but I want something for the CLI. Is there a way to create a directory based on name of the archived file? Thanks

theNbomr 12-11-2011 02:32 PM

So you want to create a directory name, based on the name of the tarball, and then unroll the tarball into that directory?
Code:

function tarzan {
    tarball=$1

    case $tarball in
        *.tar.gz)
            echo "tar.gz"
            directory=${tarball%.tar.gz}
            targs="zxvf"
            ;;
        *.tar.bz)
            echo "tar.bz"
            directory=${tarball%.tar.gz}
            targs="jxvf"
            ;;
        *.tar)
            echo "tar"
            directory=${tarball%.tar}
            targs="zxvf"
            ;;
        *.tgz)
            echo "tgz"
            directory=${tarball%.tgz}
            targs="zxvf"
            ;;

        *)
            echo "Not a tarball"
            exit 1
            ;;
    esac

    directory=${directory##*/}
    echo $directory

    mkdir $directory
    echo tar -C $directory -$targs $tarball
    # tar -C $directory -$targs $tarball
}

If you source this (perhaps in your ~/.bashrc), you will have a function 'tarzan' which use use the base name of the specified tarball as the directory name to create, and then unpack the tarball in that directory.
Code:

. theTarzanScript
tarzan /some/filespec/that/is/a/tarball.tar.bz

You may have to add a clause or two to match other tarball naming conventions.

--- rod.


All times are GMT -5. The time now is 11:31 AM.