How to convert string to a tree like listing?

Hi,

Is anyone able to advise how to do this?

I only have tree 1.6 and I don't think tree works anyway in regards to accepting a string and parsing it to create a hierarchical tree listing.

I want to be able to convert C:\Users\mickey\tmp\Scratchpad.txt to a tree list of some sort like below:

C:\Users
 |
 +--+ \mickey
    |
    +--+ \tmp
       |
       +-- \Scratchpad.txt

And for /home/mickey/tmp/Scratchpad.txt to be like below:

/home
 |
 +--+ /mickey
    |
    +--+ /tmp
       | 
       +-- /Scratchpad.txt

At the moment, doing it manually :frowning:

Even as simply like below would be nice :slight_smile:

C:\Users
== \mickey
==== \tmp
====== \Scratchpad.txt

/home
== /mickey
==== /tmp
====== /Scratchpad.txt

Using tree, it always just print the current directory as dot (.) Can't get it to print the current directory instead

is this on Windows ?

A simple one:

find "$PWD" | awk -F'[\\/]' '{for(i=1;i<NF-1;i++)printf "| "; if(NF>1)printf "+-- "; print $NF}'

A variant:

find "$PWD" | awk -F'[\\/]' '{for(i=1;i<NF;i++)printf "=="; if(NF>1)printf " "; print $NF}'

A full path in tree:

tree "$PWD"
2 Likes

another (there's many ways...)

cat treeMe.awk
{
	spaces=""
	for ( fld=2; fld <= NF; fld++){
		printf("%s%s%s\n",fld == 2 ? $(fld-1): "", FS , $fld  )
		if ( fld+1 > NF )
			break
		
		printf(" %s|\n%s +--+ ", spaces, spaces )
		spaces=(spaces "   ")
	}
}

echo "C:\test\my\printing\example.txt" | awk -F'\\' -f treeMe.awk
C:\test
 |
 +--+ \my
    |
    +--+ \printing
       |
       +--+ \example.txt


echo "/test/my/printing/example.txt" | awk -F'/' -f treeMe.awk
/test
 |
 +--+ /my
    |
    +--+ /printing
       |
       +--+ /example.txt

echo $PWD | awk -F'/' -f treeMe.awk
/home
 |
 +--+ /munke
    |
    +--+ /code
       |
       +--+ /scripts
          |
          +--+ /test
             |
             +--+ /myTree
1 Like