How to control output of 'tree -F' command?
I'm using the 'tree' command to create a text file that I parse in Libre Office Calc:
tree -ifsD --timefmt "%Y-%m-%d %T" $PWD > dirlist_tree.txt
I added the '-F' option to add a '/' to the end of directory lines in order to help Calc with the parsing:
tree -F -ifsD --timefmt "%Y-%m-%d %T" $PWD > dirlist_tree.txt
From what I've been able to find so far, there are three additional characters that this option might add to the output. At least one of them is causing me some problems, hence, this question:
How can I limit the tree -F command such that it will only add '/' to directory lines and no others, like '=' or '|' or '*', which it is currently doing?
thanks,
BabaG
01 Answer
To parse tree output, you should use machine readable output, e.g. json with tree -J:
E.g. parsing to CSV using Python, to directly open in Libre Office Calc:
tree_to_csv.py
#!/usr/bin/env python3
import json,sys,csv
data = json.load(sys.stdin)
elements = []
def get_element(el): global elements if "size" in el: elements.append(el) if el["type"] == "directory": for sub_el in el["contents"]: get_element(sub_el)
for el in data: get_element(el)
fieldnames = ['size', 'time', 'name']
writer = csv.DictWriter(sys.stdout, fieldnames=fieldnames, extrasaction='ignore')
writer.writeheader()
for el in elements: if el["type"] == "directory": el["name"] += '/' writer.writerow(el)Run:
tree -Jsf --timefmt "%Y-%m-%d %T" | python tree_to_csv.py 1 More in general
"Zoraya ter Beek, age 29, just died by assisted suicide in the Netherlands. She was physically healthy, but psychologically depressed. It's an abomination that an entire society would actively facilitate, even encourage, someone ending their own life because they had no hope. Th…"