Think-Environment "Directory Structure"

Plan a directory structure for secure and efficient shell operations.

Page content

Introduction

When working with a shell, it is important to first create a safe and efficient workspace. One might initially think of using the user’s home directory as a workspace, but this is not very safe. This is because the home directory already contains many directories and files managed by the system. There is a risk of accidentally deleting these files due to command operation mistakes. Here, we create a working directory under the home directory and perform shell operations in that environment. The benefits of this structure are as follows:

  1. Prevent accidental file corruption: By creating a working directory, the risk of corrupting directories and files managed by the system in the home directory due to command operation mistakes is reduced.
  2. Easy and understandable backups: By making the working directory the target for data backups, unnecessary system files in the home directory can be excluded from the backup target.

Key point: Prepare a working directory for safe shell operations.

Prerequisites

Structure of the Working Directory

The following is a proposed structure for the working directory. First, create a directory named cms under the home directory (the origin of cms is the initials of Contents Management System). Next, create a drafts directory under the cms directory, which will serve as the working directory for shell operations. Additionally, under the cms directory, you can create directories such as bin and templates to store scripts and templates.

home
 └─cms
    ├─bin
    │  ├─pwsh-sketches   <-- cloned github repository
    │  ├─python-sketches <-- cloned github repository
    │  ├─r-sketches      <-- cloned github repository
    │  └─...
    ├─drafts    <-- working directory
    ├─templates <-- report templates
    ├─tools     <-- toolbox
    └─...

The main purpose of this structure is to reduce the risk of file corruption due to shell operation mistakes. The drafts working directory should be used to store only the minimum necessary files. By placing frequently used tools one level above the drafts directory, most shell operations can be performed without leaving the drafts directory. For example, a script placed in the bin directory can be executed from the drafts directory with a command like: ../bin/my-script.ps1.

fig. There are too many files in the home directory!

Creating the Working Directory

Follow these steps to first create the cms directory under the home directory, and then create the drafts directory under cms. The following shows how to create the working directory using Explorer and how to create it from PowerShell with command operations.

Creating the Working Directory with Explorer

  1. Open the home directory with Explorer
    • WIN+R → Type . (dot)
  2. Create a new cms directory under the home directory
    • Right-click → New → Folder → Type cmsENTER
      fig. Creating the cms directory
  3. Create a new drafts directory under the cms directory
    • Open the cms directory → Right-click → New → Folder → Type draftsENTER
      fig. Creating the drafts directory

Creating the Working Directory with PowerShell

  1. Launch PowerShell
  2. Create a new cms directory under the home directory
    • New-Item -ItemType Directory -Path ~/cms
  3. Create a new drafts directory under the cms directory
    • New-Item -ItemType Directory -Path ~/cms/drafts

Working in the Working Directory

  1. Launch PowerShell
  2. Move to the drafts working directory
    • cd ~/cms/drafts
  3. Do shell operations
    • For example, create and edit files in the drafts working directory

Terms

Home Directory

The user’s home directory. In Windows, it is located at C:/Users/<username>. To access it in Explorer, WIN+R → Type . (dot).

Home in Japanese is “家” (いえ)(ie). The meaning of “家” is a building where people live. It functions as a base for the family or a personal living space. In Japan, there are various forms of homes, from traditional wooden houses to modern apartments. The term “家” includes not only the physical building but also the concept of family, household, and ancestral lineage.

Working Directory

A directory for work. It can be created anywhere, but in this article, the working directory is created under the home directory to serve as a workspace for shell operations.