---
title: "我使用过的Linux命令之tree - 以树状图列出目录的内容"
author: "codingstandards"
date: "2010-12-03T21:04:50Z"
tags: ["C", "C#", "C++", "GCC", "Linux"]
categories: ["操作系统"]
slug: "linux-tree"
draft: false
lastmod: "2010-12-03T21:06:19Z"
id: 833035
url: "https://codingstandards.iteye.com/blog/833035"
---

# 我使用过的Linux命令之tree - 以树状图列出目录的内容

本文链接：[http://codingstandards.iteye.com/blog/833035](/blog/833035)   （转载请注明出处）

## 用途说明

tree命令以树状图列出目录的内容（list contents of directories in a tree-like format.）。通常在写文档时需要列一下文件目录结构，这个时候tree命令就能帮个忙了。某些Linux上没有tree命令，本文后面提供了安装方法。

## 常用参数

tree -d 只显示目录。

tree -L 1 只显示第一层目录。

 

## 使用示例

### 示例一

\[root@qzt196 ~]# cd /  
\[root@qzt196 /]# tree -L 1  
.  
|-- bin  
|-- boot  
|-- dev  
|-- etc  
|-- home  
|-- lib  
|-- lost+found  
|-- media  
|-- misc  
|-- mnt  
|-- net  
|-- opt  
|-- proc  
|-- root  
|-- sbin  
|-- selinux  
|-- software  
|-- srv  
|-- sys  
|-- tmp  
|-- usr  
|-- var  
\`-- vmsnfs

23 directories, 0 files  
\[root@qzt196 /]#

### 示例二 安装tree命令

\[root@web186 setup]# tree  
\-bash: tree: command not found

**到ftp://mama.indstate.edu/linux/tree/下载最新的tree命令源代码压缩包。**  
\[root@web186 setup]# ls -l tree-1.5.3.tgz  
\-rw-r--r--    1 root     root        34494 12月  3 20:56 tree-1.5.3.tgz  
\[root@web186 setup]# tar zxf tree-1.5.3.tgz  
\[root@web186 setup]# cd tree-1.5.3  
\[root@web186 tree-1.5.3]# ls  
CHANGES  INSTALL  LICENSE  Makefile  man  README  strverscmp.c  tree.c  
\[root@web186 tree-1.5.3]# make  
gcc -ggdb -Wall -DLINUX -D\_LARGEFILE64\_SOURCE -D\_FILE\_OFFSET\_BITS=64   -c -o tree.o tree.c  
gcc  -o tree tree.o  
\[root@web186 tree-1.5.3]# tree  
\-bash: tree: command not found  
\[root@web186 tree-1.5.3]# cp -af tree /usr/bin  
\[root@web186 tree-1.5.3]# tree  
.  
|-- CHANGES  
|-- INSTALL  
|-- LICENSE  
|-- Makefile  
|-- man  
|   |-- tree.1  
|   \`-- tree.1.fr  
|-- README  
|-- strverscmp.c  
|-- tree  
|-- tree.c  
\`-- tree.o

1 directory, 11 files  
\[root@web186 tree-1.5.3]#

 

## 问题思考

## 相关资料

【1】天下维客   [Linux命令:Tree](http://www.allwiki.com/wiki/Linux%E5%91%BD%E4%BB%A4:Tree)     
【2】CalmArrow   [Linux下列目录树的命令tree](http://blog.chinaunix.net/u/21948/showart_297101.html)       
【3】tree命令的源代码   ftp://mama.indstate.edu/linux/tree/  
【4】Unix Tree / Linux Tree    [One-Line Shell Script](http://www.centerkey.com/tree/)     
【5】Linux - Tree structure of files  [File hierarchy under Linux](http://en.kioskea.net/contents/linux/linarb.php3)

 

返回 [我使用过的Linux命令系列总目录](/blog/786653)