簡體   English   中英

CentOS 目錄結構為樹?

[英]CentOS directory structure as tree?

CentOS 上是否有與 tree 相同的東西?

如果你的 Centos 系統上沒有安裝 tree(我通常建議服務器設置使用最少的安裝磁盤)你應該在你的命令行中輸入以下內容:

# yum install tree -y

如果這沒有安裝,那是因為您沒有正確的存儲庫。 我會使用 Dag Wieers 存儲庫:

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

之后,您可以進行安裝:

# yum install tree -y

現在你准備好開始了。 始終閱讀手冊頁: http : //linux.die.net/man/1/tree

所以很簡單,以下將返回一棵樹:

# tree

或者,您可以將其輸出到文本文件。 還有很多選項。如果您正在尋找除默認輸出之外的其他內容,請再次閱讀您的手冊頁。

# tree > recursive_directory_list.txt

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

您可以制作自己的原始“樹”(為了好玩:))

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

正如你在這里看到的。 默認情況下,CentOs 中未安裝 tree,因此您需要查找 RPM 並手動安裝

由於在 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]$ 

你在基礎倉庫中有樹。

顯示它(yum list package-name):

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

安裝它:

yum install tree

(已在 CentOS 5 和 6 上驗證)

我需要在不允許我進行yum安裝的遠程計算機上工作。 所以我修改了 bash-o-logist 的答案以獲得更靈活的答案。

它需要一個(可選)參數,它是您要顯示的子目錄的最大級別。 將它添加到您的$PATH ,並享受不需要安裝的tree命令。

我不是 shell 專家(為了這個非常短的腳本,我不得不在谷歌上搜索了很多次)。 所以如果我做錯了什么,請告訴我。 非常感謝!

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