포스트

Linux - Tree, 콘솔에서 파일 구조 그리기

Tree 명령어를 사용하면 파일의 구조를 depth를 이용한 tree 형태로 보기 쉽게 출력할 수 있다.


Tree 설치

1
2
sudo apt update 
sudo apt install tree


Tree 명령어 사용 예시

1
2
3
4
5
# 하위 파일 구조를 출력할 root directory로 이동
cd /path/to/directory 

# 명령어 사용!
tree
  • 출력 값
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    
     .
     ├── _copyright
     ├── categories.js
     ├── commons.js
     ├── home.js
     ├── misc.js
     ├── modules
     │   ├── components
     │   │   ├── back-to-top.js
     │   │   ├── category-collapse.js
     │   │   ├── clipboard.js
     │   │   ├── img-loading.js
     │   │   ├── img-popup.js
     │   │   ├── locale-datetime.js
     │   │   ├── mode-watcher.js
     │   │   ├── search-display.js
     │   │   ├── sidebar.js
     │   │   ├── toc.js
     │   │   └── tooltip-loader.js
     │   ├── layouts
     │   │   ├── basic.js
     │   │   ├── sidebar.js
     │   │   └── topbar.js
     │   ├── layouts.js
     │   └── plugins.js
     ├── page.js
     └── post.js
        
     4 directories, 23 files
        
    



옵션 몇 가지

-L n

출력 뎁스 레벨을 최대 n까지 설정

1
$ tree -L 1
  • 출력 값
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    
    .
    ├── _copyright
    ├── categories.js
    ├── commons.js
    ├── home.js
    ├── misc.js
    ├── modules
    ├── page.js
    └── post.js
      
    2 directories, 7 files
    


1
$ tree -L 2
  • 출력 값
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    
    .
    ├── _copyright
    ├── categories.js
    ├── commons.js
    ├── home.js
    ├── misc.js
    ├── modules
    │   ├── components
    │   ├── layouts
    │   ├── layouts.js
    │   └── plugins.js
    ├── page.js
    └── post.js
      
    4 directories, 9 files
      
    


-f: 파일이름에 경로를 포함

1
$ tree -f 
  • 출력 값
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    
    .
    ├── ./_copyright
    ├── ./categories.js
    ├── ./commons.js
    ├── ./home.js
    ├── ./misc.js
    ├── ./modules
    │   ├── ./modules/components
    │   │   ├── ./modules/components/back-to-top.js
    │   │   ├── ./modules/components/category-collapse.js
    │   │   ├── ./modules/components/clipboard.js
    │   │   ├── ./modules/components/img-loading.js
    │   │   ├── ./modules/components/img-popup.js
    │   │   ├── ./modules/components/locale-datetime.js
    │   │   ├── ./modules/components/mode-watcher.js
    │   │   ├── ./modules/components/search-display.js
    │   │   ├── ./modules/components/sidebar.js
    │   │   ├── ./modules/components/toc.js
    │   │   └── ./modules/components/tooltip-loader.js
    │   ├── ./modules/layouts
    │   │   ├── ./modules/layouts/basic.js
    │   │   ├── ./modules/layouts/sidebar.js
    │   │   └── ./modules/layouts/topbar.js
    │   ├── ./modules/layouts.js
    │   └── ./modules/plugins.js
    ├── ./page.js
    └── ./post.js
      
    4 directories, 23 files
      
    


--prune: 빈 디렉터리 포함 안함

1
tree --prune


정렬 옵션

1
2
3
4
--dirsfirst   List directories before files (-U disables).
--filesfirst  List files before directories (-U disables).
--sort X      Select sort: name,version,size,mtime(수정시간),ctime(생성시간).
-r            Reverse the order of the sort.
  • 폴더 우선 출력
    1
    
    $ tree --dirsfirst -L 2
    
    • 출력 값
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      
      .
      ├── modules
      │   ├── components
      │   ├── layouts
      │   ├── layouts.js
      │   └── plugins.js
      ├── _copyright
      ├── categories.js
      ├── commons.js
      ├── home.js
      ├── misc.js
      ├── page.js
      └── post.js
          
      4 directories, 9 files
        
      
  • 파일 우선 출력
    1
    
    $ tree --filesfirst -L 2
    
    • 출력 값
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      
      .
      ├── _copyright
      ├── categories.js
      ├── commons.js
      ├── home.js
      ├── misc.js
      ├── page.js
      ├── post.js
      └── modules
          ├── layouts.js
          ├── plugins.js
          ├── components
          └── layouts
          
      4 directories, 9 files
        
      
  • 이름 내림차순 출력
    1
    
    $ tree -r -L 2
    
    • 출력 값
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      
      .
      ├── post.js
      ├── page.js
      ├── modules
      │   ├── plugins.js
      │   ├── layouts.js
      │   ├── layouts
      │   └── components
      ├── misc.js
      ├── home.js
      ├── commons.js
      ├── categories.js
      └── _copyright
          
      4 directories, 9 files
          
      


--du -h: 파일 실제 크기를 단위를 붙여 표현

1
$ tree --du -h
  • 출력 값
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    
    [ 31K]  .
    ├── [ 108]  _copyright
    ├── [ 188]  categories.js
    ├── [ 112]  commons.js
    ├── [ 213]  home.js
    ├── [ 192]  misc.js
    ├── [ 25K]  modules
    │   ├── [ 16K]  components
    │   │   ├── [ 390]  back-to-top.js
    │   │   ├── [1.1K]  category-collapse.js
    │   │   ├── [3.2K]  clipboard.js
    │   │   ├── [1.3K]  img-loading.js
    │   │   ├── [ 382]  img-popup.js
    │   │   ├── [1.4K]  locale-datetime.js
    │   │   ├── [ 519]  mode-watcher.js
    │   │   ├── [3.0K]  search-display.js
    │   │   ├── [ 558]  sidebar.js
    │   │   ├── [ 366]  toc.js
    │   │   └── [ 281]  tooltip-loader.js
    │   ├── [4.5K]  layouts
    │   │   ├── [ 181]  basic.js
    │   │   ├── [ 191]  sidebar.js
    │   │   └── [ 120]  topbar.js
    │   ├── [ 140]  layouts.js
    │   └── [ 339]  plugins.js
    ├── [ 226]  page.js
    └── [ 298]  post.js
      
      61K used in 4 directories, 23 files
      
    


--du -s: 파일 실제 크기를 byte로 표현

1
$ tree --du -s
  • 출력 값
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    
    [      31412]  .
    ├── [        108]  _copyright
    ├── [        188]  categories.js
    ├── [        112]  commons.js
    ├── [        213]  home.js
    ├── [        192]  misc.js
    ├── [      25979]  modules
    │   ├── [      16816]  components
    │   │   ├── [        390]  back-to-top.js
    │   │   ├── [       1081]  category-collapse.js
    │   │   ├── [       3247]  clipboard.js
    │   │   ├── [       1356]  img-loading.js
    │   │   ├── [        382]  img-popup.js
    │   │   ├── [       1428]  locale-datetime.js
    │   │   ├── [        519]  mode-watcher.js
    │   │   ├── [       3112]  search-display.js
    │   │   ├── [        558]  sidebar.js
    │   │   ├── [        366]  toc.js
    │   │   └── [        281]  tooltip-loader.js
    │   ├── [       4588]  layouts
    │   │   ├── [        181]  basic.js
    │   │   ├── [        191]  sidebar.js
    │   │   └── [        120]  topbar.js
    │   ├── [        140]  layouts.js
    │   └── [        339]  plugins.js
    ├── [        226]  page.js
    └── [        298]  post.js
      
           62411 bytes used in 4 directories, 23 files
      
    


--noreport: 마지막 줄에 파일, 디렉터리 정보 출력 안함

