Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am doing backup using

for a in /home/*
//do backup

I want to exclude some folders in /home . but for loop takes all folders i there.

is there any way so that i can specify exclude list at top which can be used in loop

share|improve this question
up vote 2 down vote accepted

You need some sort of evaluation in order to check what to exclude from the list of folders you get in the "for"; just using bash this could be (you can use whatever you feel comfortable to evaluate what to exclude (ie sed, grep, awk,...)):

exclusiondirs="/home/foo /home/bar" # list of folders to exclude from backup

for a in /home/*; do
if ! [[ "$exclusiondirs" =~ $a ]]; then backup...

However I would have use some other tools for backup such rsync which can exclude by itself, for example:

rsync -av /home/* --exclude /home/foo --exclude /home/var destination


Since the bash regex is a bit tricky and difficult to implement in some circumstances I've replaced it with a more robust grep statement:

if ! echo "$exclusiondirs" | grep -q "\b$a\b" ; then

This grep statement will work in a home dir with names such as (where some dirs share common part names and include spaces within):

mkdir -p home/{foo,bar,foobar,a,b,c,a\ b
`-- home
    |-- a
    |-- a b
    |-- b
    |-- bar
    |-- c
    |-- foo
    `-- foobar

~$ echo $exclusiondirs 
/home/foo /home/bar /home/a
~$ for a in /home/*; do if ! echo "$exclusiondirs" | grep -q "\b$a\b" ; then echo $a; fi; done
/home/a b
share|improve this answer
thanks for that , i can use rsync but can i want separate users home dir backups in separate folders in compressed format like .tar.gz can i do that in rsync – Mirror51 May 4 '11 at 9:34

Go to Skipping multiple files and folders section in this page.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.