Logo

dev-resources.site

for different kinds of informations.

Generate files and folder structures of your code

Published at
6/23/2023
Categories
automation
tree
bash
programming
Author
iamtekson
Categories
4 categories in total
automation
open
tree
open
bash
open
programming
open
Author
9 person written this
iamtekson
open
Generate files and folder structures of your code

I recently found a cool way to generate the file and folder structure inside your root project directory. In order to generate the folder structure, you need to follow following steps,

1. Download tree package

If you are in mac, type following code in your terminal,

brew install tree
Enter fullscreen mode Exit fullscreen mode

If you are in ubuntu, type following code in your terminal,

sudo apt install tree
Enter fullscreen mode Exit fullscreen mode

2. Navigate to the root directory

cd ~/Downloads/my-project
Enter fullscreen mode Exit fullscreen mode

Make sure to change the path.

3. Explore tree command

If you want to generate the files and folder structure in your working directory, just type tree command in terminal and it will print out the files and folders in proper structure,

tree
Enter fullscreen mode Exit fullscreen mode

But, most of the time, our project might contain some of the unnecessary things such as, node_modules, python_venv etc. In that case, you might want to avoid those folders or files. In order to avoid those, simply wright the following code,

find . -type f -not -path '*/\.*' | grep -v -F -f .gitignore | cut -c 3- | tree --fromfile --prune
Enter fullscreen mode Exit fullscreen mode

Let me explain the code for you,

  • find . -type f -not -path '*/\.*': This command recursively finds all the files (-type f) in the current directory (.) and its subdirectories. The -not -path '*/\.*' part excludes hidden files such as .git.

  • grep -v -F -f .gitignore: This command filters out the files and directories listed in the .gitignore file.

  • The cut -c 3- command removes the first two characters from each line of input, effectively removing the "./" prefix in each path.

  • tree --fromfile --prune: This command reads the file and directory structure from the output of the previous commands and displays it using the tree command. The --prune option excludes the empty directories from the output.

I got the following result from above script:

.
ā”œā”€ā”€ __init__.py
ā”œā”€ā”€ file1.ipynb
ā”œā”€ā”€ file2.ipynb
ā”œā”€ā”€ requirements.txt
ā”œā”€ā”€ script_json
ā”‚   ā”œā”€ā”€ test1.json
ā”‚   ā””ā”€ā”€ test2.json
ā”œā”€ā”€ version1.ipynb
ā””ā”€ā”€ version2.ipynb

Enter fullscreen mode Exit fullscreen mode

I hope this post is informative for you. If you like this blog, please support me by subscribing to my YouTube channel: https://www.youtube.com/c/iamtekson

tree Article's
30 articles in total
Favicon
House robber III
Favicon
Inorder traversal of a binary tree
Favicon
Comprehensive Tree Care Solutions in Gig Harbor and Tacoma with Pablo Tree Services
Favicon
Tree data structures in Rust with tree-ds (#2: Tree Operations)
Favicon
The Benefits of Hiring Professional Tree Trimmers in Christchurch
Favicon
Tree data structures in Rust with tree-ds (#1: Getting Started)
Favicon
DFS Traversal Guide: Easy way to remember DFS Traversel Path
Favicon
Chain - a Goofy, Functional, Tree-backed List
Favicon
Demystifying Tree Lopping vs. Tree Chipping: Which is Right for Your Landscape?
Favicon
Ergonomic Trees in Go
Favicon
Generating Dynamic Breadcrumb Menus Using Tree Table & Recursive CTE
Favicon
Types of decision tree in machine learning
Favicon
Tree Service Tips: Keeping Your Property Safe in Tinley Park
Favicon
What is tree data structure? šŸŒ³
Favicon
Golang multinode tree with parallel search
Favicon
Implementing Nested Filters using React and Tree Data Structure
Favicon
814. Binary Tree Pruning
Favicon
A hierarchical tree component for React in Typescript
Favicon
C++ - Basic Tree Traversal(Recursive vs Queue)
Favicon
C++ - Basic Tree Traversal(Recursive vs Stack)
Favicon
Generate files and folder structures of your code
Favicon
Converting materialized paths into a tree with generics: a Golang kata
Favicon
wishing3 - if you'd 3 wishes, what'd they be?
Favicon
Represent a directory tree in a Github README.md
Favicon
Segment Trees - Part I
Favicon
A Nibble of Quadtrees in Rust
Favicon
What is AST?
Favicon
Finding all children of a node in a tree
Favicon
5 Reasons You Need Tree Doctor to Keep Your Plants Healthy
Favicon
Binary Tree Pruning

Featured ones: