命令行打印文件树列表: tree
时间:2022-05-04
本文章向大家介绍命令行打印文件树列表: tree,主要内容包括Linux & Mac、2. 命令find组合、Windows、Customization、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。
Linux & Mac
1.下载tree
lib
//mac
brew install tree
//centos
yum install tree
//ubuntu
apt-get install tree
用法
//显示所有文件
tree
//显示深度2层
tree -L 2
2. 命令find组合
find . -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g' > structure.txt
移除node_module
find . -print | grep -v "node" | sed -e 's;[^/]*/;|____;g;s;____|; |;g' > structure.txt
缺点: 不能打印深度选择,或者需要更高层次的语法编写。这里姑且先用着。够用了。
Windows
windows自带tree命令。默认只显示目录
//只显示目录
tree
//显示文件
tree /f
//输出到文件
tree /f > structure.txt
但,由于windows命令不熟悉,也不想花时间去学习windows的命令。那么可以装一个git shell或者推荐使用cmder。
Customization
手动写一个列表。先序遍历:
/**
* 先序遍历 postorder traversal 先输出根节点,然后输出子节点
* Created by Ryan Miao on 9/24/17.
*/
public class PostorderTraversal {
@Test
public void testPostOrder() {
String root = "/Users/ryan/workspace/learning/hexo-blog-src";
int stop = 3;
ArrayList<String> ignores = Lists.newArrayList(".git", ".deploy_git", "node_modules", ".DS_Store");
printTree(root, stop, ignores);
}
private void printTree(String rootFile, int stop, List<String> ignores) {
printTree(new File(rootFile), 0, stop, ignores, false, true);
}
private void printTree(File rootFile, int level, int stop, List<String> ignores, boolean isLastChild, boolean isParentLast) {
String name = rootFile.getName();
if (level > stop || ignores.stream().anyMatch(name::contains)) {
return;
}
if (level == 0) {
System.out.println(".");
} else {
prettyPrint(level, rootFile, isLastChild, isParentLast);
}
if (rootFile.isDirectory()) {
File[] files = rootFile.listFiles();
if (files != null) {
int length = files.length;
for (int i = 0; i < length; i++) {
if (i == length - 1) {
//
printTree(files[i], level + 1, stop, ignores, true, isLastChild);
} else {
printTree(files[i], level + 1, stop, ignores, false, isLastChild);
}
}
}
}
}
private void prettyPrint(int level, File file, boolean isLastChild, boolean isParentLast) {
StringBuilder sb = new StringBuilder();
if (level != 1) {
sb.append("│");
}
for (int i = 0; i < level - 2; i++) {
if (isParentLast && i == level - 3) {
sb.append(" ");
break;
}
sb.append(" |");
}
if (level != 1) {
sb.append(" ");
}
if (isLastChild) {
sb.append("└──");
} else {
sb.append("├──");
}
sb.append(file.getName());
System.out.println(sb.toString());
}
}
目前有个bug,就是递归到深入之后,孙子无法得知祖父是不是最终叶子,因此虚线没有去掉。不过,简单能用还是可以的。 console output:
.
├──_config.yml
├──db.json
├──package-lock.json
├──package.json
├──public
│ ├──2017
│ | ├──05
│ | ├──06
│ | ├──07
│ | ├──08
│ | └──09
│ ├──404.html
│ ├──about
│ | └──index.html
│ ├──archives
│ | ├──2017
│ | ├──index.html
│ | └──page
│ ├──baidusitemap.xml
│ ├──categories
│ | ├──Cache
│ | ├──Git
│ | ├──Hexo
│ | ├──index.html
│ | ├──Java
│ | ├──Java8
│ | ├──Javascript
│ | ├──Linux
│ | ├──MySQL
│ | ├──ReactJS
│ | ├──redis
│ | ├──Server
│ | ├──Spring
│ | ├──Tools
│ | ├──思考
│ | └──读书
│ ├──CNAME
│ ├──css
│ | └──main.css
│ ├──gallery
│ | └──index.html
│ ├──images
│ | ├──algolia_logo.svg
│ | ├──alipay.jpg
│ | ├──avatar.gif
│ | ├──avatar.jpeg
│ | ├──bk.bmp
│ | ├──bk.jpg
│ | ├──bk.png
│ | ├──bk2.jpg
│ | ├──cc-by-nc-nd.svg
│ | ├──cc-by-nc-sa.svg
│ | ├──cc-by-nc.svg
│ | ├──cc-by-nd.svg
│ | ├──cc-by-sa.svg
│ | ├──cc-by.svg
│ | ├──cc-zero.svg
│ | ├──loading.gif
│ | ├──placeholder.gif
│ | ├──quote-l.svg
│ | ├──quote-r.svg
│ | ├──searchicon.png
│ | └──wechat.jpg
│ ├──index.html
│ ├──js
│ | └──src
│ ├──lib
│ | ├──algolia-instant-search
│ | ├──canvas-nest
│ | ├──canvas-ribbon
│ | ├──fancybox
│ | ├──fastclick
│ | ├──font-awesome
│ | ├──Han
│ | ├──jquery
│ | ├──jquery_lazyload
│ | ├──pace
│ | ├──three
│ | ├──ua-parser-js
│ | └──velocity
│ ├──links
│ | └──index.html
│ ├──page
│ | ├──2
│ | └──3
│ ├──search.xml
│ ├──sitemap.xml
│ └──tags
│ ├──ArrayList
│ ├──banner
│ ├──Dropwizard
│ ├──EhCache
│ ├──Feign
│ ├──Git
│ ├──Hexo
│ ├──index.html
│ ├──Java
│ ├──Java8
│ ├──Javascript
│ ├──Lambda
│ ├──Linux
│ ├──Mac
│ ├──MySQL
│ ├──NodeJS
│ ├──ReactJS
│ ├──reading
│ ├──redis
│ ├──Server
│ ├──Spring
│ ├──SpringMVC
│ ├──team
│ ├──UTF-8
│ ├──vim
│ ├──Webpack
│ ├──Windows
│ └──码云
├──README.md
├──scaffolds
│ ├──draft.md
│ ├──page.md
│ └──post.md
├──source
│ ├──404.html
│ ├──_data
│ | └──links.yml
│ ├──_posts
│ | ├──banner-ascii-2-txt.md
│ | ├──dropwizard-feign.md
│ | ├──Ehcache3入门-Spring集成.md
│ | ├──git-rebase.md
│ | ├──hello-react-js.md
│ | ├──hello-world.md
│ | ├──hexo-github-oschina.md
│ | ├──hexo-next-hypercomments.md
│ | ├──hexo-next-shang.md
│ | ├──http-server-static.md
│ | ├──Java-ArrayList-remove.md
│ | ├──java-utf8-iso-乱码根源.md
│ | ├──java8-in-action-2.md
│ | ├──java8-lambda.md
│ | ├──js-cros.md
│ | ├──mac-install-mysql.md
│ | ├──mac-install-redis.md
│ | ├──react-tutorial-1.md
│ | ├──reading-schedule.md
│ | ├──spring400.md
│ | ├──switch-to-oschina.md
│ | ├──team-first-chance.md
│ | ├──tree.md
│ | ├──vim.md
│ | └──why-string-is-immutable.md
│ ├──about
│ | └──index.md
│ ├──categories
│ | └──index.md
│ ├──CNAME
│ ├──gallery
│ | └──index.md
│ ├──images
│ | ├──alipay.jpg
│ | ├──avatar.jpeg
│ | ├──bk.bmp
│ | ├──bk.jpg
│ | ├──bk.png
│ | ├──bk2.jpg
│ | └──wechat.jpg
│ ├──links
│ | └──index.md
│ └──tags
│ └──index.md
├──themes
│ ├──landscape
│ | ├──_config.yml
│ | ├──Gruntfile.js
│ | ├──languages
│ | ├──layout
│ | ├──LICENSE
│ | ├──package.json
│ | ├──README.md
│ | ├──scripts
│ | └──source
│ └──next
│ ├──.bowerrc
│ ├──.editorconfig
│ ├──.hound.yml
│ ├──.javascript_ignore
│ ├──.jshintrc
│ ├──.stylintrc
│ ├──.travis.yml
│ ├──_config.yml
│ ├──bower.json
│ ├──gulpfile.coffee
│ ├──languages
│ ├──layout
│ ├──LICENSE
│ ├──package.json
│ ├──README.cn.md
│ ├──README.md
│ ├──scripts
│ ├──source
│ └──test
└──thems-bak
│ └──next
│ ├──_config.yml
│ └──custom.styl
参考
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Salesforce Javascript(一) Promise 浅谈
- Python 技巧篇-英文单词首字母大小写转换功能实例演示,字符串切片实现
- 5千字的SpringMVC总结,我觉得你会需要
- 【原创】Java并发编程系列36 | FutureTask
- React Hooks-useTypescript!
- spring-boot项目优雅的http客户端工具,真香!
- 人人都可以学会生存分析(学徒数据挖掘)
- 谈谈const跟Object.freeze()
- Java String类源码阅读笔记
- 别再用JSON配置文件了
- 什么,你一定要基于FPKM标准化表达矩阵做单细胞差异分析
- Tomcat 9最新版安装与使用手册,tomcat更改端口号,tomcat控制台乱码问题解决方法
- Python 技术篇-读取遍历指定路径的文件,区分文件和文件夹
- MySQL蜜罐获取攻击者微信ID
- PyQt5 技巧篇-增加一个类级变量,类级变量的设置方法,类级"常量"设置方法