-   Programming (
-   -   excluding directory from backup in bash... (

astanton 06-30-2011 05:23 PM

excluding directory from backup in bash...
I've got the following code and kludge.

It's working okay except that there's a directory called "Archive" which I don't need to backup.

I've tried various combinations of &&, -a, etc., and I'm kind of at a loss for getting this right.

Here's the code I have so far:



# backup subdirs as individual tarballs with prepended timestamps and
# move them somewhere else.
# --exclude "/path/to/dir/*" (Doesn't seem to work)

#      For now a kludge that rm's the 'Archive' dir after the tarring.

# set VARS

DSTAMP=$(date +%Y%m%d)

#      Make the archive point if it doesn't already exist.

mkdir -p $BUDIRS

#      Loop through the dirs and prepend the stamps to the tarballs.

for file in *; do
 if [[ -d "${file}" ]]; then
  tar cpzvf "$BUDIRS/${DSTAMP}-${file}".tar.gz "${file}"
rm -rf "$BUDIRS/$DSTAMP-Archive.tar.gz"      # Need to fix this kludge.

The problem is with this line:


if [[ -d "${file}" ]]
The script works great until I try to "Exclude" the directory "Archive" from being tarred up.

I've tried:


if [[ -d "${file}" && !"Archive" ]]
And other variations of using an and, it just bombs.

How can I write that line so that I can loop through all of the subdirs, tarring them up, but excluding the "Archive" subdirectory?

macemoneta 06-30-2011 05:46 PM

Your syntax is incorrect for a compound if:


if [ -d "${file}" ] && [ "$file" != "Archive" ]

David the H. 06-30-2011 05:52 PM

Please be more specific about how it "bombs". Do you get any error messages? Or any output at all?

Change your shebang to #!/bin/bash. /bin/sh restricts the script to posix-compatible mode, and many bash-only features are disabled.

If it still doesn't work after that, you might try a different approach, and use extended globbing to exclude the directory.


shopt -s extglob

for file in ./!(Archive); do

More on globbing here:

Edit @macemoneta. His syntax is just fine when using bash's new [[ test keyword. It's only the old test command that needs to be broken up like that.

Ramurd 06-30-2011 06:06 PM

You can also give tar the --exclude option; might be a bit easier?

astanton 06-30-2011 06:11 PM


Originally Posted by macemoneta (Post 4400774)
Your syntax is incorrect for a compound if:


if [ -d "${file}" ] && [ "$file" != "Archive" ]

I tried that as well as:


if [ -d "${file}" ] && [ "${file}" != "Archive" ]
(With the braces around 'file'), and both ways I'm now getting the following error:


./ line 28: syntax error near unexpected token `fi'
./ line 28: `  fi'

But you're right that it looks like it should work now.

astanton 06-30-2011 06:21 PM

Oh the horror...

That was a der move on my part. nevermind and thank you so much for your help!

The problem, the second time around was a lacking:


; then
on the end of that line when I did the cut/paste.

I'll mark this as solved. Again, thanks so much :)

astanton 06-30-2011 06:56 PM


Originally Posted by Ramurd (Post 4400797)
You can also give tar the --exclude option; might be a bit easier?

That was how I originally tried it, but it didn't pan out for me, and I assumed it was because the tar command was looping within an if statement.

Anyway, everything is working great now.

Thanks :)

All times are GMT -5. The time now is 04:27 AM.