blu*_*ray 3 command-line bash scripts
我几乎没有tree一些目录结构的命令输出(在文本文件中)。它看起来像:
% cat tree.txt\n.\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 grandpartest\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 partest\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 test\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 empty-asciidoc-document.adoc\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 empty-asciidoc-document1.adoc\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 grandpartest2\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 partest2\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 test2\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 empty-asciidoc-document.adoc\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 empty-asciidoc-document1.adoc\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 empty-asciidoc-document2.adoc\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 grandpartest3\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 partest3\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 test3\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 empty-asciidoc-document.adoc\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 empty-asciidoc-document1.adoc\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 empty-asciidoc-document2.adoc\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 empty-asciidoc-document3.adoc\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 tree.txt\n\n9 directories, 10 files\nRun Code Online (Sandbox Code Playgroud)\n有什么方法可以解析这些文本文件以创建类似的目录结构吗?
\n我知道我可以使用mkdir -p和touch创建这个目录结构。但我感兴趣的主要事情是解析文本文件以获取与这些命令一起使用的值。
更新1:
\n根据@muru的要求
\n% cat tree-j.txt\n[{"type":"directory","name": ".","contents":[\n {"type":"directory","name":"grandpartest","contents":[\n {"type":"directory","name":"partest","contents":[\n {"type":"directory","name":"test","contents":[\n {"type":"file","name":"empty-asciidoc-document.adoc"},\n {"type":"file","name":"empty-asciidoc-document1.adoc"}\n ]}\n ]}\n ]},\n {"type":"directory","name":"grandpartest2","contents":[\n {"type":"directory","name":"partest2","contents":[\n {"type":"directory","name":"test2","contents":[\n {"type":"file","name":"empty-asciidoc-document.adoc"},\n {"type":"file","name":"empty-asciidoc-document1.adoc"},\n {"type":"file","name":"empty-asciidoc-document2.adoc"},\n {"type":"directory","name":"Untitled Folder","contents":[\n ]}\n ]}\n ]}\n ]},\n {"type":"directory","name":"grandpartest3","contents":[\n {"type":"directory","name":"partest3","contents":[\n {"type":"directory","name":"test3","contents":[\n {"type":"file","name":"empty-asciidoc-document.adoc"},\n {"type":"file","name":"empty-asciidoc-document1.adoc"},\n {"type":"file","name":"empty-asciidoc-document2.adoc"},\n {"type":"file","name":"empty-asciidoc-document3.adoc"}\n ]}\n ]}\n ]},\n {"type":"file","name":"tree.txt"},\n {"type":"file","name":"tree-j.txt"}\n ]},\n {"type":"report","directories":10,"files":11}\n]\n\n% cat tree-j.txt | parse-tree\nTraceback (most recent call last):\n File "/home/blueray/_resources/dotfiles/python/parse-tree", line 18, in <module>\n process(structure)\n File "/home/blueray/_resources/dotfiles/python/parse-tree", line 10, in process\n os.mkdir(entry["name"])\nFileExistsError: [Errno 17] File exists: '.'\nRun Code Online (Sandbox Code Playgroud)\n
解析通常的输出tree是很棘手的 - 它不是某种标准形式,例如 CSV。像 JSON 这样的标准结构化格式会更好,因为您需要对每个条目至少编码两到三条信息(名称、类型以及附加信息,例如链接目标或目录条目,这些信息会根据文件类型而变化)。tree -J确实提供了非常简单的 JSON 输出,您可以使用 Python 来处理它,Python 是默认 Ubuntu 安装的一部分,并且在标准库中具有 JSON 处理功能:
#! /usr/bin/env python3
import json
import os
import sys
def process(entries):
for entry in entries:
if entry["type"] == "directory":
os.makedirs(entry["name"], exist_ok=True) # Thanks @pLumo
os.chdir(entry["name"])
process(entry.get("contents", []))
os.chdir('..')
if entry["type"] == "file":
with open(entry["name"], "w"): pass
if entry["type"] == "link":
os.symlink(entry["name"], entry["target"])
# read standard input
structure = json.load(sys.stdin)
process(structure)
Run Code Online (Sandbox Code Playgroud)
这只处理目录、常规文件和链接;您需要为其他文件类型添加条件(不确定如何tree处理块设备、字符设备等)。