Matthew Orndoff

Matthew Orndoff

Front-End React Developer. I can do all things through Cold Brew 🙏

Use `ls` and `tree` to explore directories in Unix / Linux.

This post dives into a couple of useful commands: ls and tree. Both great commands for figuring out the structure of a directory and its subdirectories on Linux or Unix.

ls

ls lists directory contents.

# List current directory contents, along with its subdirectory contents.
ls -R

# List directory contents in long-format (-l),
# by date/time (-t), in reverse order (-r).
ls -ltr

# List directory contents in long format (-l),
# human-readable format (-h), sort by size (-S)
ls -lhS

tree

tree shows the contents of the current directory and its subdirectories as a tree.

# List contents of directory and subdirectories as a tree.
tree

# List only directories.
tree -d

# List dir contents, going 2 levels deep.
tree -L 2

# List dir contents, display only directories (-d), going 1 level deep.
tree -dL 1

# List dir contents, display all files (-a), going 4 levels deep.
tree -aL 4

# List dir contents, display size of files (-s) in human-readable format (-h).
tree -sh

# Filter tree using wildcard (glob) pattern (-P), display only .html files
tree -P "*.html"

# List dir contents, display size of files (-s) in human-readable format (-h).
# Go 4 levels deep, display only .txt files.

tree -shLP 4 "*.txt"

Comments