M BUZZ CRAZE NEWS
// general

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

By David Jones

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

0

1 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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy