In this short guide, we will learn how to list subdirectories at a specific depth level in Linux and Unix systems. This essential skill helps developers navigate complex project structures, analyze folder hierarchies, and manage large codebases efficiently.

Here you can find the short answer:

(1) find - List directories 1 level deep

find . -maxdepth 1 ! -path . -printf "%s %p\n" | sort -n -k1  

(2) List directories 2 levels deep

find . -maxdepth 2 -type d

(3) List directories exactly at level 2 (not shallower)

find . -mindepth 2 -maxdepth 2 -type d

(4) tree

tree Applications/ -s

(5) du

du --all --human-readable --apparent-size

(6) ls -LRlh

- `ls -LRlh ./*` - list all recursive
- `ls -Llh ./*` - list single level
- `ls -Llh ./*/*` - list 2 levels

So let's see how to control directory listing depth in the terminal.

Problem: Controlling Directory Depth

When working with nested folder structures like project repositories, you need to:

  • View specific levels of directory hierarchy
  • Avoid overwhelming output from deep recursion
  • Analyze project organization at precise depths
  • Generate folder structure reports for documentation

Solution: Use find command with -maxdepth and -mindepth options.

1: List Directories Up to N Levels Deep

Control maximum depth to limit how far find searches:

find /projects -maxdepth 2 -type d

Output Result:

/projects
/projects/website
/projects/website/frontend
/projects/website/backend
/projects/mobile-app
/projects/mobile-app/ios
/projects/mobile-app/android

How it works:

  • /projects - Starting directory (level 0)
  • -maxdepth 2 - Search up to 2 levels deep
  • -type d - Match directories only
  • Shows directories at levels 0, 1, and 2

Real-world example: Viewing top-level project structure in a company workspace without diving into deep module folders.

2: List Directories Exactly at Level N

Use both -mindepth and -maxdepth to get directories at a specific level only:

find /home/user/Documents -mindepth 2 -maxdepth 2 -type d

Output Result:

/home/user/Documents/Work/Clients
/home/user/Documents/Work/Internal
/home/user/Documents/Personal/Photos
/home/user/Documents/Personal/Finance

Command breakdown:

  • -mindepth 2 - Skip levels 0 and 1
  • -maxdepth 2 - Don't go deeper than level 2
  • Result: Only level 2 directories shown

Use case: Listing client project folders within a work directory while excluding parent folders and deeper subfolders.

3: Pretty Tree View with Depth Limit

Use tree command for visual hierarchy with controlled depth:

tree -d -L 2 /var/www

Output Result:

/var/www
├── html
│   ├── assets
│   └── uploads
├── api
│   ├── v1
│   └── v2
└── admin
    ├── dashboard
    └── settings

Options explained:

  • -d - Directories only
  • -L 2 - Limit depth to 2 levels
  • Shows visual tree structure with indentation

Install tree if needed:

sudo apt install tree  # Ubuntu/Debian
brew install tree      # macOS

Common Use Cases

Project Analysis: Understand codebase structure without deep dive

Documentation: Generate folder hierarchy for README files

Backup Planning: Identify top-level directories for backup scripts

DevOps: List deployment folders at specific infrastructure levels

Data Science: Navigate dataset directories organized by category/year

Quick Reference

Command Depth Behavior Use Case
-maxdepth 1 Current dir + immediate subdirs Quick overview
-maxdepth 2 Two levels deep Project structure
-mindepth 2 -maxdepth 2 Exact level 2 only Specific tier
tree -L 3 Visual tree, 3 levels Documentation

Resources