Directory Tree Visualizer Bash Script

marius

Administrator
Staff member
Introduction:
This bash script visualizes the directory structure by listing files and directories in a tree format.

Code Snippet:
Bash:
#!/bin/bash
# Directory Tree Visualizer
if [ -z "$1" ]; then
  DIR="."
else
  DIR="$1"
fi

function tree() {
  for entry in "$1"/*; do
    if [ -d "$entry" ]; then
      echo "|-- $(basename "$entry")/"
      tree "$entry"
    else
      echo "|-- $(basename "$entry")"
    fi
  done
}

echo "Directory tree for $DIR:"
tree "$DIR"

Instructions:
To visualize a directory tree, run the following commands:
Code:
chmod +x tree_visualizer.sh
./tree_visualizer.sh /path/to/directory

Example Output:
Code:
Directory tree for /path/to/directory:
|-- file1.txt
|-- folder1/
|-- folder2/
 
Back
Top