Andy Jarrett // Code. Develop. Create.

Two ways to graphically represent a directory structure from the CLI

A graphic overview of directory structures can aid comprehension, especially for large projects. Below are two scipts I use to visually depict a directory structure via the command line interface.

For hands-on experience, you can set up a demo directory using the commands below:


  mkdir example_dir
  cd example_dir
  touch file1.txt file2.txt
  mkdir node_modules other_folder
  cd node_modules
  touch package.json
  cd ../other_folder
  touch other_file.txt

    

Method 1: Inclusive of 'node_modules'

To get a graphical representation that includes the node_modules directory up to a specified depth, employ the tree command accompanied by the -L option:

When you run the tree command on example_dir, it will display the directory structure, incorporating node_modules up to the second level.

Method 2: Excluding 'node_modules'

If you'd prefer to omit directories like node_modules, consider using the find command combined with sed:

Executing this command on example_dir will yield a tree representation that excludes node_modules.

In conclusion, both methods offer insightful ways to visually represent directories straight from the CLI. Depending on your requirements, you have the flexibility to either incorporate or exclude specific directories, ensuring a lucid view of your project's layout.

I’m here, learning and working away. If you liked this content and want to keep me going, consider buying me a coffee. Your support keeps this site running and the coffee brewing! ☕️