日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

命令行打印文件树列表: tree

發布時間:2025/3/8 编程问答 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 命令行打印文件树列表: tree 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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 {@Testpublic 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

參考

mac 下的 tree 命令 終端展示你的目錄樹結構

總結

以上是生活随笔為你收集整理的命令行打印文件树列表: tree的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。