由于服务器中没有tree命令,且自己没有权限install新命令,只能自个用python实现该命令,费劲

具体实现方法如下

以下操作基于Linux,python2

1. 首先就是随便选个位置,然后新建个py文件(这里选在了"~/.basic_scipt",命名为"tree.py",可参考)。py文件内容如下:

#!/usr/bin/python2
# coding:utf-8
import os
import sys# -d 只显示目录
# -a 显示所有目录
# -L num 显示几层,不显示隐藏文件def print_color(color,argv):if   color == "green":print("\033[32m"+argv+"\033[0m")elif color == "read":print("\033[31m"+argv+"\033[0m")elif color == "yellow":print("\033[33m"+argv+"\033[0m")else :print(argv)def is_hidden(file):file_name = os.path.basename(file)if file_name[0] == '.':return Trueelse:return Falsedef get_len_of_listdir(lst,show_hidden=True):len_of_listdir = 0for file in lst:if show_hidden == True:len_of_listdir += 1elif is_hidden(file) is not True:len_of_listdir += 1return len_of_listdirdef tree_dir(dir, show_hidden_file, direct_only, layer_limit, layer_max,layer=0):layer_max = int(layer_max)files = os.listdir(dir)files.sort()file_lst=[]dir_lst=[]for file in files:file_path = os.path.join(dir, file)if os.path.isdir(file_path):dir_lst.append(file)else:file_lst.append(file)if show_hidden_file == False:len_of_listdir = get_len_of_listdir(dir_lst)len_of_listfile = get_len_of_listdir(file_lst)else:len_of_listdir = get_len_of_listdir(dir_lst,show_hidden=False)len_of_listfile = get_len_of_listdir(file_lst,show_hidden=False)if direct_only is not True:index = 0for file in file_lst:file_path = os.path.join(dir, file)        if layer_limit == True and layer >= layer_max:continueif show_hidden_file == False and is_hidden(file) is True:continue index += 1print(" |   " * (layer)),if (layer >= 0):if index == len_of_listfile and len_of_listdir == 0:print("|__ "),else:print("|__ "),print(file)index = 0for file in dir_lst:file_path = os.path.join(dir, file)        if layer_limit == True and layer >= layer_max:continueif show_hidden_file == False and is_hidden(file) is True:continue   index += 1print(" |   " * (layer)),if (layer >= 0):if index == len_of_listdir:print("|__ "),else:print("|__ "),print_color("green", file)tree_dir(file_path, show_hidden_file, direct_only, layer_limit, layer_max,layer + 1)def parse_option(op_str):print("option:" + op_str)import relayer_limit = Falselayer_max = 0direct_only = Falseshow_hidden_file = Falsematch_str = format("-L[\s]+\d")search_ret = re.search(match_str, op_str)if search_ret is not None:layer_limit = Truematch_str = format("\d")search_ret = re.search(match_str, search_ret.group(0))if search_ret is not None:layer_max = search_ret.group(0)match_str = format("-a")search_ret = re.search(match_str, op_str)if search_ret is not None:show_hidden_file = Truematch_str = format("-d")search_ret = re.search(match_str, op_str)if search_ret is not None:direct_only = Truereturn show_hidden_file, direct_only, layer_limit, layer_maxif __name__ == '__main__':file_name = sys.argv[0]if len(sys.argv) < 2:print_color("red", "args invalid\n")sys.exit(1)path_dir = sys.argv[1]op = ""for idx in range(len(sys.argv)):if idx >= 2:op += " " + sys.argv[idx]show_hidden_file, direct_only, layer_limit, layer_max = parse_option(op)tree_dir(path_dir,show_hidden_file, direct_only, layer_limit, layer_max)sys.exit(0)

2. 功能选项如下所示,详见第五条内容中的代码部分

# -d 只显示目录
# -a 显示所有目录
# -L num 显示几层,不显示隐藏文件

3. 找到home目录下的".bashrc"文件(或".cshrc"文件,均为隐藏文件。这里用的是bashrc文件)。进入文件后编辑,随意找个位置添加一行alias指令,内容如下:

alias tree='python ~/.basic_scipt/tree.py ./'

如果没有找到".bashrc"文件,修改".cshrc"文件亦可,命令如下(bashrc和cshrc修改一个即可):

alias tree 'python ~/.basic_scipt/tree.py ./' #注意没有等号=

4.完成以上操作之后,记得保存并退出。然后在命令终端执行如下指令(source),以使bashrc文件(或cshrc文件)的更改生效:

$ source ~/.bashrc
# 或
$ source ~/.cshrc

5. 在source命令执行完之后,就已经大功告成了,赶紧找个目录tree一下试试吧。如果报错的话,就看看python代码部分有没有错误吧。

$ tree       #没错就这么简单,但还可以加一些参数,如下
$ tree -L 2  #显示两层文件,避免眼花缭乱
$ tree -d    #只显示目录

6. 需要注意的是,该代码是使用的python2平台,如是python3平台的话,估计 肯定会出现不兼容,然后报错。

