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

简介: Linux & Mac1.下载tree lib//macbrew install tree//centosyum install tree//ubuntuapt-get install tree用法//显示所有文件tree//显示深度2层tree -L 22.

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

参考

mac 下的 tree 命令 终端展示你的目录树结构





唯有不断学习方能改变! -- Ryan Miao
目录
相关文章
|
5月前
for循环 批处理 查找文件 详细信息 文件名
for循环 批处理 查找文件 详细信息 文件名
39 0
|
2月前
|
Shell
grep 搜索当前文件夹下的所有子文件中的文件是否包含8888字符串的命令
要在当前文件夹及其所有子文件夹中的文件中搜索包含字符串 "8888" 的文件,你可以使用 grep 命令结合 -r 或 -R 选项(表示递归搜索)。这里是具体的命令: bash grep -r "8888" . 其中: -r 或 -R:递归搜索。 "8888":你要搜索的字符串。 .:表示当前目录。 这个命令会列出所有包含字符串 "8888" 的文件的名称以及匹配的行。如果你只想看到文件名,而不看具体的匹配行,可以加上 -l 选项: bash grep -rl "8888" . 这样,命令只会输出包含字符串 "8888" 的文件名。
|
Shell
利用 shell 脚本配合 find 命令展示目录结构树
利用 shell 脚本配合 find 命令展示目录结构树
156 0
利用 shell 脚本配合 find 命令展示目录结构树
|
前端开发 Shell Perl
目录内批量查找替换,我用 tree 替代 find。
最近有需求,需要在编译好的前端文件里,查找替换特定字段。 通常都会想到用 find 找出文件再使用管道输出给 sed。但实际验证下来,包括去 StackOverFlow 上查找,解决方案都不理想。 主要的痛点是:find 对需要忽略的目录路径,处理很麻烦,效果还差异很大。
1259 0
|
Perl
tree命令
tree命令,是大小写敏感的。常用的是: tree -C 颜色显示 tree -f 显示文件全路径 tree -L 2 只显示2层 tree -P *.pl 只显示文件目录和*.pl的perl文件。
1279 0