Score:1

How to control output of 'tree -F' command?

cn flag

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

Score:1
in flag

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
BabaG avatar
cn flag
Wow! Thanks for this, pLumo. It's a lot more than I was expecting. I was hoping to keep my process more or less as-is rather than changing to calling a python script. I was hoping there might be some simple qualifier I could add that would tell '-F' to omit '=', '|', and '*' and only apply '/' to directory lines. This will take me a lot of rethinking as I'm not much of a scripter/programmer.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.