linux tree命令包含文件的头内容

时间:2018-08-22 14:59:17

标签: linux bash

我需要使用树命令..来创建文件raport,包括每个文件的开头内容,例如当前命令示例:

tree -hF

├── [129K]  219.137.24.130
│   └── [125K]  gibi_gibi_member.csv
├── [5.7M]  219.142.104.84
│   ├── [ 43K]  xshop_unihongbaorecord.csv
│   ├── [ 18K]  xshop_zdmemberaddr.csv
│   ├── [2.2M]  xshop_zdmember_copy.csv
│   ├── [2.6M]  xshop_zdmember.csv
│   ├── [7.5K]  xshop_zdmembertelinfo.csv
│   └── [877K]  xshop_zdshareusersubmitinfo.csv
├── [9.9M]  219.84.203.57
│   ├── [259K]  wealth_e_newsletter_subscriptions.csv
│   ├── [ 59K]  wealth_orders.csv
│   └── [9.6M]  wealth_users.csv

我想要这个输出:

├── [9.9M]  219.84.203.57
│   ├── [259K]  wealth_e_newsletter_subscriptions.csv
                ./head 219.84.203.57/wealth_e_newsletter_subscriptions.csv
│   ├── [ 59K]  wealth_orders.csv
                ./head 219.84.203.57/wealth_orders.csv
│   └── [9.6M]  wealth_users.csv
                ./head 219.84.203.57/wealth_users.csv

我该怎么做?

1 个答案:

答案 0 :(得分:0)

使用awktree的组合很容易做到:

tree -hFf | awk -v n=4 '1
                       { file=$NF; sub($NF"$","");gsub(/./," "); 
                         for(i=1;i<=n;++i) {getline var < file; print $0 var}
                         close(file) }'

此处n表示文件应打印多少行。