10

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

13

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)/
...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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