I have a large list of directories and filenames in the format
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
And would like to create the standard tree
output. Specifically, only showing directories and only down to level 3 e.g. tree -L 3 -d
:
├── Directory\ One
│ ├── Subdir
├── Directory\ Two
│ ├── Subdir
│ ├── Subdir\ Something
│ └── Subdir\ Two
├── Directory\ Other
│ └── Subdir
I can accomplish this with a decently-complex bash script, but I'm suspecting there is an easier way
2 Answers 2
This wasn't available back then but as of Version 1.8.0
(11/16/2018) tree
has a --fromfile
option: "Reads a directory listing from a file rather than the file-system."
So, if you properly format your sample infile
like
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
then you can run
tree --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
To only show directories and only down to level 3:
tree -L 3 -d --fromfile infile
infile
└── Archive
├── Directory One
│ ├── Subdir
│ └── Subdir Two
├── Directory Other
│ └── Subdir
└── Directory Two
└── Subdir Something
8 directories
-
Being able to only show a limited number of levels with
--fromfile
would be great, but mytree
version 1.8.0 simply ignores the-L
option when using the--fromfile
option. So your last example is not valid, imo.Bastian– Bastian2023年07月11日 07:27:45 +00:00Commented Jul 11, 2023 at 7:27
Here's my own answer, but probably improvable:
# 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\(.*\)|\"Archive1円\"|' | xargs mkdir -p
# Use tree as you would normally
tree -L 2 -d Archive
The directory list is a huge archive (200+ GB) so this approach takes a while