Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

splitpea

macrumors 65816
Original poster
Oct 21, 2009
1,156
424
Among the starlings
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?
 
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).
 
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!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.