Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

In the following script, How do I exclude a directory in the loop? I want to do this because all files are symlinks pointing to this directory that exists in the same level, so I don't want to add it.


cd /var/www
for dir in */
do
        base=$(basename "$dir")
        tar -czfh "${base}.tar.gz" "$dir"
done

share|improve this question

2 Answers 2

up vote 3 down vote accepted

You can add a continue statement like this :

cd /var/www
for dir in */
do
        if [ "$dir" == "foo" ] ; then
              continue;
        fi
        base=$(basename "$dir")
        tar -czfh "${base}.tar.gz" "$dir"
done

Or you can do it with find command :

find /var/www -maxdepth 1 -type d \( -name foo \) -prune -o -print -exec bash -c "tar -czfh \`basename {}\`.tar.gz {}" \;
share|improve this answer

You need to enable extglob for this if it's not already enabled (shopt -s extglob):

for dir in !(dirname)/
...
share|improve this answer
    
I didn't know this one, thanks –  Coren Feb 25 '12 at 21:52

Your Answer

 
discard

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.