Reputation: 59
I'd like to see a list of paths in a textfile as a tree in the command line.
I know about the tree utility in gnu/linux, but it seems to default to listing files in the filesystem. Is there a way to give it my list of paths from file so it builds its tree visualization from that instead?
Upvotes: 3
Views: 635
Reputation: 5531
The tree
utility takes the --fromfile
argument, which can be used to pass file paths (one per line).
tree --fromfile /path/to/file.txt
To pass the list over stdin
, use the '.' argument with fromfile.
find ...(args)... | tree --fromfile .
# or e.g. from an array variable
tree --fromfile . <<< $( printf '%s\n' "${an_array[@]}" )
The annoying part about tree
's output in this case is that it puts an extra '.' to represent the filename at the start. E.g.:
find . -type f -iname '*aa*.txt' | tree --fromfile .
.
└── .
└── Food and Diet
└── Bread
└── Flatbreads, Naan, Focaccia.md.txt
4 directories, 1 file
If you're scripting this, you might want to edit the output a bit, e.g. to remove extra '.' and shift the subsequent lines to the left:
# adding -C to tree's arguments so it produces coloured output despite the pipe
find . -type f -iname '*aa*.txt' |
tree -C --fromfile . |
sed -E '1d; ${p;d}; s/....(.*)/\1/'
.
└── Food and Diet
└── Bread
└── Flatbreads, Naan, Focaccia.md.txt
4 directories, 1 file
Upvotes: 0
Reputation: 1758
Is the list of paths in the following format, like the output from the "find" command?
.
./aaa
./aaa/bbb
./aaa/ccc
./ddd
./ddd/eee
For example, using the "awk" command, I think you can get a format similar to the output of tree.
$ cat paths.txt | sort | awk '{n=split($0,a,/\//);for(i=1;i<n-1;i++)printf("| ");if(n>1){printf("+-- ")}print a[n]}'
Upvotes: 3