Wednesday, July 9, 2025

How to list all subdirectories and exclude certain subdirectories in a tree-like fashion

In this example I want to see all subdirectories that starts with "dir", but also to exclude the directory "dirdat":
cd $ORACLE_GG
tree -L 2 --noreport $(ls -d dir* | grep -v '^dirdat$')
  • ls -d dir*: lists all dir* entries (only directories if they match)
  • grep -v '^dirdat$': removes dirdat from the list
  • $(...): expands the filtered list as arguments to tree