I got a comment about using sed command for tree-like output to emulate the similar output to the iconic tree command on my video, the one-liner command reads:
alias tree='find . -print | sed -e '\''s;[^/]*/;|____;g;s;____|; |;g'\'''
With structure like the following code would create:
mkdir -p /tmp/treesed/dir{1,2{,/dir3}} touch /tmp/treesed/dir{1/file1,2/dir3/file2} cd /tmp/treesed
This tree alias is same as the following command:
find . -print |
sed -e 's;[^/]*/;|____;g;s;____|; |;g'
It would produce:
. |____dir2 | |____dir3 | | |____file2 |____dir1 | |____file1
This style is actually looking nice, but I wanted to make it with line in middle of line rather than using underlines, so I changed the styles and reduced the indentation by 1 character:
find . -print |
sed -e 's;[^/]*/;| ;g;s;| \([^|]\);+-- \1;g'
The output:
. +-- dir2 | +-- dir3 | | +-- file2 +-- dir1 | +-- file1
Needless to say, I have to go over to Unicode wonderland, hence the following code with Unicode box drawing symbols:
find . -print |
sed -e 's;[^/]*/;│ ;g;s;│ \([^│]\);├── \1;g'
The output:
. ├── dir2 │ ├── dir3 │ │ ├── file2 ├── dir1 │ ├── file1
The root node with period (.) is fairly annoying, got to be replaced:
find . -print | sed -e '1s/.*/┬/' \ -e 's;[^/]*/;│ ;g;s;│ \([^│]\);├── \1;g'
The output:
┬ ├── dir2 │ ├── dir3 │ │ ├── file2 ├── dir1 │ ├── file1
At this point, I noticed that the branch line of file2 is problematic, so Awk is used.
find . -print | sed -e '1s/.*/┬/' \ -e 's;[^/]*/;│ ;g;s;│ \([^│]\);├── \1;g' | awk ' function print_prev_line() { if (new_pos != old_pos) { sub(/├/, "╰", prev_line) } print prev_line old_pos = new_pos prev_line = $0 } { new_pos = index($0, "├") print_prev_line() } END { new_pos = -1 print_prev_line() } '
The output:
┬ ╰── dir2 │ ╰── dir3 │ │ ╰── file2 ╰── dir1 │ ╰── file1
It looks much better, even those the code size is increased significantly, but I want to make a little adjustment to make dir2 and dir3 connect downward:
find . -print | sed -e '1s/.*/┬/' \ -e 's;[^/]*/;│ ;g;s;│ \([^│]\);├── \1;g' | awk ' function print_prev_line() { if (new_pos != old_pos && substr($0, old_pos, 1) != "│") { sub(/├/, "╰", prev_line) } print prev_line old_pos = new_pos prev_line = $0 } { new_pos = index($0, "├") print_prev_line() } END { new_pos = -1 print_prev_line() } '
The output:
┬ ├── dir2 │ ├── dir3 │ │ ╰── file2 ├── dir1 │ ╰── file1
This is different taste, some might prefer previous one, I am okay with both, but there is still a unresolved issue, there are overhanging branches from dir1 and dir3.
The perfect output should be for me, but I think that’s too much to ask:
┬ ├── dir2 │ ╰── dir3 │ ╰── file2 ╰── dir1 ╰── file1
If you are willing to tackle with my modified code, please so do so and share back with me.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.