17

My bash fu is not what it should be.

I want to create a little batch script which will copy a list of directories into a new zip file.

There are (at least) two ways I can think of providing the list of files:

  1. Read from a file (say config.txt). The file will contain the list of directories to zip up

OR

  1. Hard code the list directly into the bash script

The first option seems more straightforward (though less elegant).

The two problems I am facing are that I am not sure how to do the following:

  • Provide the list of directories to the shell script
  • Iterate over the list of directories

Could someone suggest in a few lines, how I can do this?

BTW, I am running on Ubuntu 10.0.4

Anil
  • 2,539
  • 6
  • 33
  • 42
morpheous
  • 16,270
  • 32
  • 89
  • 120

6 Answers6

37

You can create a gzipped tar on the commandline as follows:

tar czvf mytar.tar.gz dir1 dir2 .. dirN

If you wanted to do that in a bash script and pass the directories as arguments to the script, those arguments would end up in $@. So then you have:

tar czvf mytar.tar.gz "$@"

If that is in a script (lets say myscript.sh), you would call that as:

./myscript.sh dir1 dir2 .. dirN

If you want to read from a list (your option 1) you could do that like so (this does not work if there is whitespace in directory names):

tar czvf mytar.tar.gz $(<config.txt)
heijp06
  • 11,558
  • 1
  • 40
  • 60
  • @peter: thanks for your answer - which almost gets me where I want to be. But I am lazy, and dont want to type the same list of files each time at the command line (also this is error prone) - thats why I want to 'hard code' the directory list - either in the script itself, or separately maintained in an external file. Could you please explain how I can modify the commands above to do that? – morpheous Jul 27 '10 at 06:26
  • sweet!. That looks like what I want to do. I must say the syntax looks pretty fearsome though. I better get myself a bash programming book - FAST! :) – morpheous Jul 27 '10 at 06:29
  • @morpheus: apart from buying a book I would recommend reading `man bash` (in this case the section under *Command Substitution*). Also note that the solution with config.txt does not work for directories with whitespace in the name. – heijp06 Jul 27 '10 at 06:40
  • Er, when I tried to create a bash script to run the commands, I get the following error: "bash: ./work-backup.sh: /usr/bin/bash: bad interpreter: No such file or directory". My script starts with the line #!/usr/bin/bash ... any ideas whats going on? – morpheous Jul 27 '10 at 06:43
  • @morpheus: That means `bash` can not be found in the path `/usr/bin/bash`. You could type `which bash` at the command line to find out where `bash` is on your system. – heijp06 Jul 27 '10 at 06:48
  • @morpheus: Are you sure you did not make a typo on the first line? – heijp06 Jul 27 '10 at 06:50
  • @peter: bash is located at usr/bin/bash, and I am using this command in my shell script: tar czvf mytar.tar.gz $( – morpheous Jul 27 '10 at 06:59
  • Hmm, I finally got it to work by replacing #!/usr/bin/bash with #! /usr/bin/env bash - I have seen it done before, so I thought I'd try it - I ahve no freaking idea why I had to do that, but at least it works now – morpheous Jul 27 '10 at 07:05
0

In case, you are looking for compressing a directory, the following command can help.

pbzip2 compresses directories using parallel implementation

tar cf <outputfile_name> --use-compress-prog=pbzip2 <directory_name>
MANAUWER RAZA
  • 29
  • 1
  • 10
0

create two files: filelist - place all required directories ( one on single line )

and create a simple bash script:

    #!/bin/bash


for DIR in `cat filelist` 
do 
    if [ -d $DIR ] 
    then
        echo $DIR
    fi
done
Simpl
  • 49
  • 1
0

You can export a variable like DIRECTORIES="DIR1 DIR2 DIR3 ...." And in the script you need to use the variable like tar czvf $DIRECTORIES

Raghuram
  • 3,937
  • 2
  • 19
  • 25
0

Just use the null-byte as delimiter when you write file / directory names to file. This way you need not worry about spaces, newlines, etc. in file names!

printf "%s\000" */ > listOfDirs.txt    # alternative: find ... -print0 

while IFS="" read -r -d '' dir; do command ls -1abd "$dir"; done < listOfDirs.txt

tar --null -czvf mytar.tar.gz --files-from listOfDirs.txt 
jackIT
  • 1
0

Regularly I use:

tar -cv Folder1 Folder2 | gzip > Folders.tar.gz
Shrm
  • 426
  • 4
  • 8