Write Linux bash/shell script to Crawl filesystem directories and printing a tree

by | Jan 13, 2021 | BASH

We need an overview of the directory or system’s file structure to view all files in a single place to examine those files.

Here, we will write a bash script to travel directory recursively and check all files and directories to print them in a tree structure on screen.

We will use a recursive function and print tree to write a bash script to Crawling filesystem directories and printing a tree structure of files.

Note:

This exercise is a bit of a fun exercise and certainly recreates the proverbial “wheel”. This can be easily done by running the tree command, however, this will be useful in an upcoming exercise when we’ll be building arrays of arrays for files.

Getting ready

Before writing script, let’s create some test data to test script:

$ mkdir -p parentdir/child_with_kids
$ mkdir -p parentdir/second_child_with_kids
$ mkdir -p parentdir/child_with_kids/grand_kid/
$ touch parentdir/child.txt parentdir/child_with_kids/child.txt parentdir/child_with_kids/grand_kid/gkid1.txt
$ touch parentdir/second_child_with_kids/cousin1.txt parentdir/z_child.txt parentdir/child.txt parentdir/child2.txt

How to do it…

Open a terminal and create the mytree.sh script:

mytree.sh

#!/bin/bash
CURRENT_LVL=0

function 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
recursive_tree $PARENTDIR 1

In your terminal, now run:

$ bash mytree.sh parentdir

How it works…

The creation of filetree.sh is not simple, but the logic we used inside the script is recursive functions. We also used the concept of ${CURRENT_LVL}, which is used to define the number of period or level script from the starting point: maindir. Each directory creates a loop to test each file into the directory and test the entry is a file or directory. If it is a directory, we increment ${CURRENT_LVL}, and then execute the same logic for this directory. This logic is also run again and again recursively for all the directories. If we found a file, we print and continue to do the same logic.

Executing the script should produce an output similar to the following, but notice how the script remembers how many layers deep it might be, and that the directories are shown with a \_ instead of |_:

$ bash mytree.sh parentdir
.|_parentdir/child2.txt
.|_parentdir/child.txt
.\_ parentdir/child_with_kids
..|_parentdir/child_with_kids/child.txt
..\_ parentdir/child_with_kids/grand_kid
....|_parentdir/child_with_kids/grand_kid/gkid1.txt
.\_ parentdir/empty_dir
.\_ parentdir/second_child_with_kids
..|_parentdir/second_child_with_kids/cousin1.txt
.|_parentdir/z_child.txt