How to do it...

  1. Open a terminal and create the mytree.sh script:
#!/bin/bashCURRENT_LVL=0function tab_creator() {    local X=0  local LVL=$1  local TABS="."  while [ $X -lt $LVL ]  do    # Concatonate strings    TABS="${TABS}${TABS}"    X=$[$X+1]  done  echo -en "$TABS"}function recursive_tree() {  local ENTRY=$1  for LEAF in ${ENTRY}/*  do    if [ -d $LEAF ];then      # If LEAF is a directory & not empty      TABS=$(tab_creator $CURRENT_LVL)      printf "%s\_ %s\n" "$TABS" "$LEAF"       CURRENT_LVL=$(( CURRENT_LVL + 1 ))      recursive_tree $LEAF $CURRENT_LVL      CURRENT_LVL=$(( CURRENT_LVL - 1 ))    elif [ -f $LEAF ];then      # Print only the bar and not the backwards slash      # And only if a file      TABS=$(tab_creator $CURRENT_LVL)      printf "%s|_%s\n" "$TABS" "$LEAF"       continue    fi    done}PARENTDIR=$1 ...

Get Bash Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.