我有以下目录结构:
bocoup_data/
├── text_reuse
│ └── v2
│ └── json
│ ├── alignments
│ └── texts
└── topic_modelling
└── v2
└── json
我希望能够显示这些目录中的文件子集,因为上图中的某些目录有数千个文件。tree
在 Ubuntu 中调用命令时,有没有办法只显示每个目录中的前 n 个文件?
答案1
tree
因此没有任何选项,但您可以阻止它打印包含超过n
条目的目录的文件:
$ tree /usr --filelimit 10
/usr
├── bin [3260 entries exceeds filelimit, not opening dir]
├── include [1110 entries exceeds filelimit, not opening dir]
├── lib [3700 entries exceeds filelimit, not opening dir]
├── lib32 [610 entries exceeds filelimit, not opening dir]
├── lib64 -> lib
├── local
│ ├── bin
│ │ ├── gpg1v -> /usr/bin/gpgv
│ │ └── vless
│ ├── etc
│ ├── games
│ ├── include
│ ├── lib
│ ├── man
│ ├── sbin
│ ├── share
│ │ └── man -> ../man
│ └── src
├── sbin -> bin
├── share [243 entries exceeds filelimit, not opening dir]
└── src
19 directories, 2 files
答案2
shellfind
命令具有将输出限制为特定最大期限的文件的选项。这可能会对您有所帮助。
答案3
同样的问题也出现在苏, 和格拉伦为其编写了一个 Python 脚本,因为tree
没有选项:
用法:
tree.py -f [file limit] <directory>
如果为 -f [文件限制] 指定了一个数字,则
... <additional files>
打印并跳过其他文件。但是不应跳过其他目录。如果文件限制设置为 10000(默认),则不作为限制#! /usr/bin/env python # tree.py # # Written by Doug Dahms # modified by glallen @ StackExchange # # Prints the tree structure for the path specified on the command line from os import listdir, sep from os.path import abspath, basename, isdir from sys import argv def tree(dir, padding, print_files=False, limit=10000): print padding[:-1] + '+-' + basename(abspath(dir)) + '/' padding = padding + ' ' limit = int(limit) files = [] if print_files: files = listdir(dir) else: files = [x for x in listdir(dir) if isdir(dir + sep + x)] count = 0 for file in files: count += 1 path = dir + sep + file if isdir(path): print padding + '|' if count == len(files): tree(path, padding + ' ', print_files, limit) else: tree(path, padding + '|', print_files, limit) else: if limit == 10000: print padding + '|' print padding + '+-' + file continue elif limit == 0: print padding + '|' print padding + '+-' + '... <additional files>' limit -= 1 elif limit <= 0: continue else: print padding + '|' print padding + '+-' + file limit -= 1 def usage(): return '''Usage: %s [-f] [file-listing-limit(int)] <PATH> Print tree structure of path specified. Options: -f Print files as well as directories -f [limit] Print files as well as directories up to number limit PATH Path to process''' % basename(argv[0]) def main(): if len(argv) == 1: print usage() elif len(argv) == 2: # print just directories path = argv[1] if isdir(path): tree(path, ' ') else: print 'ERROR: \'' + path + '\' is not a directory' elif len(argv) == 3 and argv[1] == '-f': # print directories and files path = argv[2] if isdir(path): tree(path, ' ', True) else: print 'ERROR: \'' + path + '\' is not a directory' elif len(argv) == 4 and argv[1] == '-f': # print directories and files up to max path = argv[3] if isdir(path): tree(path, ' ', True, argv[2]) else: print 'ERROR: \'' + path + '\' is not a directory' else: print usage() if __name__ == '__main__': main()
运行时,它应该产生类似以下内容的输出:
user@host /usr/share/doc $ python /tmp/recipe-217212-1.py -f 2 . | head -n 40 +-doc/ | +-libgnuradio-fft3.7.2.1/ | | | +-copyright | | | +-changelog.Debian.gz | +-libqt4-script/ | | | +-LGPL_EXCEPTION.txt | | | +-copyright | | | +-... <additional files> | +-xscreensaver-gl/ | | | +-copyright | | | +-changelog.Debian.gz | | | +-... <additional files>