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
.