Home Create skelleton template from project directories and file structure using tree, find and xagrgs commands
Post
Cancel

Create skelleton template from project directories and file structure using tree, find and xagrgs commands

Example Use case

Your customer has requested your help to create, a direcory and files skelleton template structure of an existing project. Customer wants the directory structure and file names to be the same but do not want any of the file content to be present they just want the skelleton template of the project stucture.

To solve this we need to create two files. One that contains the directory stucture and one file that contains the filenames of the files in the project. We solve this using the tree command. First we create the file that are only listing the directory structure.

Project requirements

As seen below in our example the customers project folders are located in ~/Ansible_Projects/ The customer wants us to create a skelleton template of the project install_terraform

1
2
3
4
5
6
7
8
9
 .
 ├── ansible-collection-hardening
 ├── awx
 ├── awx-operator
 ├── folder_to_template
 ├── install_terraform
 ├── kvm-deploy-ansible
 ├── my_test_driven_developments
 └── netbox-examples

Output folder

The customer wants us to put the generated text files in a directory called output that are located in the current users home directory.
~/output

Project Template

~/skelleton_project_templates

Project naming standard

project_project_name_dir.txt - the directory structure file
project_project_name_files.txt - the projects file

~/skelleton_project_templates/project_name/ - the skelleton project structure

Files and directores to be created

Based on the naming standard that the client has set we will create the following project directory and files.

~/skelleton_project_templates/install_terraform/ - skelleton project directory
project_install_terraform_dir.txt - filename for the directory structure
project_install_terraform_files.txt - filename for the project files

Now when we have the projects requirements set, we can start to create it.

Create the requested folders

Output folder

1
mkdir ~/output

Skelleton project folder

1
mkdir -p ~/skelleton_project_templates/install_terraform/

Create the template files

cd into ~/Ansible_Projects

1
cd  ~/Ansible_Projects

Create the directory structure template using tree

1
tree  install_terraform -dfiUR --noreport > ~/output/project_install_terraform_dir.txt | sed -i '1d' ~/output/project_install_terraform_dir.txt

Create the files template using tree

1
tree install_terraform -IfiUR '.git|.terraform|.gitignore' --noreport  > ~/output/project_install_terraform_files_.txt 

Finally create the skelleton project using xargs

cd into the template project stucture

1
cd ~/skelleton_project_templates/install_terraform/

Create the directory structure with xargs

1
xargs -I {} mkdir -p "{}" <  ~/output/project_install_terraform_dir.txt

Create the file structure with xargs

1
xargs -d'\n' touch < ~/output/project_install_terraform_files_.txt 

Example finished skelleton template

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
.
└── install_terraform
    ├── blank.md
    ├── inventory
    │   ├── dynamic_kvm_inventory.yml
    │   └── libvirt_lxc.yml
    ├── kvm_multiply_machines
    │   ├── cloud_init_centos.8.cfg
    │   ├── libvirt.tf
    │   ├── network_config_dhcp.cfg
    │   ├── state
    │   │   └── multiple_machines.tfstate
    │   ├── terraform.tfstate
    │   └── terraform.tfstate.backup
    ├── kvm_terraform
    │   ├── config
    │   │   └── cloud_init.yml
    │   ├── id_rsa
    │   ├── id_rsa.pub
    │   ├── libvirt.tf
    │   ├── main.tf
    │   ├── outputs.tf
    │   ├── terraform.tfstate
    │   └── terraform.tfstate.backup
    ├── local_test.yml
    ├── PlayBooks
    │   ├── check_update_needed.yml
    │   ├── collec_a_few_facts.yml
    │   ├── get_latest_release.yml
    │   ├── install_or_update_terraform.yml
    │   ├── install_terraform.yml
    │   ├── inv
    │   ├── list_vms.yml
    │   └── test_libvirt_inventory_plugin.yml
    ├── README.md
    └── terragrunt_project_test
This post is licensed under CC BY 4.0 by the author.