Search code examples
unixterminaltreedirectoryls

tree terminal command: Avoid printing all sub folders/files and putting limit


I would like to print all the subdirectories and files from a certain directory. But some of the subfolders have humungous number of files and I would like to cap the number of subdirectories/files they print for each subfolder where it goes over that cap. How do I do it?

Currently I have this situation:

/data$ tree
.
├── filenames.json
├── tripletlists
│   ├── class_tripletlist_test.txt
│   ├── class_tripletlist_train.txt
│   ├── class_tripletlist_val.txt
│   ├── closure_tripletlist_test.txt
│   ├── closure_tripletlist_train.txt
│   ├── closure_tripletlist_val.txt
│   ├── gender_tripletlist_test.txt
│   ├── gender_tripletlist_train.txt
│   ├── gender_tripletlist_val.txt
│   ├── heel_tripletlist_test.txt
│   ├── heel_tripletlist_train.txt
│   └── heel_tripletlist_val.txt
└── ut-zap50k-images
    ├── Boots
    │   ├── Ankle
    │   │   ├── adidas
    │   │   │   ├── 8030969.3.jpg
    │   │   │   └── 8030970.107722.jpg
    │   │   ├── adidas Kids
    │   │   │   ├── 8070145.388249.jpg
    │   │   │   └── 8070146.388250.jpg
    │   │   ├── adidas Originals
    │   │   │   ├── 8027274.372160.jpg
    │   │   │   ├── 8027274.372161.jpg
    │   │   │   ├── 8027310.115329.jpg
    │   │   │   ├── 8027310.183092.jpg
    │   │   │   ├── 8027320.372147.jpg
    │   │   │   └── 8027320.372178.jpg
    │   │   ├── adidas Originals Kids
    │   │   │   ├── 8025627.371498.jpg
    │   │   │   ├── 8025627.74095.jpg
    │   │   │   ├── 8025719.11196.jpg

Solution

  • You can use the flag --filename N in tree --filenames N where N is the number of caps. For example, if I just want to print maximum of four subdirectories or files per subdirectory, you can youse tree --filename 4.

     >> ls
        filenames.json  tripletlists  ut-zap50k-images
    
     >> tree --filelimit 4
        .
        ├── filenames.json
        ├── tripletlists [12 entries exceeds filelimit, not opening dir]
        └── ut-zap50k-images
            ├── Boots [5 entries exceeds filelimit, not opening dir]
            ├── Sandals
            │   ├── Athletic [6 entries exceeds filelimit, not opening dir]
            │   ├── Flat [314 entries exceeds filelimit, not opening dir]
            │   └── Heel [25 entries exceeds filelimit, not opening dir]
            ├── Shoes [10 entries exceeds filelimit, not opening dir]
            └── Slippers
                ├── Boot [6 entries exceeds filelimit, not opening dir]
                ├── Slipper Flats [77 entries exceeds filelimit, not opening dir]
                └── Slipper Heels
                    ├── Daniel Green [8 entries exceeds filelimit, not opening dir]
                    └── L.B. Evans
                        ├── 7590239.255.jpg
                        └── 7590239.72.jpg