Mac 下使用tree命令列目录

问题描述

相信很多使用过Linux的用户都用过tree命令,它可以像windows的文件管理器一样清楚明了的显示目录结构。不过有是有并不是系统本身就自带的,如果需要的话,Centos下可执行下列命令安装。

# yum install tree
[root@izbp12xhp8qv06m0aryf6jz web]# tree
.
├── admin.py
├── admin.pyc
├── apps.py
├── __init__.py
├── __init__.pyc
├── migrations
│   ├── 0001_initial.py
│   ├── 0001_initial.pyc
│   ├── 0002_auto_20180512_0757.py
│   ├── 0002_auto_20180512_0757.pyc
│   ├── 0003_group_user.py
│   ├── 0003_group_user.pyc
│   ├── 0004_args.py
│   ├── 0004_args.pyc
│   ├── 0005_assert.py
│   ├── 0005_assert.pyc
│   ├── 0006_auto_20180517_0457.py
│   ├── 0006_auto_20180517_0457.pyc
│   ├── __init__.py
│   └── __init__.pyc
├── models.py
├── models.pyc
├── __pycache__
│   └── __init__.cpython-36.pyc
├── tests.py
├── uploadpic.py
├── uploadpic.pyc
├── url.py
├── url.pyc
├── views.py
└── views.pyc

2 directories, 29 files

那么在 macOS 系统中如何使用 tree 呢?

知识背景

一般在Mac上配置环境变量时经常要创建、编辑 .bash_profile文件。
创建该文件时一般都会选择在当前用户目录下,即Mac下的.bash_profile 文件的路径是 /Users/YourMacUserName/.bash_profile (如果该文件已经创建过的话)。

1、创建 .bash_profile

(1) 启动终端

(2) 进入当前用户的home目录(默认就是): 
    cd ~   或 cd /Users/YourMacUserName  

(3)输入touch .bash_profile

2、查看 、编辑 .bash_profile 文件

(1)终端输入 open -e .bash_profile 
(如果只是查看,直接使用open .bash_profile)

(2)编辑

(3)关闭即可保存修改

3、更新刚配置的环境变量

输入 source .bash_profile

解决方案

方案1

按照上述只是背景可以知道,我们增加一个脚本资源 tree 即可有 tree 命令了:

打开 .bash_profile 并在里面添加如下一行代码:

  alias tree="find . -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'" 

保存退出后,更新刚配置的环境变量脚本资源,这是再在终端下试一下tree命令,看一看是不是成功了!!

soudu tree
.
|____.DS_Store
|____soudu
| |____.DS_Store
| |____spiders
| | |______init__.py
| | |______pycache__
| | | |____title.cpython-36.pyc
| | | |______init__.cpython-36.pyc
| | |____title.py
| |______init__.py
| |______pycache__
| | |____pipelines.cpython-36.pyc
| | |____items.cpython-36.pyc
| | |____settings.cpython-36.pyc
| | |______init__.cpython-36.pyc
| |____middlewares.py
| |____settings.py
| |____items.py
| |____pipelines.py
|____scrapy.cfg

上面指令的缺点也暴露出来了,我们无法指定遍历层级.所以决定安装 tree 工具

方案2

mac 下使用 brew包管理工具安装 tree

$ brew install tree

安装成功后,直接在终端使用, 使用 --help 查看帮助信息

