简体   繁体   English

CentOS 目录结构为树?

[英]CentOS directory structure as tree?

CentOS 上是否有与 tree 相同的东西?

If tree is not installed on your Centos system (I typically recommend server setups to use minimal install disk anyhow) you should type the following at your command line:如果你的 Centos 系统上没有安装 tree(我通常建议服务器设置使用最少的安装磁盘)你应该在你的命令行中输入以下内容:

# yum install tree -y

If this doesn't install it's because you don't have the proper repository.如果这没有安装,那是因为您没有正确的存储库。 I would use the Dag Wieers repository:我会使用 Dag Wieers 存储库:

http://dag.wieers.com/rpm/FAQ.php#B http://dag.wieers.com/rpm/FAQ.php#B

After that you can do your install:之后,您可以进行安装:

# yum install tree -y

Now you're ready to roll.现在你准备好开始了。 Always read the man page: http://linux.die.net/man/1/tree始终阅读手册页: http : //linux.die.net/man/1/tree

So quite simply the following will return a tree:所以很简单,以下将返回一棵树:

# tree

Alternatively you can output this to a text file.或者,您可以将其输出到文本文件。 There's a ton of options too.. Again, read your man page if you're looking for something other than default output.还有很多选项。如果您正在寻找除默认输出之外的其他内容,请再次阅读您的手册页。

# tree > recursive_directory_list.txt

(^^ in a text file for later review ^^) (^^ 在文本文件中供以后查看 ^^)

You can make your own primitive "tree" ( for fun :) )您可以制作自己的原始“树”(为了好玩:))

#!/bin/bash
# only if you have bash 4 in your CentOS system
shopt -s globstar
for file in **/*
do
    slash=${file//[^\/]}
    case "${#slash}" in
        0) echo "|-- ${file}";;
        1) echo "|   |--  ${file}";;
        2) echo "|   |   |--  ${file}";;
    esac
done

As you can see here .正如你在这里看到的。 tree is not installed by default in CentOs, so you'll need to look for an RPM and install it manually默认情况下,CentOs 中未安装 tree,因此您需要查找 RPM 并手动安装

Since tree is not installed by default in CentOS ...由于在 CentOS 中默认未安装tree ...

[user@CentOS test]$ tree
-bash: tree: command not found
[user@CentOS test]$ 

You can also use the following ls command to produce almost similar output with tree您还可以使用以下ls命令生成与tree几乎相似的输出

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

Example:例子:

[user@CentOS test]$ ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/   /' -e 's/-/|/'
   .
   |-directory1
   |-directory2
   |-directory3
[user@CentOS directory]$ 

You have tree in the base repo.你在基础仓库中有树。

Show it (yum list package-name):显示它(yum list package-name):

# yum list tree
Available Packages
tree.i386               1.5.0-4               base

Install it:安装它:

yum install tree

(verified on CentOS 5 and 6) (已在 CentOS 5 和 6 上验证)

I need to work on a remote computer that won't allow me to yum install.我需要在不允许我进行yum安装的远程计算机上工作。 So I modified bash-o-logist's answer to get a more flexible one.所以我修改了 bash-o-logist 的答案以获得更灵活的答案。

It takes an (optional) argument that is the maximum level of subdirectories you want to show.它需要一个(可选)参数,它是您要显示的子目录的最大级别。 Add it to your $PATH , and enjoy a tree command that doesn't need installation.将它添加到您的$PATH ,并享受不需要安装的tree命令。

I am not an expert in shell (I had to Google a ton of times just for this very short script).我不是 shell 专家(为了这个非常短的脚本,我不得不在谷歌上搜索了很多次)。 So if I did anything wrong, please let me know.所以如果我做错了什么,请告诉我。 Thank you so much!非常感谢!

#!/bin/bash
# only if you have bash 4 in your CentOS system

shopt -s globstar # enable double star

max_level=${1:-10}

for file in **
do
    # Get just the folder or filename
    IFS='/'
    read -ra ADDR <<< "$file"
    last_field=${ADDR[-1]}
    IFS=' '

    # Get the number of slashes
    slash=${file//[^\/]}
    
    # print folder or file with correct number of leadings
    if [ ${#slash} -lt $max_level ]
    then
        spaces="   "
        leading=""
        if [ "${#slash}" -gt 0 ]
        then
            leading=`eval $(echo printf '"|${spaces}%0.s"' {1..${#slash}})`
        fi
        echo "${leading}|-- $last_field"
    fi
done 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM