Linux下用sheel脚本完整实现tree功能

这篇博客详细介绍了如何在Ubuntu系统中使用Shell脚本来实现tree命令的功能,包括创建树形目录结构,并在此基础上进一步优化,通过颜色区分文件属性,达到与tree命令相似的效果。文章分为两个主要步骤,1.1部分讲解了基础的树形结构实现,1.2部分则展示了如何增加颜色显示,以增强视觉效果。
摘要由CSDN通过智能技术生成

ubuntu下用sheel脚本完整实现tree功能,

1.1 该部分实现树形结构

这里写代码片
!/bin/bash
branch_vline="│   "     #Branch vertical line 
branch_null="    "      #null separation branch 
middle_branch_end="├── "  #middle file branch
last_branch_end="└── "    #the last file branch
branch_sum="" 
tree()  
{   
        #Define local variables, implement the number of files under each file, record the number of files, 
        #and pay attention to the location of this variable definition 
        local num=0 
        for file in *;  
        do  
        #Count the number of directory files for this file 
      thelastfile=`ls |wc -l`
      num=$((num+1))
      #When the file belongs to the last file in that directory, the output is appended to $last_branch_end 
        if [[ $thelastfile -eq $num ]]; then
                if [ -f "$file" ]; then
                    echo -e "${branch_sum}$last_branch_end$file"             
                fi
                #When the file is executable, the display file with green    
                if [ -d "$file" ]; then   
                    echo -e "${branch_sum}$last_branch_end
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值