13

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

2 Answers 2

21

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 {}" \;
7

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

for dir in !(dirname)/
...
0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.