7. 需要注意的第二点,由于对该命令作了些简化,所以只能实现对当前所在路径的tree,即加一些路径当作参数是不可以的。例:

$ tree ../  #这是错误的用法

python代码参考了其他人的博客,略微做了些修改,忘记出处了,见谅

如果您在实际使用中出现问题或者有更好的解决办法,欢迎留言。

乌拉、利用python实现tree命令相关推荐

  1. python - python实现tree命令

    tree 命令 : 显示目录的树形结构 [root@localhost test]# ls 222 aaaa bbbb cccc.txt [root@localhost test]# tree . ├ ...

  2. 用Python实现tree命令

    实现效果 思路及笔记 """ @name : 实现tree命令 @author : huangshilong @projectname : pythonfunction ...

  3. 利用Python实现PING命令

            一.PING简介 PING(Packet Internet Grope),也称因特网包探索器.PING使用ICMP回送(ECHO)请求和回送(ECHO)应答报文,一般用于测试网络连通性 ...

  4. python 用命令安装pip_利用Python的pip命令安装nump

    >将python安装目录中的script的目录导入到环境变量path中 >在Windows的DOS窗口执行:pip >>将出现pip的相关信息 >在Windows中的DO ...

  5. 安利一款Python开发的仿Linux树形显示目录tree命令

    大家好,我是小小明,今天我要给大家分享一个用python实现的仿Linux的tree命令. 详见:https://pypi.org/project/filestools/ 通过以下命令安装即可直接使用 ...

  6. cmd运行python服务器,python如何利用paramiko执行服务器命令

    话不多说直接上代码 封装连接 @staticmethod def connect(ip, server_user, server_port, server_path): ""&qu ...

  7. Python语言学习:利用python语言实现调用内部命令(python调用Shell脚本)—命令提示符cmd的几种方法

    Python语言学习:利用python语言实现调用内部命令(python调用Shell脚本)-命令提示符cmd的几种方法 目录 利用python语言实现调用内部命令-命令提示符cmd的几种方法 T1. ...

  8. python数据分析方法和命令_《利用Python进行数据分析》 —— (1)

    <利用Python进行数据分析> -- (1) Python的学习需要自主探索各种类型,函数和方法的文档. 2.1 Python解释器 在IPython(Jupyter Qtconsole ...

  9. 三个你必须要记住的Pyinstaller打包命令——利用Python实现打包exe

    点击上方"Python爬虫与数据挖掘",进行关注 回复"书籍"即可获赠Python从入门到进阶共10本电子书 今 日 鸡 汤 未收天子河湟地,不拟回头望故乡. ...

  10. python subprocess使用_python利用subprocess执行交互命令

    已经知道,os.system可以方便的利用python代码执行一些像ping.ipconfig之类的系统命令,但却只能得到命令执行是否成功,不能获得命令成功执行后的结果,像下面这样: 1 >&g ...

最新文章

  1. C++实现stack【栈】
  2. underscore:get elements counts of a array
  3. ntko office在线编辑控件问题记录
  4. Atcoder Educational DP Contest 题解
  5. 数据结构实验之栈与队列九:行编辑器
  6. Python小练习:批量删除多个文件夹内的相同文件
  7. 分层模型:OSI与TCP/IP
  8. Python函数中参数* 和 ** 的区别
  9. embed 标签怎么嵌入pdf_联合Aspect-Sentiment主题嵌入的弱监督的情感分析(2020年10)
  10. CSDN 日报第 1 期:蚂蚁集团将向机构投资者退款;Pyston v2 发布 比 Python 快 20%
  11. 我在Google用AI研究基因,入门从吴恩达的课程开始
  12. Android Studio for Experts(Android Dev Summit2015)
  13. linux下mysql的忘记root密码的解决办法
  14. SolidWorks工程图做到一半切换工程图模板的方法
  15. mongodb WT_ERROR: non-specific WiredTiger error
  16. 推荐个php在线手册,非常的全
  17. 【GamePlay】UGUI的注意事项
  18. MxN螺旋矩阵(由外向内)
  19. Python输出csv文件中文乱码及解决
  20. oracle 表收缩,Oracle在线收缩表——shrink space

热门文章

  1. 远控免杀专题10--TheFatRat免杀
  2. TheFatRat自动化渗透工具
  3. python 离散点 等高线_飞时达软件离散点高程、等高线高程、特征线高程等检查与处理...
  4. oppok3如何刷机_OPPO K3(6GB/64GB/全网通)手机密码忘记怎么刷机?
  5. wxParse空格解析不生效的解决方案
  6. 完整的【ArcGIS地理信息系统空间分析实验教程】(包括光盘数据)
  7. Hadoop集群搭建(六):hadoop配置namenode服务
  8. host地址切换工具-SwitchHosts
  9. 高斯克吕格投影,将经纬度转换为投影坐标
  10. ubuntu 系统获取和阅读 linux 源码