Bash script to zip multiple directories?

Discussion in 'Mac Programming' started by splitpea, Apr 4, 2010.

  1. splitpea macrumors 6502a

    Joined:
    Oct 21, 2009
    Location:
    Among the starlings
    #1
    I need to put together a bash script that will read in a text file containing a list of directories (one per line) and for each line output a separate archive (either zip archives or tarballs would be fine) of each directory, with the directory name (plus extension) as the filename.

    So far I have the following, but need a way to read in the list of directories:

    Code:
    for dirname in [LIST OF DIRECTORIES]
        do
            tar -czvf "backups/$dirname.tar.gz" "$dirname"
        done
    
    Any suggestions?
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Assuming the directories to zip are in a file called dirfile with one item per line then

    Code:
    cat dirfile | xargs -IR tar -czvf "backups/R.tar.gz" "R"
    
    should work (although I have not tested this).
     
  3. splitpea thread starter macrumors 6502a

    Joined:
    Oct 21, 2009
    Location:
    Among the starlings
    #3
    Yup, I just tested that on some dummy data and it worked perfectly. Thank you!

    Btw, I never really understood the xargs command before, and now it makes a lot more sense. Much appreciated!
     

Share This Page