LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-11-2011, 11:53 AM   #1
kurokuon
LQ Newbie
 
Registered: Dec 2011
Posts: 1

Rep: Reputation: Disabled
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
 
Old 12-11-2011, 03:32 PM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Sort files in directories based on the files date... CharlieMike73 Programming 5 09-09-2009 11:04 PM
Trying to create directories based on file names JackieBrown Linux - General 3 05-15-2009 10:13 PM
Samba: "homes" share, cannot create directories, can create files Herg Linux - Software 1 09-14-2006 09:48 AM
user permissions to create files and directories ringding Linux - Security 3 09-07-2006 05:34 PM
Create an archive file which includes files in sub-directories? amaze Linux - General 1 09-26-2003 09:43 AM


All times are GMT -5. The time now is 01:28 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration