0

我正在尝试使用 unix“查找”命令获取排序的目录/文件列表。

# find . -type f 
.
./bin
./data
./data/disks
./inc
./inc/calls
./inc/calls/show
./inc/calls/show/system
./inc/calls/show/cli
./inc/calls/show/network
./inc/calls/show/stats
./inc/calls/services
./inc/calls/services/ntp
./inc/calls/services/tsa
./inc/calls/services/webgui
./inc/calls/services/engine
./inc/calls/system
./inc/calls/change
./inc/calls/change/password
./inc/calls/change/network
./inc/calls/disk
./inc/calls/disk/encr
./inc/etc

我想对它进行排序:

./inc/calls/show/system \    
./inc/calls/show/cli \
./inc/calls/show/network \
./inc/calls/show/stats \
./inc/calls/services/ntp \
./inc/calls/services/tsa \
./inc/calls/services/webgui \
./inc/calls/services/engine \
./inc/calls/change/password \
./inc/calls/change/network \
./inc/calls/disk/encr \
./inc/calls/system \
./inc/calls/change \
./inc/calls/services \
./inc/calls/disk \
./inc/calls/show \
./inc/calls \
./data/disks \
./inc/etc \
./bin \
./data \
./inc

哪个节点(目录/文件)有更多的子节点(目录/文件)应该是第一个......我想用bash或python来做......最好的方法是什么?

4

2 回答 2

1

匹配包含/字段数的行并将其添加到/用作分隔符的行中,对字段数进行排序并删除计数。

$ awk -F/ '/\//{print NF,$0}' file | sort -nrk1 | cut -d' ' -f2-
./inc/calls/show/system
./inc/calls/show/stats
./inc/calls/show/network
./inc/calls/show/cli
./inc/calls/services/webgui
./inc/calls/services/tsa
./inc/calls/services/ntp
./inc/calls/services/engine
./inc/calls/disk/encr
./inc/calls/change/password
./inc/calls/change/network
./inc/calls/system
./inc/calls/show
./inc/calls/services
./inc/calls/disk
./inc/calls/change
./inc/etc
./inc/calls
./data/disks
./inc
./data
./bin
于 2013-01-29T12:57:24.590 回答
0

我会使用 python 并尝试转换:

a/b
a/c
b/e/f
b/e/g

在类似的东西:

{'a': {'b': {}, 'c': {}},
'b': {'e': {'f': {}, 'g': {}}},
} 

为达到这个:

def add_list_to_dict(lst,d):
   key, lst = lst[0], lst[1:]
   if not key in d:
       d[key] = {}
   if lst:
       add_list_to_dict(lst,d[key])

d = {}
for path in paths:
    add_list_to_dict(path.split('/'),d)
于 2013-01-29T12:59:13.820 回答