整合營銷服務商

          電腦端+手機端+微信端=數(shù)據同步管理

          免費咨詢熱線:

          tree命令的安裝與使用

          inux tree命令的安裝與使用

          在Linux系統(tǒng)中,tree命令是一個非常實用的工具,它能夠以樹狀圖的形式列出目錄及其子目錄的內容,使得目錄結構一目了然。然而,并非所有的Linux發(fā)行版都默認安裝了tree命令。本文將介紹如何在沒有預裝tree命令的Linux系統(tǒng)上安裝它,并詳細闡述其使用方法。

          安裝tree命令

          對于基于Debian(如Ubuntu)的系統(tǒng)

          在基于Debian的Linux發(fā)行版上,你可以使用apt包管理器來安裝tree。打開終端并輸入以下命令:

          sudo apt update
          sudo apt install tree

          首先,apt update命令會更新軟件包列表,確保你安裝的是最新版本的軟件包。然后,apt install tree命令會安裝tree軟件包。

          對于基于RPM(如CentOS、Fedora)的系統(tǒng)

          在基于RPM的Linux發(fā)行版上,你可以使用yum(在較舊的CentOS版本中)或dnf(在Fedora和較新版本的CentOS中)來安裝tree

          對于使用yum的系統(tǒng):

          sudo yum install tree

          對于使用dnf的系統(tǒng):

          sudo dnf install tree

          對于Arch Linux

          在Arch Linux上,你可以使用pacman包管理器來安裝tree

          sudo pacman -S tree

          通用方法(從源代碼編譯)

          如果上述方法都不適用,或者你傾向于從源代碼編譯安裝,你可以訪問tree的官方網站或其他可靠的源代碼倉庫下載最新的源代碼包。解壓后,按照README文件中的說明進行編譯和安裝。不過,這種方法相對復雜,且通常不是必要的,除非你需要安裝特定版本的tree或有其他特殊需求。

          使用tree命令

          安裝完tree命令后,你就可以開始使用它了。以下是一些基本用法示例:

          列出當前目錄的樹狀結構

          只需在終端中輸入tree(不帶任何參數(shù)),它將列出當前目錄及其所有子目錄和文件的樹狀結構。

          tree

          限制目錄深度

          如果你只想查看前幾層的目錄結構,可以使用-L選項來限制目錄的深度。例如,要查看當前目錄及其直接子目錄的結構,可以使用:

          tree -L 2

          只顯示目錄

          如果你只對目錄結構感興趣,而不關心文件,可以使用-d選項來只顯示目錄。

          tree -d

          帶有文件大小的樹狀圖

          使用-h選項(人類可讀的格式),tree將顯示每個文件和目錄的大小。

          tree -h

          忽略特定文件或目錄

          有時,你可能想忽略某些特定的文件或目錄。tree命令提供了-I選項來實現(xiàn)這一點。例如,要忽略所有.txt文件和名為temp的目錄,可以這樣做:

          tree -I '*.txt|temp'

          注意:在某些shell中,你可能需要使用引號或轉義字符來正確傳遞模式。

          更多選項

          tree命令還有許多其他選項,可以通過閱讀其手冊頁(man tree)來了解更多信息。手冊頁詳細描述了每個選項的作用和用法,是學習和掌握tree命令的好資源。

          --help         Outputs a verbose usage listing.
          --version      Outputs the version of tree.
          -a             All files are printed. By default, tree does not print hidden files (those beginning with a dot `.'). In no event does tree print the file system constructs `.' (current directory) and `..' (previous directory).
          -d             List directories only.
          -f             Prints the full path prefix for each file.
          -i             Tree does not print the indentation lines. Useful when used in conjunction with the -f option.
          -l             Follows symbolic links to directories as if they were directories. Links that would result in a recursive loop are avoided.
          -x             Stay on the current file system only, as with find -xdev.
           
          -P pattern     List only those files that match the wildcard pattern. Note: you must use the -a option to also consider those files beginning with a dot `.' for matching. Valid wildcard operators are `*' (any zero or more characters), `?' (any single character), `[...]' (any single character listed between brackets (optional - (dash) for character range may be used: ex: [A-Z]), and `[^...]' (any single character not listed in brackets) and `|' separates alternate patterns.
           
          -I pattern     Do not list those files that match the wildcard pattern.
           
          --prune           Makes tree prune empty directories from the output, useful when used in conjunction with -P or -I.
          --filelimit #     Do not descend directories that contain more than # entries.
          --timefmt format  Prints (implies -D) and formats the date according to the format string which uses the strftime syntax.
          --noreport        Omits printing of the file and directory report at the end of the tree listing.
           
          -p             Print the protections for each file (as per ls -l).
          -s             Print the size of each file with the name.
          -u             Print the username, or UID # if no username is available, of the file.
          -g             Print the group name, or GID # if no group name is available, of the file.
          -D             Print the date of the last modification time for the file listed.
          --inodes       Prints the inode number of the file or directory
          --device       Prints the device number to which the file or directory belongs
          -F             Append a `/' for directories, a `=' for socket files, a `*' for executable files and a `|' for FIFO's, as per ls -F
          -q             Print non-printable characters in file names as question marks instead of the default carrot notation.
          -N             Print non-printable characters as is instead of the default carrot notation.
          -r             Sort the output in reverse alphabetic order.
          -t             Sort the output by last modification time instead of alphabetically.
          --dirsfirst    List directories before files.
          -n             Turn colorization off always, overridden by the -C option.
          -C             Turn colorization on always, using built-in color defaults if the LS_COLORS environment variable is not set. Useful to colorize output to a pipe.
          -A             Turn on ANSI line graphics hack when printing the indentation lines.
          -S             Turn on ASCII line graphics (useful when using linux console mode fonts). This option is now equivalent to `--charset=IBM437' and eventually is depreciated.
          -L level       Max display depth of the directory tree.
          -R             Recursively cross down the tree each level directories (see -L option), and at each of them execute tree again adding `-o 00Tree.html' as a new option.
          -H baseHREF    Turn on HTML output, including HTTP references. Useful for ftp sites. baseHREF gives the base ftp location when using HTML output. That is, the local directory may be `/local/ftp/pub', but it must be referenced as `ftp://host-name.organization.domain/pub' (baseHREF should be `ftp://hostname.organization.domain'). Hint: don't use ANSI lines with this option, and don't give more than one directory in the directory list. If you want to use colors via CSS stylesheet, use the -C option in addition to this option to force color output.
           
          -T title             Sets the title and H1 header string in HTML output mode.
          --charset charset    Set the character set to use when outputting HTML and for line drawing.
          --nolinks            Turns off hyperlinks in HTML output.
          -o file name         Send output to file name.

          總結

          tree命令是Linux系統(tǒng)中一個非常有用的工具,它以直觀的方式展示了目錄結構。通過本文,你應該已經學會了如何在不同的Linux發(fā)行版上安裝tree命令,并掌握了其基本的使用方法。現(xiàn)在,你可以利用tree命令來更高效地瀏覽和管理你的文件系統(tǒng)了。


          關注本頭條號,每天堅持更新原創(chuàng)干貨技術文章。

          如需學習視頻,請在微信搜索公眾號“智傳網優(yōu)”直接開始自助視頻學習

          1. 前言

          本文主要講解Linux系統(tǒng)上的tree命令的詳細使用方法。

          tree 命令是一個小型的跨平臺命令行程序,用于遞歸地以樹狀格式列出或顯示目錄的內容。它輸出每個子目錄中的目錄路徑和文件,以及子目錄和文件總數(shù)的摘要。

          tree程序可以在Unix和類Unix系統(tǒng)(如Linux)中使用,也可以在DOS、Windows和許多其他操作系統(tǒng)中使用。它為輸出操作提供了各種選項,從文件選項、排序選項到圖形選項,并支持XML、JSON和HTML格式的輸出。

          在這篇教程中,我們將通過使用案例演示如何使用tree命令遞歸地列出Linux系統(tǒng)上目錄的內容。

          Linux tree 命令詳細使用說明


          2. 在各種發(fā)行版上安裝tree命令

          幾乎所有的Linux發(fā)行版都可以使用tree命令,但是,如果默認情況下沒有安裝它,可以使用系統(tǒng)的包管理器來安裝它,如下所示。

          2.1 在RHEL/CentOS 7上安裝tree命令工具

          yum install tree

          2.2 在Fedora 22+ /RHEL/CentOS 8上安裝tree命令工具

          dnf install tree

          2.3 在Ubuntu/Debian系統(tǒng)上安裝tree命令工具

          sudo apt install tree

          2.4 在openSUSE系統(tǒng)上安裝tree命令工具

          sudo zypper in tree

          3. tree命令的日常使用案例

          安裝之后,您可以通過下面的使用案例進一步學習tree命令的用法。

          要以類似樹的格式列出目錄內容,請切換到所需的目錄并運行tree命令,不帶任何選項或參數(shù),如下所示。某些目錄需要root權限,請使用sudo調用root權限,獲取訪問權。

          tree

          tree命令默認輸出


          sudo tree

          它將遞歸地顯示工作目錄的內容,顯示子目錄和文件,以及子目錄和文件總數(shù)的摘要。您可以使用-a標志顯示隱藏文件。

          sudo tree -a

          要使用-f列出每個子目錄和文件的完整路徑內容,如下所示。

          sudo tree -f

          列出每個子目錄和文件的完整路徑內容

          您還可以使用-d選項指定tree只打印子目錄不顯示里面的文件。如果與-f選項一起使用,tree將打印完整的目錄路徑,如下所示。

          sudo tree -d 

          指定tree只打印子目錄不顯示里面的文件

          sudo tree -df

          可以使用-L選項指定目錄樹的最大顯示深度。例如,如果您希望深度為2,則運行以下命令。

          sudo tree -f -L 2

          下面是將目錄樹的最大顯示深度設置為3的使用案例:

          sudo tree -f -L 3

          若要僅顯示與通配符內容匹配的文件,請使用-P選擇并指定您的匹配內容。在本例中,該命令將只列出與cata*匹配的文件,例如Catalina.sh, catalina.bat等將被列出。

          sudo tree -f -P cata*

          使用tree命令以樹狀的形式顯示目錄內容


          還可以通過添加--prune選項告訴tree從輸出內容中刪除空目錄,如下所示。

          sudo tree -f --prune

          tree還支持一些有用的文件選項,如-p,它以類似ls -l命令的方式打印每個文件的文件類型和權限。

          sudo tree -f -p 

          此外,要打印每個文件的用戶名(如果沒有用戶名,則為UID),使用-u選項,而-g選項打印組名(如果沒有組名,則為GID)。您可以組合-p、-u和-g選項來執(zhí)行類似于ls -l命令的輸出結果,顯示文件和目錄的詳細信息。

          sudo tree -f -pug

          還可以使用-s選項打印每個文件的字節(jié)大小以及文件名。為了以更易于閱讀的格式打印每個文件的大小,使用-h選項并指定大小字母表示千字節(jié)(K)、兆字節(jié)(M)、千兆字節(jié)(G)、tb (T)等。

          sudo tree -f -s

          或者

          sudo tree -f -h

          要顯示每個子目錄或文件的最后修改時間的日期,請使用-D選項,如下所示。

          sudo tree -f -pug -h -D

          使用tree命令層次化顯示目錄內容


          另一個有用的選項是--du,它將顯示指定目錄所占用的磁盤空間。

          sudo tree -f --du

          您還可以使用-o選項將tree的輸出內容發(fā)送或重定向到文件名,以便稍后進行分析。

          sudo tree -o direc_tree.txt

          4. 總結

          以上就是tree命令的全部內容,運行tree了解更多用法和選項。如果您有任何問題或想法要分享,請使用下面的反饋表格聯(lián)系我們。

          本文已同步至博客站,尊重原創(chuàng),轉載時請在正文中附帶以下鏈接:https://www.linuxrumen.com/cyml/1783.html

          xml是基于 libxml2解析庫的Python封裝。libxml2是使用C語言編寫的,解析速度很好,不過安裝起來稍微有點復雜。安裝說明可以參考(http: //Lxml.de/installation.html),在CentOS7上中文安裝說明(http://www.cjavapy.com/article/64/),使用lxml庫來解析網絡爬蟲抓取到的HTML是一種非常高效的方式。lxml的html模塊特別適合處理HTML內容,它可以快速解析大型HTML文件,并提供XPath和CSS選擇器來查詢和提取數(shù)據。

          參考文檔:https://www.cjavapy.com/article/65/

          一、可能不合法的html標簽解析

          從網絡上抓取到的html的內容,有可能都是標準寫法,標簽什么的都閉合,屬性也是標準寫法,但是有可能有的網站的程序員不專業(yè),這樣抓到的html解析就有可能有問題,因此,解析時先將有可能不合法的html解析為統(tǒng)一的格式。避免為后續(xù)的解析造成困擾。

          1、lxml.html

          lxml.html是專門用于解析和處理HTML文檔的模塊。它基于lxml.etree,但是為HTML文檔的特點做了優(yōu)化。lxml.html能夠處理不良形式的HTML代碼,這對于解析和爬取網頁尤其有用。

          >>> import lxml.html
          >>> broken_html = '<ul class="body"><li>header<li>item</ul>'
          >>> tree = lxml.html.fromstring(broken_html) #解析html
          >>> fixed_html = lxml.html.tostring(tree,pretty_print=True)
          >>> print fixed_html
          <ul class="body">
          <li>header</li>
          <li>item</li>
          </ul>

          2、lxml.etree

          lxml.etree是lxml庫中用于處理XML文檔的模塊。它基于非常快的XML解析庫libxml2,提供了一個類似于標準庫xml.etree.ElementTreeAPI的接口,但是在性能和功能性方面要更加強大。lxml.etree支持XPath、XSLT、和Schema驗證等高級XML特性。

          >>> import lxml.etree
          >>> broken_html = '<ul class="body"><li>header<li>item</ul>'
          >>> tree = lxml.etree.fromstring(broken_html) #解析html
          >>> fixed_html = lxml.etree.tostring(tree,pretty_print=True)
          >>> print fixed_html
          <ul class="body">
          <li>header</li>
          <li>item</li>
          </ul>

          通過以上可以看出,lxml可以正確解析兩側缺失的括號,并閉合標簽,但不會額外增加<html>和<body>標簽。

          二、處理lxml解析出來的html內容

          若在html中找到我們想要的內容,用lxml有幾種不同的方法,XPath選擇器類似Beautiful Soup的find()方法。CSS選擇器用法和jQuery中的選擇器類似。兩種選擇器都可以用來查找文檔中的元素,但它們各有特點和適用場景。XPath是一種在XML文檔中查找信息的語言。它可以用來遍歷XML文檔的元素和屬性。CSS選擇器通常用于選擇和操作HTML文檔中的元素。

          1、XPath選擇器(/單斜杠表示絕對查找,//雙斜杠表示相對查找)

          from lxml import etree
          source_html = """
                   <div>
                      <ul>
                           <li class="item-0"><a href="link1.html">first item</a></li>
                           <li class="item-1"><a href="link2.html">second item</a></li>
                           <li class="item-inactive"><a href="link3.html">third item</a></li>
                           <li class="item-1"><a href="link4.html">fourth item</a></li>
                           <li class="item-0"><a href="link5.html">fifth item</a>
                       </ul>
                   </div>
                  """
          html = etree.HTML(source_html)
          print(html)
          result = etree.tostring(html)#會對的html標簽進行補全
          print(result.decode("utf-8"))

          輸出結果:

          <Element html at 0x39e58f0>
          <html><body><div>
          <ul>
          <li class="item-0"><a href="link1.html">first item</a></li>
          <li class="item-1"><a href="link2.html">second item</a></li>
          <li class="item-inactive"><a href="link3.html">third item</a></li>
          <li class="item-1"><a href="link4.html">fourth item</a></li>
          <li class="item-0"><a href="link5.html">fifth item</a>
          </li></ul>
          </div>
          </body></html>

          1)獲取某個標簽的內容(a標簽后不需要加斜杠,否則會報錯)

          #第一種寫法

          html = etree.HTML(source_html)
          html_data = html.xpath('/html/body/div/ul/li/a')#絕對查找
          #html_data = html.xpath('//li/a')#相對查找
          print(html)
          for i in html_data:
              print(i.text)
          

          輸出結果:

          <Element html at 0x14fe6b8>
          first item
          second item
          third item
          fourth item
          fifth item

          #第二種寫法
          #在要找的標簽后面加/text(),就是獲取標簽中的文本內容,結果中直接就是文本內容了,不用在通過text屬性獲取了。

          html = etree.HTML(source_html)
          html_data = html.xpath('/html/body/div/ul/li/a/text()')#絕對查找
          #html_data = html.xpath('//li/a/text()')#相對查找
          print(html)
          for i in html_data:
              print(i)

          輸出結果:

          <Element html at 0x128e3b7>
          first item
          second item
          third item
          fourth item
          fifth item

          2)獲取a標簽下的屬性

          html = etree.HTML(source_html)
          html_data = html.xpath('//li/a/@href') #相對查找
          #html_data = html.xpath('/html/body/div/ul/li/a/@href') #絕對查找
          for i in html_data:
              print(i)
          

          輸出結果:

          link1.html
          link2.html
          link3.html
          link4.html
          link5.html

          3)查找a標簽屬性等于link2.html的內容

          html = etree.HTML(source_html)
          html_data = html.xpath('/html/body/div/ul/li/a[@href="link2.html"]/text()')絕對查找
          #html_data = html.xpath('//li/a[@href="link2.html"]/text()')#相對查找
          print(html_data)
          for i in html_data:
              print(i)
          

          輸出結果:

          ['second item']
          second item

          4)查找最后一個li標簽里的a標簽的href屬性

          html = etree.HTML(source_html)
          html_data = html.xpath('//li[last()]/a/text()')
          print(html_data)
          for i in html_data:
              print(i)
          

          輸出結果:

          ['fifth item']
          fifth item

          5)查找倒數(shù)第二個li標簽里a標簽的href屬性

          html = etree.HTML(source_html)
          html_data = html.xpath('//li[last()-1]/a/text()')
          print(html_data)
          for i in html_data:
              print(i)
          

          輸出結果:

          ['fourth item']
          fourth item

          6)查找某個標簽id屬性值等于value的標簽

          //*[@id="value"]

          7)使用chrome瀏覽器提取某個標簽的XPath

          2、CSS選擇器(基本上和jQuery選擇器用法一樣)

          選擇器

          描述

          *

          選擇所有標簽

          a

          選擇<a>標簽

          .link

          選擇所有class = 'link'的元素

          a.link

          選擇class = 'link'的<a>標簽

          a#home

          選擇id = 'home'的<a>標簽

          a > span

          選擇父元素為<a>標簽的所有<span>子標簽

          a span

          選擇<a>標簽內部的所有<span>標簽

          使用示例:

          >>> html = """<div>
          <tr id="places_area_row" class="body">
          <td>header</td>
          <td class="w2p_fw">item1</td>
          <td class="w2p_fw">item2</td>
          <td class="w2p_fw">item3</td>
          <td><tr><td class="w2p_fw">header</td>
          <td class="w2p_fw">item4</td>
          <td class="w2p_fw">item5</td>
          <td class="w2p_fw">item6</td></tr></td>
          </tr>
          </div>"""
          >>> tree = lxml.html.fromstring(html)
          >>> td = tree.cssselect('tr#places_area_row > td.w2p_fw')[0]
          >>> htmlText = td.text_content()
          >>> print htmlText
          item1

          參考文檔:https://www.cjavapy.com/article/65/

          ?


          主站蜘蛛池模板: 精品熟人妻一区二区三区四区不卡 | 国产乱码精品一区二区三区四川| 久久蜜桃精品一区二区三区| 精品少妇一区二区三区视频| a级午夜毛片免费一区二区| A国产一区二区免费入口| AV无码精品一区二区三区| 精品一区狼人国产在线| 老熟妇仑乱一区二区视頻| 久久精品一区二区| 成人国内精品久久久久一区| 婷婷亚洲综合一区二区| 亚洲影视一区二区| 中文字幕一区在线播放| 国产麻豆精品一区二区三区| 日本道免费精品一区二区| 日本强伦姧人妻一区二区| 老熟妇仑乱一区二区视頻| 国产女人乱人伦精品一区二区| 国模大尺度视频一区二区| 精品人妻少妇一区二区三区不卡| 免费无码一区二区三区蜜桃| 99精品国产一区二区三区2021| 一区二区三区福利| 亚洲一区二区三区精品视频| 中文字幕一区二区三区永久| 日韩福利视频一区| 日本视频一区在线观看免费| 中文字幕一区在线| chinese国产一区二区| 色欲综合一区二区三区| 精品人妻少妇一区二区三区不卡 | 国产SUV精品一区二区四| 日韩欧国产精品一区综合无码| 亚洲中文字幕丝袜制服一区| 99在线精品一区二区三区| 日韩一区二区久久久久久| 农村人乱弄一区二区| 亚洲一区二区三区无码中文字幕| 国产一区二区三区在线观看免费| 一区二区三区福利视频免费观看|