Linux命令以树的形式打印目录结构

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3455625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 20:21:04  来源:igfitidea点击:

Linux command to print directory structure in the form of a tree

linuxcommand-line

提问by user243655

Is there any linux command that I can call from a Bash script that will print the directory structure in the form of a tree, e.g.,

是否有任何 linux 命令可以从 Bash 脚本中调用,该脚本将以树的形式打印目录结构,例如,

folder1
   a.txt
   b.txt
folder2
   folder3

采纳答案by crafty

Is this what you're looking for tree? It should be in most distributions (maybe as an optional install).

这就是你要找的吗?它应该在大多数发行版中(可能作为可选安装)。

~> tree -d /proc/self/
/proc/self/
|-- attr
|-- cwd -> /proc
|-- fd
|   `-- 3 -> /proc/15589/fd
|-- fdinfo
|-- net
|   |-- dev_snmp6
|   |-- netfilter
|   |-- rpc
|   |   |-- auth.rpcsec.context
|   |   |-- auth.rpcsec.init
|   |   |-- auth.unix.gid
|   |   |-- auth.unix.ip
|   |   |-- nfs4.idtoname
|   |   |-- nfs4.nametoid
|   |   |-- nfsd.export
|   |   `-- nfsd.fh
|   `-- stat
|-- root -> /
`-- task
    `-- 15589
        |-- attr
        |-- cwd -> /proc
        |-- fd
        | `-- 3 -> /proc/15589/task/15589/fd
        |-- fdinfo
        `-- root -> /

27 directories

sample taken from maintainer's web page.

来自维护者网页的样本。

You can add the option -L #where #is replaced by a number, to specify the max recursion depth.

您可以添加选项-L #,其中#由数字替换,以指定最大递归深度。

Remove -dto display also files.

删除-d以显示文件。

回答by Soufiane Hassou

You can use this one:

你可以使用这个:

ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/   /' -e 's/-/|/'

It will show a graphical representation of the current sub-directories without files in a few seconds, e.g. in /var/cache/:

它将在几秒钟内显示没有文件的当前子目录的图形表示,例如/var/cache/

   .
   |-apache2
   |---mod_cache_disk
   |-apparmor
   |-apt
   |---archives
   |-----partial
   |-apt-xapian-index
   |---index.1
   |-dbconfig-common
   |---backups
   |-debconf

Source

来源

回答by RussellStewart

To add Hassou's solution to your .bashrc, try:

要将 Hassou 的解决方案添加到您的 .bashrc,请尝试:

alias lst='ls -R | grep ":$" | sed -e '"'"'s/:$//'"'"' -e '"'"'s/[^-][^\/]*\//--/g'"'"' -e '"'"'s/^/   /'"'"' -e '"'"'s/-/|/'"'"

回答by Robert

I'm prettifying the output of @Hassou's answer with:

我正在美化@Hassou 的答案的输出:

ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//──/g' -e 's/─/├/' -e '$s/├/└/'

This is much like the output of treenow:

这很像tree现在的输出:

.
├─pkcs11
├─pki
├───ca-trust
├─────extracted
├───────java
├───────openssl
├───────pem
├─────source
├───────anchors
├─profile.d
└─ssh

You can also make an alias of it:

您还可以为其创建别名:

alias ltree=$'ls -R | grep ":$" | sed -e \'s/:$//\' -e \'s/[^-][^\/]*\//──/g\' -e \'s/─/├/\' -e \'$s/├/└/\''

BTW, treeis not available in some environment, like MinGW. So the alternate is helpful.

顺便说一句,tree在某些环境中不可用,例如 MinGW。所以替代是有帮助的。

回答by ylspirit

You can also use the combination of find and awk commands to print the directory tree. For details, please refer to "How to print a multilevel tree directory structure using the linux find and awk combined commands"

您还可以使用 find 和 awk 命令的组合来打印目录树。详情请参考《如何使用linux find和awk组合命令打印多级树目录结构

find . -type d | awk -F'/' '{ 
depth=3;
offset=2;
str="|  ";
path="";
if(NF >= 2 && NF < depth + offset) {
    while(offset < NF) {
        path = path "|  ";
        offset ++;
    }
    print path "|-- "$NF;
}}'

回答by Pavan Kumar

This command works to display both folders and files.

此命令可同时显示文件夹和文件

find . | sed -e "s/[^-][^\/]*\// |/g" -e "s/|\([^ ]\)/|-/"

Example output:

示例输出:

.
 |-trace.pcap
 |-parent
 | |-chdir1
 | | |-file1.txt
 | |-chdir2
 | | |-file2.txt
 | | |-file3.sh
 |-tmp
 | |-json-c-0.11-4.el7_0.x86_64.rpm

Source:Comment from @javasheriff here. Its submerged as a comment and posting it as answer helps users spot it easily.

来源:来自@javasheriff 的评论在这里。它作为评论被淹没并作为答案发布有助于用户轻松发现它。