问题描述
相信很多使用过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命令列目录