4

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

5

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
1
  • Being able to only show a limited number of levels with --fromfile would be great, but my tree version 1.8.0 simply ignores the -L option when using the --fromfile option. So your last example is not valid, imo.
    – Bastian
    Jul 11, 2023 at 7:27
0

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\(.*\)|\"Archive\1\"|' | 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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .