是否有任何linux命令,我可以从Bash脚本调用,将以树的形式打印目录结构,例如,
folder1
a.txt
b.txt
folder2
folder3
是否有任何linux命令,我可以从Bash脚本调用,将以树的形式打印目录结构,例如,
folder1
a.txt
b.txt
folder2
folder3
当前回答
将已有的答案组合并扩展为t壳函数
t() {
find -E "${1:-.}" -maxdepth "${2:-3}" \
-not -regex ".*\/((.idea|.git|.venv|node_modules|venv)\/.*|.DS_Store)" \
| sort | sed \
-e "s/[^-][^\/]*\// ├ /g" \
-e "s/├ \//├ /g" \
-e "s/├ ├/│ ├/g" \
-e "s/├ ├/│ ├/g" \
-e "s/├ │/│ │/g" \
-e '$s/├/└/'
}
适用于Mac:
$ t
.
├ src
│ ├ .idea
│ ├ plugins
│ │ ├ .flake8
│ │ ├ .git
│ │ ├ .github
│ │ ├ .gitignore
│ │ ├ .pre-commit-config.yaml
│ │ ├ .python-version
│ │ ├ Makefile
│ │ ├ README.md
│ │ ├ buildspecs
│ │ ├ cicd
│ │ ├ cicd.py
│ │ ├ docker
│ │ ├ packages
│ │ ├ plugin_template
│ │ ├ plugins
│ │ ├ scripts
│ │ └ venv
$ t . 2
.
├ src
│ ├ .idea
│ └ plugins
$ t src/plugins/ | more
│ ├
│ ├ .flake8
│ ├ .git
│ ├ .github
│ │ ├ pull_request_template.md
│ ├ .gitignore
│ ├ .pre-commit-config.yaml
│ ├ .python-version
│ ├ Makefile
│ ├ README.md
│ ├ buildspecs
│ │ ├ test-and-deploy.yml
│ ├ cicd
:
|更可以放在函数的末尾,方便使用。
其他回答
在bashrc中添加下面的函数可以让您在不带任何参数的情况下运行该命令,该命令显示当前目录结构,当以任何路径作为参数运行时,将显示该路径的目录结构。这样可以避免在运行命令之前切换到特定目录。
function tree() {
find ${1:-.} | sed -e "s/[^-][^\/]*\// |/g" -e "s/|\([^ ]\)/|-\1/"
}
这在gitbash中也适用。
来源:@javasheriff的评论
将已有的答案组合并扩展为t壳函数
t() {
find -E "${1:-.}" -maxdepth "${2:-3}" \
-not -regex ".*\/((.idea|.git|.venv|node_modules|venv)\/.*|.DS_Store)" \
| sort | sed \
-e "s/[^-][^\/]*\// ├ /g" \
-e "s/├ \//├ /g" \
-e "s/├ ├/│ ├/g" \
-e "s/├ ├/│ ├/g" \
-e "s/├ │/│ │/g" \
-e '$s/├/└/'
}
适用于Mac:
$ t
.
├ src
│ ├ .idea
│ ├ plugins
│ │ ├ .flake8
│ │ ├ .git
│ │ ├ .github
│ │ ├ .gitignore
│ │ ├ .pre-commit-config.yaml
│ │ ├ .python-version
│ │ ├ Makefile
│ │ ├ README.md
│ │ ├ buildspecs
│ │ ├ cicd
│ │ ├ cicd.py
│ │ ├ docker
│ │ ├ packages
│ │ ├ plugin_template
│ │ ├ plugins
│ │ ├ scripts
│ │ └ venv
$ t . 2
.
├ src
│ ├ .idea
│ └ plugins
$ t src/plugins/ | more
│ ├
│ ├ .flake8
│ ├ .git
│ ├ .github
│ │ ├ pull_request_template.md
│ ├ .gitignore
│ ├ .pre-commit-config.yaml
│ ├ .python-version
│ ├ Makefile
│ ├ README.md
│ ├ buildspecs
│ │ ├ test-and-deploy.yml
│ ├ cicd
:
|更可以放在函数的末尾,方便使用。
还可以使用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;
}}'
这个命令可以同时显示文件夹和文件。
find . | sed -e "s/[^-][^\/]*\// |/g" -e "s/|\([^ ]\)/|-\1/"
示例输出:
.
|-trace.pcap
|-parent
| |-chdir1
| | |-file1.txt
| |-chdir2
| | |-file2.txt
| | |-file3.sh
|-tmp
| |-json-c-0.11-4.el7_0.x86_64.rpm
来源:@javasheriff的评论。它被淹没在评论中,并将其作为答案发布,可以帮助用户轻松发现它。
这就是你要找的树吗?它应该在大多数发行版中(可能作为可选安装)。
~> 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
样本取自维护者的网页。
您可以添加选项- l#,其中#被一个数字替换,以指定最大递归深度。
删除-d以显示also文件。