1

How can I loop through directories and restrict the depth to only 3 levels? My version of find does not have -maxdepth. I'd like to keep it to one line if possible.

e.g: find /data -type d -print

Any help would be much appreciated. Thanks.

1 Answer 1

5
shopt -s nullglob
for d in /data/*/ /data/*/*/ /data/*/*/*/
do
  echo "$d"
done
Sign up to request clarification or add additional context in comments.

4 Comments

It doesn't do the -type d though; using [ -d "$d" ] && echo "$d" would, and you could drop setting nullglob then.
@user2719058: The terminating slash makes sure that it does, for the most part.
@IgnacioVazquez-Abrams: Ah, I stand corrected. Indeed, I didn't think about the slashes. What do you mean by "for the most part"? I can't think of cases where it doesn't work.
@user2719058: It will also include symlinks to directories.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.