CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / File & Directory Management / tree

tree

Show a directory hierarchy as a tree

Beginnernavigationfilesystemdocumentation

tree draws a directory and everything under it as an indented tree, with counts at the end. It's the fastest way to understand an unfamiliar project, a Helm chart or a config directory, and to paste a layout into a ticket or README.

Syntaxtree [-adfhiLpugJ] [-L LEVEL] [-I PATTERN] [-P PATTERN] [DIRECTORY...]

Watch it run

You've cloned or SSHed into an unfamiliar service and want its layout at a glance.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 4
tree app

tree is a small separate package, not part of a minimal Ubuntu Server or Amazon Linux install. Ubuntu's command-not-found helper tells you which package provides it.

Anatomy of a command

$
commandtree

List contents recursively as a tree. Not installed by default on Ubuntu Server: sudo apt install tree.

How it works

tree reads each directory with the same getdents() system call ls uses, sorts the entries (alphabetically by default, with --dirsfirst or -t to change that) and recurses into subdirectories depth-first, drawing the branch lines with box-drawing characters. It stats entries to know which are directories and, with -p, -u, -g, -h or -D, to print the inode details in brackets. It doesn't follow symlinks to directories unless you pass -l, which avoids loops. Options like -L, -d, -I and -P only change what is printed or descended into. At the end it prints how many directories and files it showed (the starting directory counts as one). With -J or -X it emits JSON or XML instead, which scripts can parse.

Options you'll actually use

-L NDescend at most N levels
-aShow hidden files (names starting with .)
-dDirectories only
-I PATTERNExclude names matching the pattern ('node_modules|.git')
-P PATTERN --pruneShow only files matching the pattern, drop empty branches
-h / --duHuman-readable sizes / directory sizes summed up
-p -u -gShow permissions, owner and group of each entry
-f -iFull paths, no indentation lines (a find-like list)
--dirsfirstList directories before files
-FAppend / to directories, * to executables, @ to symlinks
-JOutput JSON

Common mistakes

tree in / or in a repository with node_modules

It prints every file in the hierarchy: hundreds of thousands of lines that flood the terminal.

tree -L 2, tree -d, or tree -I 'node_modules|.git'
tree -I node_modules|.git

Unquoted, the shell treats | as a pipe and tries to run .git as a command.

tree -I 'node_modules|.git'
Reading tree --du sizes as disk usage

tree sums apparent file sizes; disk usage depends on block allocation and sparse files.

du -sh dir for real disk usage

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Show the structure of ~/app two levels deep, including hidden files.

  2. 2

    Show only the directories under ~/data.

  3. 3

    Show where all .txt files are under ~/data, hiding branches with no .txt files.

  4. 4

    List ~/app/src with permissions, owner and sizes in a human-readable form.

Related commands

lsList directory contentsfindSearch the filesystem by name, size, age, owner and moreduFind out which files and directories use the space
PreviouslocateNextstat
Linux / File & Directory Management / tree

tree

Show a directory hierarchy as a tree

Beginnernavigationfilesystemdocumentation

tree draws a directory and everything under it as an indented tree, with counts at the end. It's the fastest way to understand an unfamiliar project, a Helm chart or a config directory, and to paste a layout into a ticket or README.

Syntaxtree [-adfhiLpugJ] [-L LEVEL] [-I PATTERN] [-P PATTERN] [DIRECTORY...]

Watch it run

You've cloned or SSHed into an unfamiliar service and want its layout at a glance.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 4
tree app

tree is a small separate package, not part of a minimal Ubuntu Server or Amazon Linux install. Ubuntu's command-not-found helper tells you which package provides it.

Anatomy of a command

$
commandtree

List contents recursively as a tree. Not installed by default on Ubuntu Server: sudo apt install tree.

How it works

tree reads each directory with the same getdents() system call ls uses, sorts the entries (alphabetically by default, with --dirsfirst or -t to change that) and recurses into subdirectories depth-first, drawing the branch lines with box-drawing characters. It stats entries to know which are directories and, with -p, -u, -g, -h or -D, to print the inode details in brackets. It doesn't follow symlinks to directories unless you pass -l, which avoids loops. Options like -L, -d, -I and -P only change what is printed or descended into. At the end it prints how many directories and files it showed (the starting directory counts as one). With -J or -X it emits JSON or XML instead, which scripts can parse.

Options you'll actually use

-L NDescend at most N levels
-aShow hidden files (names starting with .)
-dDirectories only
-I PATTERNExclude names matching the pattern ('node_modules|.git')
-P PATTERN --pruneShow only files matching the pattern, drop empty branches
-h / --duHuman-readable sizes / directory sizes summed up
-p -u -gShow permissions, owner and group of each entry
-f -iFull paths, no indentation lines (a find-like list)
--dirsfirstList directories before files
-FAppend / to directories, * to executables, @ to symlinks
-JOutput JSON

Common mistakes

tree in / or in a repository with node_modules

It prints every file in the hierarchy: hundreds of thousands of lines that flood the terminal.

tree -L 2, tree -d, or tree -I 'node_modules|.git'
tree -I node_modules|.git

Unquoted, the shell treats | as a pipe and tries to run .git as a command.

tree -I 'node_modules|.git'
Reading tree --du sizes as disk usage

tree sums apparent file sizes; disk usage depends on block allocation and sparse files.

du -sh dir for real disk usage

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Show the structure of ~/app two levels deep, including hidden files.

  2. 2

    Show only the directories under ~/data.

  3. 3

    Show where all .txt files are under ~/data, hiding branches with no .txt files.

  4. 4

    List ~/app/src with permissions, owner and sizes in a human-readable form.

Related commands

lsList directory contentsfindSearch the filesystem by name, size, age, owner and moreduFind out which files and directories use the space
PreviouslocateNextstat