Results 1 to 8 of 8

Thread: Display a directory tree

  1. #1
    Join Date
    Apr 2014
    Beans
    4

    Display a directory tree

    Hi, I need to make a directory tree which display the full path of the directory whose tree is being created...looks like this :

    Code:
    `---Folder1
    |      `---text1.txt
    |      `---text2.txt
    `---Folder2
    |      `---subfolder1
    |      |      `---text3.txt
    |      `---subfolder2
    |      |      `---subfolder3
    |      |      |      `---text4.txt
    |      |      `---subfolder4
    |      |      |      `---text5.txt
    |      `---subfolder5
    |      |      `---text6.txt
    `---text7.txt
    `---text8.txt
    Is there anyway to do this without using the sed or awk feature ?
    Thank you.

  2. #2
    Join Date
    Apr 2012
    Beans
    5,430

    Re: Display a directory tree

    There's a nice program for that called... 'tree'

    IIRC it's not installed by default, but you can install from your favorite package manager, or from the command line

    Code:
    sudo apt-get install tree

  3. #3
    Join Date
    Apr 2014
    Beans
    4

    Re: Display a directory tree

    Thanks for the reply!
    But I actually need to do this on a bash shell...

  4. #4
    Join Date
    Jan 2007
    Location
    Toronto ON Canada
    Beans
    1,077
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Display a directory tree

    Quote Originally Posted by soul2 View Post
    Thanks for the reply!
    But I actually need to do this on a bash shell...
    That's where it works - I don't yet know if it will follow links somehow though.....

    OK - it will - use
    Code:
    tree -lshn
    for a list with follow links on, sizes, human readable as an example. Lets you turn color on and off, ASCII or graphic chars, set max level to go down... etc etc
    Last edited by freebird54; April 7th, 2014 at 01:25 AM.
    |Intel E6550 2.33 | Intel mb/gfx/snd | 4 Gb RAM | Seagate 500Gb | Magnasonic 19" | Ubuntu 12.04 | AMD A10 5800K 3.8GHz / ATI gfx| Asus A88X-Pro mb/snd | 8 Gb RAM | Corsair Force 128 GB SSD | Seagate 3+2TB | USB 2TB | Sony 40" | Ubuntu 12.04 / 14.04 |

  5. #5
    Join Date
    Apr 2014
    Beans
    4

    Directory tree doesn't showing the full path

    Hi,
    I am writing a bash shell script that display the contents of a directory in the form of a tree structure. The output of the directory is suppose to be like this when I type in the command "./Script1.sh subdirectory" in the main Directory:

    Code:
    `---Folder1
    |      `---text1.txt
    |      `---text2.txt
    `---Folder2
    |      `---subfolder1
    |      |      `---text3.txt
    |      `---subfolder2
    |      |      `---subfolder3
    |      |      |      `---text4.txt
    |      |      `---subfolder4
    |      |      |      `---text5.txt
    |      `---subfolder5
    |      |      `---text6.txt
    `---text7.txt
    `---text8.txt
    However, my final output is like this:
    Code:
    `---Folder1
    `---Folder2
    `---text7.txt
    `---text8.txt
    What happen ? why it is not showing the full path of the directory ?
    and when I try to move the script into the sub directory and test it again, it does showing what I am expecting to see

    Please help...

  6. #6
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!
    Distro
    Ubuntu Development Release

    Re: Display a directory tree

    Please don't start multiple threads on the same topic. Threads merged.

  7. #7
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!
    Distro
    Ubuntu Development Release

  8. #8
    Join Date
    Apr 2014
    Beans
    4

    Re: Display a directory tree

    Quote Originally Posted by sisco311 View Post
    Could you post the script?
    This is the one that goes worong...
    Code:
    #!/bin/bash
    #echo $(ls -a $1)
    
    
    param=2
    tab=0
    zero=0
    if [ $# -ge $param ]
    then
      tab=$2
    fi
    
    
    
    
    for i in $(ls -a $1)
    do
      if [[ $i != "." && $i != ".." ]]
      then
        directory=$1
        directory+=$i
        directory+="/"
    
    
        j=0
    
    
        while [ "$j" -lt "$tab" ]
        do
          echo -ne "|\t"
          ((j++))
        done
    
    
        echo -ne "\`---"
        echo $i
        if [ -d "$directory" ]
        then
          #echo -ne ""
        #  echo $directory
          sp=$tab
          ((sp++))
          ./$0 $directory $sp
    
    
        fi
      fi
    
    
    
    
    done

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •