December 9, 2019

Tree: ignore directories with patterns

Tree is a linux command that provides a list of recursive directories or a tree can display all file and directory listings in a directory.

Comparison of ls and tree usage

Tree:

$ tree Bash
Bash
├── BackupRestore-dbOpenEdx
│   ├── backup_mongo.sh
│   ├── backup_mysql.sh
│   ├── restore_mongo.sh
│   └── restore_mysql.sh
├── auto-update-dplk_env
│   ├── main.sh
│   └── playbook
│       ├── backend.yml
│       ├── config.yml
│       └── frontend.yml
├── backup\ database\ postgres.sh
├── backup_db.sh
├── compress_backup.sh
├── espeak.sh
├── get-date-backupdb-dplk.sh
├── get-log-core.sh
├── get-log-tmp.sh
├── import.sh
├── loop\ change\ time.sh
├── newProject.sh
├── scp_Dilla.sh
└── uni.sh

3 directories, 20 files
~/G/G/icarrr ❯❯❯

ls:

$ ls Bash
BackupRestore-dbOpenEdx     backup database postgres.sh compress_backup.sh          get-date-backupdb-dplk.sh   get-log-tmp.sh              loop change time.sh         scp_Dilla.sh
auto-update-dplk            backup_db.sh                espeak.sh                   get-log-core.sh             import.sh                   newProject.sh               uni.sh

To be able to use trees, trees must be installed in your environment:

-) Ubuntu or family

$ sudo apt install tree

-) CentOS or RHEL

$ sudo yum install tree

-I option allows to exclude directories that match specific pattern e.g.

$ tree Bash -I playbook
Bash
├── BackupRestore-dbOpenEdx
│   ├── backup_mongo.sh
│   ├── backup_mysql.sh
│   ├── restore_mongo.sh
│   └── restore_mysql.sh
├── auto-update-dplk
│   └── main.sh
├── backup\ database\ postgres.sh
├── backup_db.sh
├── compress_backup.sh
├── espeak.sh
├── get-date-backupdb-dplk.sh
├── get-log-core.sh
├── get-log-tmp.sh
├── import.sh
├── loop\ change\ time.sh
├── newProject.sh
├── scp_Dilla.sh
└── uni.sh

2 directories, 17 files

In order to exclude multiple directories at once, their names must be separated by | sign, i.e.

$ tree Bash -I 'playbook|BackupRestore-dbOpenEdx'
Bash
├── auto-update-dplk
│   └── main.sh
├── backup\ database\ postgres.sh
├── backup_db.sh
├── compress_backup.sh
├── espeak.sh
├── get-date-backupdb-dplk.sh
├── get-log-core.sh
├── get-log-tmp.sh
├── import.sh
├── loop\ change\ time.sh
├── newProject.sh
├── scp_Dilla.sh
└── uni.sh

1 directory, 13 files

This command will skip playbook.

Thank you for reading and sorry if the language is chaotic because it is currently learning to speak English.

Reference: