Skip to content

Instantly share code, notes, and snippets.

@dpilafian dpilafian/tree.sh
Last active Feb 5, 2019

Embed
What would you like to do?
Unix/Linux Folder Tree
#!/bin/bash
#####################################################
# Unix/Linux Folder Tree #
# Version: 2.8 #
# File: ~/apps/tree/tree.sh #
# #
# Displays structure of folder hierarchy #
# ------------------------------------------------- #
# This tiny script uses "ls", "grep", and "sed" #
# in a single command to show the nesting of #
# subfolders. #
# #
# Setup: #
# $ cd ~/apps/tree #
# $ chmod +x tree.sh #
# $ ln -s tree.sh /usr/local/bin/tree #
# $ which tree #
# #
# Usage: #
# $ tree [FOLDER] #
# #
# Examples: #
# $ tree #
# $ tree /etc/apache2 #
# $ tree .. #
# #
# WTFPL ~ https://centerkey.com/tree ~ Dem Pilafian #
#####################################################
echo
test -z "$1" || cd "$1" #if parameter exists, use as base folder
pwd
ls -R | grep "^[.]/" | sed -e "s/:$//" -e "s/[^\/]*\//--/g" -e "s/^/ |/"
# grep: select folders (filter out files)
# 1st sed: remove trailing colon
# 2nd sed: replace higher level folder names with dashes
# 3rd sed: indent graph and add leading vertical bar
topFolders=$(ls -F -1 | grep "/" | wc -l)
test $topFolders -ne 0 || echo " --> no subfolders"
echo
#!/bin/bash
##########################
# Unix/Linux Folder Tree #
##########################
# To make this file runnable:
# $ chmod +x *.sh.command
banner="Publish tree.sh"
projectHome=$(cd $(dirname $0); pwd)
displayIntro() {
cd $projectHome
echo
echo $banner
echo $(echo $banner | sed s/./=/g)
pwd
echo
}
publishWebFiles() {
cd $projectHome
publishWebRoot=$(grep ^DocumentRoot /private/etc/apache2/httpd.conf | awk -F'"' '{ print $2 }')
publishFolder=$publishWebRoot/centerkey.com/tree
publish() {
echo "Publishing:"
echo $publishFolder
cp -v tree.sh $publishFolder
echo
}
test -w $publishFolder && publish
}
launchBrowser() {
cd $projectHome
url=https://centerkey.com/tree/
test -w $publishFolder && url=http://localhost/centerkey.com/tree/
echo "Opening:"
echo $url
sleep 2
open $url
echo
}
displayIntro
publishWebFiles
launchBrowser
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.