$ tree --help
usage: tree [-acdfghilnpqrstuvxACDFJQNSUX] [-H baseHREF] [-T title ]
    [-L level [-R]] [-P pattern] [-I pattern] [-o filename] [--version]
    [--help] [--inodes] [--device] [--noreport] [--nolinks] [--dirsfirst]
    [--charset charset] [--filelimit[=]#] [--si] [--timefmt[=]<f>]
    [--sort[=]<name>] [--matchdirs] [--ignore-case] [--] [<directory list>]
  ------- Listing options -------
  -a            All files are listed.
  -d            List directories only.
  -l            Follow symbolic links like directories.
  -f            Print the full path prefix for each file.
  -x            Stay on current filesystem only.
  -L level      Descend only level directories deep.
  -R            Rerun tree when max dir level reached.
  -P pattern    List only those files that match the pattern given.
  -I pattern    Do not list files that match the given pattern.
  --ignore-case Ignore case when pattern matching.
  --matchdirs   Include directory names in -P pattern matching.
  --noreport    Turn off file/directory count at end of tree listing.
  --charset X   Use charset X for terminal/HTML and indentation line output.
  --filelimit # Do not descend dirs with more than # files in them.
  --timefmt <f> Print and format time according to the format <f>.
  -o filename   Output to file instead of stdout.
  -------- File options ---------
  -q            Print non-printable characters as '?'.
  -N            Print non-printable characters as is.
  -Q            Quote filenames with double quotes.
  -p            Print the protections for each file.
  -u            Displays file owner or UID number.
  -g            Displays file group owner or GID number.
  -s            Print the size in bytes of each file.
  -h            Print the size in a more human readable way.
  --si          Like -h, but use in SI units (powers of 1000).
  -D            Print the date of last modification or (-c) status change.
  -F            Appends '/', '=', '*', '@', '|' or '>' as per ls -F.
  --inodes      Print inode number of each file.
  --device      Print device ID number to which each file belongs.
  ------- Sorting options -------
  -v            Sort files alphanumerically by version.
  -t            Sort files by last modification time.
  -c            Sort files by last status change time.
  -U            Leave files unsorted.
  -r            Reverse the order of the sort.
  --dirsfirst   List directories before files (-U disables).
  --sort X      Select sort: name,version,size,mtime,ctime.
  ------- Graphics options ------
  -i            Don't print indentation lines.
  -A            Print ANSI lines graphic indentation lines.
  -S            Print with CP437 (console) graphics indentation lines.
  -n            Turn colorization off always (-C overrides).
  -C            Turn colorization on always.
  ------- XML/HTML/JSON options -------
  -X            Prints out an XML representation of the tree.
  -J            Prints out an JSON representation of the tree.
  -H baseHREF   Prints out HTML format with baseHREF as top directory.
  -T string     Replace the default HTML title and H1 header with string.
  --nolinks     Turn off hyperlinks in HTML output.
  ---- Miscellaneous options ----
  --version     Print version and exit.
  --help        Print usage and this help message and exit.
  --            Options processing terminator.


我们要在目录遍历时使用 -L 参数指定遍历层级


$ tree -L 1
.
├── _config.yml
├── db.json
├── gulpfile.js
├── node_modules
├── package-lock.json
├── package.json
├── public
├── scaffolds
├── source
├── static
└── themes

6 directories, 5 files

如果你想把一个目录的结构树导出到文件 Readme.md ,可以这样操作:

$ tree -L 3 > README.md

README.md文档我就不放出来了,tree 还有很多详细的用法可以看帮助文档!!!!

此文章同时同步到我的个人博客緣來來來 » Mac 下使用tree命令列目录

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 157,298评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,701评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 107,078评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,687评论 0 202
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,018评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,410评论 1 211
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,729评论 2 310
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,412评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,124评论 1 239
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,379评论 2 242
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,903评论 1 257
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,268评论 2 251
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,894评论 3 233
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,014评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,770评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,435评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,312评论 2 260

推荐阅读更多精彩内容

  • 官网 中文版本 好的网站 Content-type: text/htmlBASH Section: User ...
    不排版阅读 4,306评论 0 5
  • 下载和安装 下载- 在官网下载安装包然后安装 安装一路默认安装即可然后在终端运行go version,如果显示类似...
    楠小忎阅读 12,208评论 0 2
  • 现在的社会,生活条件越来越好了,人们更加想要去追求自己想要的。好比如说出书,那么对于个人出书具体的流程是怎...
    0d6rB7w7阅读 259评论 0 0
  • 我担心,现有订单业务超过了团队的能力,导致无法有效完成,导致失去客户信任,最终影响到每个人的财务回报。 我担心,没...
    邹敏阅读 234评论 0 0
  • 4.6 事情总会有尾声 对比前段时间的生活,压力大、很被动、很乏味,当然也很累,用了上百种方法去解决问题,发现行不...
    只爱金泽阅读 135评论 0 0