CentOS目录结构是树形的吗?

发布于 2024-11-02 16:08:35 字数 25 浏览 4 评论 0原文

CentOS 上有相当于树的东西吗?

Is there an equivalent to tree on CentOS?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

愿得七秒忆 2024-11-09 16:08:35

如果你的 Centos 系统上没有安装 tree(我通常建议服务器设置无论如何都使用最小安装磁盘),你应该在命令行中键入以下内容:

# yum install tree -y

如果没有安装,那是因为你没有正确的存储库。我将使用 Dag Wieers 存储库:

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

之后您就可以进行安装了:

# yum install tree -y

现在您就可以开始了。始终阅读手册页: http://linux.die.net/man/1/tree

非常简单以下将返回一棵树:

# tree

或者您可以将其输出到文本文件。还有很多选项。再次,如果您正在寻找默认输出以外的内容,请阅读您的手册页。

# tree > recursive_directory_list.txt

(^^ 在文本文件中供以后查看^^)

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:

# 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:

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

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 ^^)

撩人痒 2024-11-09 16:08:35

你可以制作自己的原始“树”(为了好玩:))

#!/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

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
甜是你 2024-11-09 16:08:35

由于 CentOS 中默认情况下未安装 tree ...

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

您还可以使用以下 ls 命令生成与 tree

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

示例几乎相似的输出:

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

Since tree is not installed by default in CentOS ...

[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 -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]$ 
你好,陌生人 2024-11-09 16:08:35

正如您在此处看到的那样。 CentOs 中默认未安装 tree,因此您需要查找 RPM 并手动安装它

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

想挽留 2024-11-09 16:08:35

您在基础存储库中有树。

显示它 (yum list package-name):

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

安装它:

yum install tree

(在 CentOS 5 和 6 上验证)

You have tree in the base repo.

Show it (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)

-黛色若梦 2024-11-09 16:08:35

我需要在不允许我进行 yum 安装的远程计算机上工作。所以我修改了 bash-o-logger 的答案以获得更灵活的答案。

它需要一个(可选)参数,该参数是您要显示的子目录的最大级别。将其添加到您的 $PATH 中,即可享受无需安装的 tree 命令。

我不是 shell 专家(为了这个非常短的脚本,我不得不用 Google 搜索很多次)。所以如果我做错了什么,请告诉我。太感谢了!

#!/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 

I need to work on a remote computer that won't allow me to yum install. So I modified bash-o-logist's answer to get a more flexible one.

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.

I am not an expert in shell (I had to Google a ton of times just for this very short script). 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 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文