Results 1 to 6 of 6

Thread: Simultaneous Commands

  1. #1
    Join Date
    Jun 2007
    Location
    Baile Átha Cliath, Éire
    Beans
    213
    Distro
    Ubuntu 12.04 Precise Pangolin

    Simultaneous Commands

    Hi y'all. This is probably the wrong forum, but here goes anyway! I'm in the middle of writing a backup script for my documents, and I want to archive four folders at once. Of course I could just do:
    Code:
    tar -czvf Folder1.tar.gz Folder1
    tar -czvf Folder2.tar.gz Folder2
    tar -czvf Folder3.tar.gz Folder3
    tar -czvf Folder4.tar.gz Folder4
    but that's boring and will only archive each one in sequence. Is there any way to get all four to start 'tarring' simultaneously and then make the shell wait until all four archives are finished before taking a new command?
    Code:
    tar -czvf Folder1.tar.gz Folder1 &
    tar -czvf Folder2.tar.gz Folder2 &
    tar -czvf Folder3.tar.gz Folder3 &
    tar -czvf Folder4.tar.gz Folder4
    …won't work if any of the first three folders take longer to archive than the last one does.
    Please check any commands that you have been told to run; they may be dangerous!
    Stuck? – Your problem may have already been solved! Try the search utility in the top right corner… | The 'one post' thread.
    안아주세요!

  2. #2
    Join Date
    Jun 2007
    Location
    Baile Átha Cliath, Éire
    Beans
    213
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Simultaneous Commands

    For those of you who might be wondering, I think I've figured it out:
    Code:
    tar -czvf Folder1.tar.gz Folder1 &
    ARCHIVE1=$!
    tar -czvf Folder2.tar.gz Folder2 &
    ARCHIVE2=$!
    tar -czvf Folder3.tar.gz Folder3 &
    ARCHIVE3=$!
    tar -czvf Folder4.tar.gz Folder4 &
    ARCHIVE4=$!
    wait $ARCHIVE1 $ARCHIVE2 $ARCHIVE3 $ARCHIVE4
    Please check any commands that you have been told to run; they may be dangerous!
    Stuck? – Your problem may have already been solved! Try the search utility in the top right corner… | The 'one post' thread.
    안아주세요!

  3. #3
    Join Date
    Sep 2006
    Beans
    Hidden!
    Distro
    Ubuntu 6.10 Edgy

    Re: Simultaneous Commands

    Wouldn't

    tar -czvf Folder1.tar.gz Folder1 && tar -czvf Folder2.tar.gz Folder2 && tar -czvf Folder3.tar.gz Folder3 && tar -czvf Folder4.tar.gz Folder4

    work?

  4. #4
    Join Date
    Nov 2005
    Location
    Scandinavia
    Beans
    939

    Re: Simultaneous Commands

    Since i think bash is fun (and confusing at times...), i'll drop in an alternative for you to experiment with

    Code:
    archives="folder1 folder2 folder3 folder4"
    for name in $archives
    do
    tar -czvf "$name.tar.gz" "$name" &
    pid="$pid $!"
    done
    
    wait $pid
    This way you will only have to adjust your variable content. You could of course automate it even further if you wanted to.
    Last edited by pelle.k; August 23rd, 2007 at 10:39 PM.
    - "though It seems that I know that I know, what I would like to see Is the I that sees me, when I know that I know that I know" / Alan Watts

  5. #5
    Join Date
    Sep 2006
    Beans
    Hidden!
    Distro
    Ubuntu 6.10 Edgy

    Re: Simultaneous Commands

    Quote Originally Posted by pelle.k View Post
    Since i think bash is fun (and confusing at times...), i'll drop in an alternative for you to experiment with

    Code:
    archives="folder1 folder2 folder3 folder4"
    for name in $archives
    do
    tar -czvf "$name.tar.gz" "$name" &
    pid="$pid $!"
    done
    
    wait $pid
    This way you will only have to adjust your variable content. You could of course automate it even further if you wanted to.
    What? No indent?

    If the names change, wouldn't it be better to put a "read" in the beggining of the file, so that it could (I think) be piped with a command like "find -mtime 1" (finds every file modified in the last 24 hours) with a command like "find -mtime 1 | backup.sh"?

  6. #6
    Join Date
    Jun 2007
    Location
    Baile Átha Cliath, Éire
    Beans
    213
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Simultaneous Commands

    Quote Originally Posted by rolando2424 View Post
    Wouldn't

    tar -czvf Folder1.tar.gz Folder1 && tar -czvf Folder2.tar.gz Folder2 && tar -czvf Folder3.tar.gz Folder3 && tar -czvf Folder4.tar.gz Folder4

    work?
    That's what I thought originally, but it seemed to do weird things (like executing any shell scripts stored in the folders). Couldn't figure that out.
    Quote Originally Posted by pelle.k View Post
    Since i think bash is fun (and confusing at times...), i'll drop in an alternative for you to experiment with

    Code:
    archives="folder1 folder2 folder3 folder4"
    for name in $archives
    do
    tar -czvf "$name.tar.gz" "$name" &
    pid="$pid $!"
    done
    
    wait $pid
    This way you will only have to adjust your variable content. You could of course automate it even further if you wanted to.
    Wouldn't that still just archive each folder one after another (with each iteration of the 'for loop'?

    No, my mistake - not using my eyes as usual. That would make more sense. Looks cooler too!
    Please check any commands that you have been told to run; they may be dangerous!
    Stuck? – Your problem may have already been solved! Try the search utility in the top right corner… | The 'one post' thread.
    안아주세요!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •