Quickly Visualize Linux Directory Trees with a Custom tf Bash Script
This guide shows how to create and use a Bash script called tf that leverages the tree command to display directory structures, supports keyword searching, and explains the color codes used to differentiate file types in the output.
The article presents a Bash script named tf that displays the file hierarchy of a specified path (or the current directory) using the tree command and optionally filters the result by a keyword.
Script
#!/usr/bin/env bash
# This command shows the file structure of a given or current path, with optional search
# tf – show current directory structure
# tf keyword – search current directory
# tf dir keyword – search specified directory
# The script depends on the <strong>tree</strong> command
DIR=
KEYWORD=
# If only one argument is given, treat it as a keyword
if [ $# -eq 1 ]; then
KEYWORD=$1
fi
# If two arguments are given, treat them as directory and keyword
if [ $# -eq 2 ]; then
DIR=$1
KEYWORD=$2
fi
if [ -n "$DIR" ]; then
cd "$DIR" || exit 1
fi
pwd
tree -C -f | grep "$KEYWORD"Usage Examples
Show the current directory tree:
tfSearch the current directory for a keyword (e.g., lib): tf lib Search a specific path for a keyword (e.g., /usr/local/ lib):
tf /usr/local/ libColor Meaning in Tree Output
Blue – directories
Green – executable files
Red – compressed files
Light blue – symbolic links
Gray – other file types
Flashing red – broken symbolic links
Yellow – device files
White – regular files such as text, configuration, source code, etc.
The script provides a quick way to explore and search directory structures directly from the command line, making it useful for system administrators and developers who need to locate files efficiently.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
