0

I am trying to loop through directories within a given folder and I want to skip a particular directory name. I have written this bash script to do it, but this is giving me errors. Please tell me where I am going wrong:

for f in *
do
    if [ -d "$f" ]; then
        if [ -d "TEST"];then
        echo "skip TEST directory"
        continue
        fi
    echo "$f"
    fi
done    

I want to skip TEST directory.

user3138373
  • 2,559

1 Answers1

1

Your if statement is incorrect. Try changing your 2nd if statement to the following.

for f in *
do
    if [ -d "$f" ]; then # Modify to [[ ! -L "$f" && -d "$f" ]] to check only for directories and not symlinks since -d will also get symlinks
        if [ "$f" = "TEST" ]; then
            echo "Skipping $f dir"
            continue
        fi
        # Code ...
    fi
done
  • 1
    Note = as the standard equivalent to == and ! as the standard equivalent to -not. See also Why is looping over find's output bad practice?. That -regex is functionally equivalent to -regex '.*/TEST.*' or its -path '*/TEST* standard equivalent. Maybe you meant find . -name TEST -prune -o type d -print. Also remember echo can't be used for arbitrary data. – Stéphane Chazelas Feb 24 '23 at 20:40
  • Note that [ -d "$f" ] also returns true on symlinks to directories while find's -type d excludes them. – Stéphane Chazelas Feb 24 '23 at 20:43
  • @StéphaneChazelas Yes I was looking for something like prune lol. I had no clue it was bad to loop with find, I always viewed it as being more powerful with more options specifically with -exec I will modify it to use exec for commands. I was just trying to provide alternatives if they were just doing something simple that a single find command could do. I will add checks for symlinks too. – Killian Fortman Feb 24 '23 at 20:49