You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tree.sh 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/bin/sh
  2. #######################################################
  3. # UNIX TREE #
  4. # Version: 2.3 #
  5. # File: ~/apps/tree/tree.sh #
  6. # #
  7. # Displays Structure of Directory Hierarchy #
  8. # ------------------------------------------------- #
  9. # This tiny script uses "ls", "grep", and "sed" #
  10. # in a single command to show the nesting of #
  11. # sub-directories. The setup command for PATH #
  12. # works with the Bash shell (the Mac OS X default). #
  13. # #
  14. # Setup: #
  15. # $ cd ~/apps/tree #
  16. # $ chmod u+x tree.sh #
  17. # $ ln -s ~/apps/tree/tree.sh ~/bin/tree #
  18. # $ echo "PATH=~/bin:\${PATH}" >> ~/.profile #
  19. # #
  20. # Usage: #
  21. # $ tree [directory] #
  22. # #
  23. # Examples: #
  24. # $ tree #
  25. # $ tree /etc/opt #
  26. # $ tree .. #
  27. # #
  28. # Public Domain Software -- Free to Use as You Like #
  29. # http://www.centerkey.com/tree - By Dem Pilafian #
  30. #######################################################
  31. echo
  32. if [ "$1" != "" ] #if parameter exists, use as base folder
  33. then cd "$1"
  34. fi
  35. pwd
  36. ls -R | grep ":$" | \
  37. sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/'
  38. # 1st sed: remove colons
  39. # 2nd sed: replace higher level folder names with dashes
  40. # 3rd sed: indent graph three spaces
  41. # 4th sed: replace first dash with a vertical bar
  42. if [ `ls -F -1 | grep "/" | wc -l` = 0 ] # check if no folders
  43. then echo " -> no sub-directories"
  44. fi
  45. echo
  46. exit