[英]Linux command to print a tree like directory structure
I'm trying to write a script which will print directories, subdirectories and their files in a tree structure, but without using tree command. 我正在尝试编写一个脚本,该脚本将以树形结构打印目录,子目录及其文件,但不使用tree命令。
Example: 例:
DIR: A
f1
f2
DIR: B
f3
DIR: C
file1
file21
I have tried multiple of solutions, but I always ran into the problem that I could not distinguish between a directory or a file, therefore I could not apply the right formatting. 我尝试了多种解决方案,但始终遇到无法区分目录或文件的问题,因此无法应用正确的格式。 The "DIR: " prefix makes it complicated.
“ DIR:”前缀使它变得复杂。 Is there something very obvious that I'm missing?
是否有我很想念的东西?
Use the the -d
test: 使用
-d
测试:
if [ -d "$path" ] ; then
echo DIR
fi
Cited from this SO question 引自这个SO问题
Is this what your looking for tree , should be in most distributions (maybe as an optional install)? 这是您在大多数发行版中寻找的tree ,(可能是可选安装)?
~> 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 maintainers web page. 样本来自维护者网页。
You can add the option -L #
where #
is replaced by a number, to specify the max recursivity level. 您可以添加选项
-L #
,其中#
用数字代替,以指定最大递归级别。
Remove -d
to display also files. 删除
-d
也显示文件。
您也可以尝试这些命令的变体
find . -type d -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'
find . -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'
find . | sed -e 's;[^/]*/;|____;g;s;____|; |;g'
find -type d
tree -l
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.