Ignore node_modules folder when using the `tree` command
May 11, 2022 ‐ 2 min read
The tree is a handy command-line utility for listing the contents of a folder with the contents of all the subfolders. However convenient, this can get out of hand rather quickly, especially in a Node.js project with a buffy node_modules/ folder.
You may ignore a folder in the output of tree by using the -I (capital i) option. Thus, ignoring the node modules would be like tree -I node_modules.
$ tree -I node_modules
.
├── components
│ └── PostsPage.vue
├── nuxt.config.js
├── package.json
├── package-lock.json
├── pages
│ ├── index.vue
│ ├── page
│ │ └── _page.vue
│ └── _slug.vue
├── static
│ └── favicon.ico
└── utils
└── fetchPostsMixin.js
5 directories, 9 files
Besides the node_modules/ folder there might be other folders you wish to ignore in the output of tree, a dist/ folder for example that may contain the output of a build.
You separate the directories you pass to the ignore option by a | symbol. Make sure to put the entire list of folders between quotes, otherwise the | is interpreted by your shell as output redirection.
$ tree -I 'node_modules|dist'
Besides ignoring certain directories there are other options to trim the output of the tree command:
- The
-doption will only list directories and no files:tree -I node_modules -d. - The
-Loption will only list directories down to a given level of depth:tree -I node_modules -L 2.