1
$ tree --noreport
  • 출력 값
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    
    .
    ├── _copyright
    ├── categories.js
    ├── commons.js
    ├── home.js
    ├── misc.js
    ├── modules
    │   ├── components
    │   │   ├── back-to-top.js
    │   │   ├── category-collapse.js
    │   │   ├── clipboard.js
    │   │   ├── img-loading.js
    │   │   ├── img-popup.js
    │   │   ├── locale-datetime.js
    │   │   ├── mode-watcher.js
    │   │   ├── search-display.js
    │   │   ├── sidebar.js
    │   │   ├── toc.js
    │   │   └── tooltip-loader.js
    │   ├── layouts
    │   │   ├── basic.js
    │   │   ├── sidebar.js
    │   │   └── topbar.js
    │   ├── layouts.js
    │   └── plugins.js
    ├── page.js
    └── post.js
      
    


--gitignore

.gitignore 설정을 적용해서 출력

1
$ tree --gitignore -L 1
  • 출력 값
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    
    .
    ├── Gemfile
    ├── LICENSE
    ├── README.md
    ├── _config.yml
    ├── _data
    ├── _includes
    ├── _javascript
    ├── _layouts
    ├── _plugins
    ├── _posts
    ├── _sass
    ├── _tabs
    ├── assets
    ├── index.html
    ├── jekyll-theme-chirpy.gemspec
    ├── package.json
    ├── rollup.config.js
    └── tools
      
    11 directories, 8 files
      
    
  • .gitignore 옵션 없이 사용했을 때
    1
    
    $ tree -L 1
    
    • 출력 값
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      
      .
      ├── Gemfile
      ├── Gemfile.lock
      ├── LICENSE
      ├── README.md
      ├── _config.yml
      ├── _data
      ├── _includes
      ├── _javascript
      ├── _layouts
      ├── _plugins
      ├── _posts
      ├── _sass
      ├── _site
      ├── _tabs
      ├── assets
      ├── index.html
      ├── jekyll-theme-chirpy.gemspec
      ├── package.json
      ├── rollup.config.js
      └── tools
          
      12 directories, 9 files
          
      


-J: json format 출력

1
$ tree -J -L 1
  • 출력 값
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    
    [
      {"type":"directory","name":".","contents":[
        {"type":"file","name":"Gemfile"},
        {"type":"file","name":"Gemfile.lock"},
        {"type":"file","name":"LICENSE"},
        {"type":"file","name":"README.md"},
        {"type":"file","name":"_config.yml"},
        {"type":"directory","name":"_data"},
        {"type":"directory","name":"_includes"},
        {"type":"directory","name":"_javascript"},
        {"type":"directory","name":"_layouts"},
        {"type":"directory","name":"_plugins"},
        {"type":"directory","name":"_posts"},
        {"type":"directory","name":"_sass"},
        {"type":"directory","name":"_site"},
        {"type":"directory","name":"_tabs"},
        {"type":"directory","name":"assets"},
        {"type":"file","name":"index.html"},
        {"type":"file","name":"jekyll-theme-chirpy.gemspec"},
        {"type":"file","name":"package.json"},
        {"type":"file","name":"rollup.config.js"},
        {"type":"directory","name":"tools"}
      ]}
    ,
      {"type":"report","directories":12,"files":9}
    ]
      
    


-X: xml format 출력

1
$ tree -X -L 1
  • 출력 값
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    
    <?xml version="1.0" encoding="UTF-8"?>
    <tree>
      <directory name=".">
        <file name="Gemfile"></file>
        <file name="Gemfile.lock"></file>
        <file name="LICENSE"></file>
        <file name="README.md"></file>
        <file name="_config.yml"></file>
        <directory name="_data"></directory>
        <directory name="_includes"></directory>
        <directory name="_javascript"></directory>
        <directory name="_layouts"></directory>
        <directory name="_plugins"></directory>
        <directory name="_posts"></directory>
        <directory name="_sass"></directory>
        <directory name="_site"></directory>
        <directory name="_tabs"></directory>
        <directory name="assets"></directory>
        <file name="index.html"></file>
        <file name="jekyll-theme-chirpy.gemspec"></file>
        <file name="package.json"></file>
        <file name="rollup.config.js"></file>
        <directory name="tools"></directory>
      </directory>
      <report>
        <directories>12</directories>
        <files>9</files>
      </report>
    </tree>
      
    




참고한 사이트

이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.