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

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

Last updated Apr 28, 2021

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

Related Posts

Creating a config file and using it in tandem with your scripts

In this article, we are going to create a config file and use it in our shell script.PrerequisitesBesides having a terminal open, you need basic knowledge of creating scripts and config files.Write scriptNow, we are going to create a script and config file. The...

Calculating and reducing the runtime of a script

In this article, we are going to learn how to calculate and reduce the script’s runtime. A simple time command will help in calculating the execution time.PrerequisitesBesides having a terminal open, make sure you have the necessary scripts present in your...

Using Bash to monitor battery life and optimize it

In this article, we will learn about the TLP Linux tool. TLP is a command-line tool; it is used for power management and will optimize the battery life.PrerequisitesBesides having a Terminal open, you need to ensure that you have TLP installed on your system.How to do...

Creating a simple NAT and DMZ firewall using bash script

In this article, we will create a simple NAT firewall with DMZ using iptables.PrerequisitesBesides having a Terminal open, you need to ensure that iptables is installed in your machine.Write scriptWe will write a script to set up a DMZ using iptables. Create...

Follow Us

Our Communities

More on BASH

The Ultimate Managed Hosting Platform
Load WordPress Sites in as fast as 37ms!

0 Comments

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

five × 2 =

Shares