我有一个很大的目录和文件名列表,格式如下
drwxr-sr-x hamiltont/hamiltont 0 2015-03-11 23:54 Archive/Directory One/Subdir/
-rw-r--r-- hamiltont/hamiltont 21799 2014-01-10 12:52 Archive/Directory One/Subdir/file2.txt
-rw-r--r-- hamiltont/hamiltont 21799 2014-01-10 12:52 Archive/Directory One/Subdir/file3.txt
-rw-r--r-- hamiltont/hamiltont 21799 2014-01-10 12:52 Archive/Directory One/Subdir Two/somefile.txt
-rw-r--r-- hamiltont/hamiltont 21799 2014-01-10 12:52 Archive/Directory Two/Subdir Something/somefile.txt
-rw-r--r-- hamiltont/hamiltont 21799 2014-01-10 12:52 Archive/Directory Other/Subdir/somefile.txt
并想创建标准tree
输出。具体来说,仅显示目录并且仅显示到第 3 级,例如tree -L 3 -d
:
├── Directory\ One
│ ├── Subdir
├── Directory\ Two
│ ├── Subdir
│ ├── Subdir\ Something
│ └── Subdir\ Two
├── Directory\ Other
│ └── Subdir
我可以使用相当复杂的 bash 脚本来完成此任务,但我怀疑有一种更简单的方法
答案1
当时不可用,但截至Version 1.8.0
(2018 年 11 月 16 日)tree
有一个--fromfile
选项:“从文件而不是文件系统中读取目录列表。”
因此,如果您正确格式化示例,infile
例如
Archive/Directory One/Subdir/
Archive/Directory One/Subdir/file2.txt
Archive/Directory One/Subdir/file3.txt
Archive/Directory One/Subdir Two/somefile.txt
Archive/Directory Two/Subdir Something/somefile.txt
Archive/Directory Other/Subdir/somefile.txt
然后你可以运行
树--fromfile infile
infile
└── Archive
├── Directory One
│ ├── Subdir
│ │ ├── file2.txt
│ │ └── file3.txt
│ └── Subdir Two
│ └── somefile.txt
├── Directory Other
│ └── Subdir
│ └── somefile.txt
└── Directory Two
└── Subdir Something
└── somefile.txt
8 directories, 5 files
仅显示目录且仅显示到第 3 级:
树-L 3 -d --fromfile infile
infile
└── Archive
├── Directory One
│ ├── Subdir
│ └── Subdir Two
├── Directory Other
│ └── Subdir
└── Directory Two
└── Subdir Something
8 directories
答案2
这是我自己的答案,但可能可以改进:
# Find all lines with directories
# Remove all ls output before the path is listed
# Recreate the directory tree
grep '^d' archive_filelist.txt | sed 's|.*Archive\(.*\)|\"Archive\1\"|' | xargs mkdir -p
# Use tree as you would normally
tree -L 2 -d Archive
目录列表是一个巨大的存档(200+ GB),因此这种方法需要一段时间