extract and delete files from one tar and add to another new one












0















I have a bzip2ed tar file and a text file with a list of files. I want to extract the files listed in the text file from the tar, add them to a new tar, and then delete them from the first tar.



For example, if I have a tar file like this:



$ tar -tvf test.tar.bz2
drwxrwxrwx nacho/nacho 0 2018-11-16 23:30 one/test/
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/a
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/b
drwxrwxrwx nacho/nacho 0 2018-11-16 23:25 one/test/c/
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/a
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/b
drwxrwxrwx nacho/nacho 0 2018-11-16 23:25 one/test/c/c/
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/c/a
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/c/b
drwxrwxrwx nacho/nacho 0 2018-11-16 23:25 one/test/c/d/
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/d/a
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/d/b
drwxrwxrwx nacho/nacho 0 2018-11-16 23:34 one/test/e/


And a text file with a list of files like this:



$ cat files_to_extract
one/test/b
one/test/e/
one/test/c/b
one/test/c/d/a


After it is done, this is what the original tar file should look like:



$ tar -tvf test.tar.bz2
drwxrwxrwx nacho/nacho 0 2018-11-16 23:30 one/test/
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/a
drwxrwxrwx nacho/nacho 0 2018-11-16 23:25 one/test/c/
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/a
drwxrwxrwx nacho/nacho 0 2018-11-16 23:25 one/test/c/c/
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/c/a
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/c/b
drwxrwxrwx nacho/nacho 0 2018-11-16 23:25 one/test/c/d/
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/d/b


And what the new tar file should look like.



$ tar -tvf new.tar.bz2
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/b
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/b
drwxrwxrwx nacho/nacho 0 2018-11-16 23:34 one/test/e/
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/d/a


Note, the order of the files is irrelevant.










share|improve this question

























  • What have you tried to do? This seems like something a basic python (or maybe even bash) script could do

    – onVal
    Nov 17 '18 at 11:57











  • Your question is better suited to Super User. This page is intended for programming questions.

    – Cyrus
    Dec 23 '18 at 11:02











  • tried that and they said to come here

    – IMTheNachoMan
    Dec 23 '18 at 17:58











  • Okay, that's weird.

    – Cyrus
    Dec 24 '18 at 11:01
















0















I have a bzip2ed tar file and a text file with a list of files. I want to extract the files listed in the text file from the tar, add them to a new tar, and then delete them from the first tar.



For example, if I have a tar file like this:



$ tar -tvf test.tar.bz2
drwxrwxrwx nacho/nacho 0 2018-11-16 23:30 one/test/
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/a
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/b
drwxrwxrwx nacho/nacho 0 2018-11-16 23:25 one/test/c/
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/a
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/b
drwxrwxrwx nacho/nacho 0 2018-11-16 23:25 one/test/c/c/
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/c/a
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/c/b
drwxrwxrwx nacho/nacho 0 2018-11-16 23:25 one/test/c/d/
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/d/a
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/d/b
drwxrwxrwx nacho/nacho 0 2018-11-16 23:34 one/test/e/


And a text file with a list of files like this:



$ cat files_to_extract
one/test/b
one/test/e/
one/test/c/b
one/test/c/d/a


After it is done, this is what the original tar file should look like:



$ tar -tvf test.tar.bz2
drwxrwxrwx nacho/nacho 0 2018-11-16 23:30 one/test/
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/a
drwxrwxrwx nacho/nacho 0 2018-11-16 23:25 one/test/c/
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/a
drwxrwxrwx nacho/nacho 0 2018-11-16 23:25 one/test/c/c/
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/c/a
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/c/b
drwxrwxrwx nacho/nacho 0 2018-11-16 23:25 one/test/c/d/
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/d/b


And what the new tar file should look like.



$ tar -tvf new.tar.bz2
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/b
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/b
drwxrwxrwx nacho/nacho 0 2018-11-16 23:34 one/test/e/
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/d/a


Note, the order of the files is irrelevant.










