title: Shell script to recursively print full directory tree using ls
body: <p>Assignment: I have to create a shell script using diff and sort, and a pipeline using ls -l, grep '^d', and awk '{print $9}' to print a full directory tree. </p>
  
  <p>I wrote a C program to display what I am looking for. Here is the output: </p>
  
  <pre><code>ryan@chrx:~/Documents/OS-Projects/Project5_DirectoryTree$ ./a.out 
  TestRoot/
  [Folder1]
    [FolderC]
    [FolderB]
    [FolderA]
  [Folder2]
    [FolderD]
    [FolderF]
    [FolderE]
  [Folder3]
    [FolderI]
    [FolderG]
    [FolderH]
  </code></pre>
  
  <p>I wrote this so far:</p>
  
  <pre><code>ls -R -l $1 | grep '^d' | awk '{print $9}' 
  </code></pre>
  
  <p>to print the directory tree but now I need a way to sort it by folder depth and possibly indent but not required. Any suggestions? I can't use find or tree commands.</p>
  
  <p>EDIT: The original assignment &amp; restrictions were mistaken and changed at a later date. The current answers are good solutions if you disregard the restrictions so please leave them for any people with similar issues. As for the the new assignment in case anybody was wondering. I was to recursively print all sub directories, sort them, then compare them with my program to make sure they have similar results. Here was my solution:</p>
  
  <pre><code>#!/bin/bash
  echo Program:
  ./a.out $1 | sort
  echo Shell Script:
  ls -R -l $1 | grep '^d' | awk '{print $9}' | sort
  
  diff &lt;(./a.out $1 | sort) &lt;(ls -R -l $1 | grep '^d' | awk '{print $9}' | sort)
  DIFF=$?
  
  if [[ $DIFF -eq 0 ]]
  then
      echo "The outputs are similar!"
  fi
  </code></pre>