Join my free Bash scripting course on Udemy πŸ‘€

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 -d option will only list directories and no files: tree -I node_modules -d.
  • The -L option will only list directories down to a given level of depth: tree -I node_modules -L 2.