share|improve this question

























  • What have you tried to do? This seems like something a basic python (or maybe even bash) script could do

    – onVal
    Nov 17 '18 at 11:57











  • Your question is better suited to Super User. This page is intended for programming questions.

    – Cyrus
    Dec 23 '18 at 11:02











  • tried that and they said to come here

    – IMTheNachoMan
    Dec 23 '18 at 17:58











  • Okay, that's weird.

    – Cyrus
    Dec 24 '18 at 11:01














0












0








0


1






I have a bzip2ed tar file and a text file with a list of files. I want to extract the files listed in the text file from the tar, add them to a new tar, and then delete them from the first tar.



For example, if I have a tar file like this:



$ tar -tvf test.tar.bz2
drwxrwxrwx nacho/nacho 0 2018-11-16 23:30 one/test/
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/a
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/b
drwxrwxrwx nacho/nacho 0 2018-11-16 23:25 one/test/c/
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/a
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/b
drwxrwxrwx nacho/nacho 0 2018-11-16 23:25 one/test/c/c/
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/c/a
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/c/b
drwxrwxrwx nacho/nacho 0 2018-11-16 23:25 one/test/c/d/
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/d/a
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/d/b
drwxrwxrwx nacho/nacho 0 2018-11-16 23:34 one/test/e/


And a text file with a list of files like this:



$ cat files_to_extract
one/test/b
one/test/e/
one/test/c/b
one/test/c/d/a


After it is done, this is what the original tar file should look like:



$ tar -tvf test.tar.bz2
drwxrwxrwx nacho/nacho 0 2018-11-16 23:30 one/test/
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/a
drwxrwxrwx nacho/nacho 0 2018-11-16 23:25 one/test/c/
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/a
drwxrwxrwx nacho/nacho 0 2018-11-16 23:25 one/test/c/c/
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/c/a
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/c/b
drwxrwxrwx nacho/nacho 0 2018-11-16 23:25 one/test/c/d/
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/d/b


And what the new tar file should look like.



$ tar -tvf new.tar.bz2
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/b
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/b
drwxrwxrwx nacho/nacho 0 2018-11-16 23:34 one/test/e/
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/d/a


Note, the order of the files is irrelevant.










share|improve this question
















I have a bzip2ed tar file and a text file with a list of files. I want to extract the files listed in the text file from the tar, add them to a new tar, and then delete them from the first tar.



For example, if I have a tar file like this:



$ tar -tvf test.tar.bz2
drwxrwxrwx nacho/nacho 0 2018-11-16 23:30 one/test/
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/a
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/b
drwxrwxrwx nacho/nacho 0 2018-11-16 23:25 one/test/c/
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/a
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/b
drwxrwxrwx nacho/nacho 0 2018-11-16 23:25 one/test/c/c/
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/c/a
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/c/b
drwxrwxrwx nacho/nacho 0 2018-11-16 23:25 one/test/c/d/
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/d/a
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/d/b
drwxrwxrwx nacho/nacho 0 2018-11-16 23:34 one/test/e/


And a text file with a list of files like this:



$ cat files_to_extract
one/test/b
one/test/e/
one/test/c/b
one/test/c/d/a


After it is done, this is what the original tar file should look like:



$ tar -tvf test.tar.bz2
drwxrwxrwx nacho/nacho 0 2018-11-16 23:30 one/test/
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/a
drwxrwxrwx nacho/nacho 0 2018-11-16 23:25 one/test/c/
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/a
drwxrwxrwx nacho/nacho 0 2018-11-16 23:25 one/test/c/c/
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/c/a
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/c/b
drwxrwxrwx nacho/nacho 0 2018-11-16 23:25 one/test/c/d/
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/d/b


And what the new tar file should look like.



$ tar -tvf new.tar.bz2
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/b
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/b
drwxrwxrwx nacho/nacho 0 2018-11-16 23:34 one/test/e/
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/d/a


Note, the order of the files is irrelevant.







linux tar bzip2






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 17 '18 at 8:24









Cyrus

45.5k43677




45.5k43677










asked Nov 17 '18 at 4:48









IMTheNachoManIMTheNachoMan

1,154926




1,154926













  • What have you tried to do? This seems like something a basic python (or maybe even bash) script could do

    – onVal
    Nov 17 '18 at 11:57











  • Your question is better suited to Super User. This page is intended for programming questions.

    – Cyrus
    Dec 23 '18 at 11:02











  • tried that and they said to come here

    – IMTheNachoMan
    Dec 23 '18 at 17:58











  • Okay, that's weird.

    – Cyrus
    Dec 24 '18 at 11:01



















  • What have you tried to do? This seems like something a basic python (or maybe even bash) script could do

    – onVal
    Nov 17 '18 at 11:57











  • Your question is better suited to Super User. This page is intended for programming questions.

    – Cyrus
    Dec 23 '18 at 11:02











  • tried that and they said to come here

    – IMTheNachoMan
    Dec 23 '18 at 17:58











  • Okay, that's weird.

    – Cyrus
    Dec 24 '18 at 11:01

















What have you tried to do? This seems like something a basic python (or maybe even bash) script could do

– onVal
Nov 17 '18 at 11:57





What have you tried to do? This seems like something a basic python (or maybe even bash) script could do

– onVal
Nov 17 '18 at 11:57













Your question is better suited to Super User. This page is intended for programming questions.

– Cyrus
Dec 23 '18 at 11:02





Your question is better suited to Super User. This page is intended for programming questions.

– Cyrus
Dec 23 '18 at 11:02













tried that and they said to come here

– IMTheNachoMan
Dec 23 '18 at 17:58





tried that and they said to come here

– IMTheNachoMan
Dec 23 '18 at 17:58













Okay, that's weird.

– Cyrus
Dec 24 '18 at 11:01





Okay, that's weird.

– Cyrus
Dec 24 '18 at 11:01












1 Answer
1






active

oldest

votes


















0














With GNU tar:



# unpack test.tar.bz2 to test.tar
bunzip2 test.tar.bz2

# create a working directory
mkdir tempdir

# extract desired files to tempdir
tar -C tempdir -xvf test.tar --files-from files_to_extract

# create new tar.bz2 with desired files in tempdir
tar -C tempdir -cvjf new.tar.bz2 one

# check content of new.tar.bz2
tar -tjf new.tar.bz2


Output of last command:




one/
one/test/
one/test/c/
one/test/c/b
one/test/c/d/
one/test/c/d/a
one/test/e/
one/test/b


# remove directory tempdir in current directory
rm -rf tempdir

# remove files in test.tar listed in files_to_extract
tar --delete -vf test.tar --files-from files_to_extract

# pack test.tar to test.tar.bz2
bzip2 test.tar

# check content of test.tar.bz2
tar -tjf test.tar.bz2


Output of last command:




one/
one/test/
one/test/c/
one/test/c/c/
one/test/c/c/a
one/test/c/c/b
one/test/c/a
one/test/c/d/
one/test/c/d/b
one/test/a





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53348319%2fextract-and-delete-files-from-one-tar-and-add-to-another-new-one%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    With GNU tar:



    # unpack test.tar.bz2 to test.tar
    bunzip2 test.tar.bz2

    # create a working directory
    mkdir tempdir

    # extract desired files to tempdir
    tar -C tempdir -xvf test.tar --files-from files_to_extract

    # create new tar.bz2 with desired files in tempdir
    tar -C tempdir -cvjf new.tar.bz2 one

    # check content of new.tar.bz2
    tar -tjf new.tar.bz2


    Output of last command:




    one/
    one/test/
    one/test/c/
    one/test/c/b
    one/test/c/d/
    one/test/c/d/a
    one/test/e/
    one/test/b


    # remove directory tempdir in current directory
    rm -rf tempdir

    # remove files in test.tar listed in files_to_extract
    tar --delete -vf test.tar --files-from files_to_extract

    # pack test.tar to test.tar.bz2
    bzip2 test.tar

    # check content of test.tar.bz2
    tar -tjf test.tar.bz2


    Output of last command:




    one/
    one/test/
    one/test/c/
    one/test/c/c/
    one/test/c/c/a
    one/test/c/c/b
    one/test/c/a
    one/test/c/d/
    one/test/c/d/b
    one/test/a





    share|improve this answer






























      0














      With GNU tar:



      # unpack test.tar.bz2 to test.tar
      bunzip2 test.tar.bz2

      # create a working directory
      mkdir tempdir

      # extract desired files to tempdir
      tar -C tempdir -xvf test.tar --files-from files_to_extract

      # create new tar.bz2 with desired files in tempdir
      tar -C tempdir -cvjf new.tar.bz2 one

      # check content of new.tar.bz2
      tar -tjf new.tar.bz2


      Output of last command:




      one/
      one/test/
      one/test/c/
      one/test/c/b
      one/test/c/d/
      one/test/c/d/a
      one/test/e/
      one/test/b


      # remove directory tempdir in current directory
      rm -rf tempdir

      # remove files in test.tar listed in files_to_extract
      tar --delete -vf test.tar --files-from files_to_extract

      # pack test.tar to test.tar.bz2
      bzip2 test.tar

      # check content of test.tar.bz2
      tar -tjf test.tar.bz2


      Output of last command:




      one/
      one/test/
      one/test/c/
      one/test/c/c/
      one/test/c/c/a
      one/test/c/c/b
      one/test/c/a
      one/test/c/d/
      one/test/c/d/b
      one/test/a





      share|improve this answer




























        0












        0








        0







        With GNU tar:



        # unpack test.tar.bz2 to test.tar
        bunzip2 test.tar.bz2

        # create a working directory
        mkdir tempdir

        # extract desired files to tempdir
        tar -C tempdir -xvf test.tar --files-from files_to_extract

        # create new tar.bz2 with desired files in tempdir
        tar -C tempdir -cvjf new.tar.bz2 one

        # check content of new.tar.bz2
        tar -tjf new.tar.bz2


        Output of last command:




        one/
        one/test/
        one/test/c/
        one/test/c/b
        one/test/c/d/
        one/test/c/d/a
        one/test/e/
        one/test/b


        # remove directory tempdir in current directory
        rm -rf tempdir

        # remove files in test.tar listed in files_to_extract
        tar --delete -vf test.tar --files-from files_to_extract

        # pack test.tar to test.tar.bz2
        bzip2 test.tar

        # check content of test.tar.bz2
        tar -tjf test.tar.bz2


        Output of last command:




        one/
        one/test/
        one/test/c/
        one/test/c/c/
        one/test/c/c/a
        one/test/c/c/b
        one/test/c/a
        one/test/c/d/
        one/test/c/d/b
        one/test/a





        share|improve this answer















        With GNU tar:



        # unpack test.tar.bz2 to test.tar
        bunzip2 test.tar.bz2

        # create a working directory
        mkdir tempdir

        # extract desired files to tempdir
        tar -C tempdir -xvf test.tar --files-from files_to_extract

        # create new tar.bz2 with desired files in tempdir
        tar -C tempdir -cvjf new.tar.bz2 one

        # check content of new.tar.bz2
        tar -tjf new.tar.bz2


        Output of last command:




        one/
        one/test/
        one/test/c/
        one/test/c/b
        one/test/c/d/
        one/test/c/d/a
        one/test/e/
        one/test/b


        # remove directory tempdir in current directory
        rm -rf tempdir

        # remove files in test.tar listed in files_to_extract
        tar --delete -vf test.tar --files-from files_to_extract

        # pack test.tar to test.tar.bz2
        bzip2 test.tar

        # check content of test.tar.bz2
        tar -tjf test.tar.bz2


        Output of last command:




        one/
        one/test/
        one/test/c/
        one/test/c/c/
        one/test/c/c/a
        one/test/c/c/b
        one/test/c/a
        one/test/c/d/
        one/test/c/d/b
        one/test/a






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 17 '18 at 11:12

























        answered Nov 17 '18 at 10:40









        CyrusCyrus

        45.5k43677




        45.5k43677






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53348319%2fextract-and-delete-files-from-one-tar-and-add-to-another-new-one%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Guess what letter conforming each word

            Port of Spain

            Run scheduled task as local user group (not BUILTIN)