Per-Directory Management — Advanced Bash Customization
- Understand the concept and purpose of per-directory configuration in Bash.
- Learn how to apply different behaviors or environments automatically when entering specific folders.
- Discover the main approaches — manual Bash logic, modular
.bashrc.d/, and thedirenvtool. - View the directory structure (tree view) of user-level and WordPress-site-level setups.
- Apply per-directory configurations for WordPress VPS management, such as environment tags, PHP version checks, and automatic WP-CLI context.
- Follow best practices for performance, safety, and security when using contextual Bash setups.
1. What Is Per-Directory Management
Per-directory management means that Bash changes its configuration or behavior depending on the current working directory ($PWD).
When you navigate (cd) into a directory, specific environment variables, prompts, or aliases can activate — and they can reset automatically when you leave that directory.
Example goal:
- Enter
/home/dev-wpstrategist/public_html→ prompt turns green and$WP_ENVbecomesdevelopment. - Enter
/home/prod-wpstrategist/public_html→ prompt turns red and$WP_ENVbecomesproduction.
2. Why Per-Directory Setup Matters
| Reason | Description |
|---|---|
| Context Awareness | Automatically adapts configuration for each WordPress site. |
| Safety | Helps prevent running dangerous commands on production sites. |
| Convenience | Loads custom aliases, variables, or functions for each directory. |
| Automation | Reduces manual setup when managing multiple VPS WordPress installations. |
| Isolation | Keeps each site’s environment independent and reproducible. |
3. Where Configuration Lives
| Scope | File | Description |
|---|---|---|
| User-Level | ~/.bashrc.d/ | Modular Bash scripts loaded for each user. |
| Directory-Specific | .envrc (via direnv) | Automatically loads per-directory variables. |
| System-Level | /etc/profile.d/ | Shared across all users and environments. |
4. File Locations (Tree View)
User Configuration
/home/
└── donnyariw/
├── .bashrc
├── .bashrc.d/
│ ├── dev.sh
│ ├── stage.sh
│ └── prod.sh
├── .bash_profile
├── .bash_prompt
├── .bash_aliases
└── .bash_functions
WordPress VPS Example
/home/
├── dev-wpstrategist/
│ └── public_html/
│ └── .envrc ← dev-only configuration
├── stage-wpstrategist/
│ └── public_html/
│ └── .envrc
└── prod-wpstrategist/
└── public_html/
└── .envrc
5. Approach 1: Modular .bashrc.d/ Directory
a. Setup Modular Structure
Create directory:
mkdir ~/.bashrc.d
Inside ~/.bashrc, add loader logic:
# Load all modular Bash configurations
for file in ~/.bashrc.d/*.sh; do
[ -r "$file" ] && . "$file"
done
Now each .sh file under .bashrc.d/ acts as a sub-module.
b. Example: dev.sh
# ~/.bashrc.d/dev.sh
if [[ $PWD == *"dev-wpstrategist"* ]]; then
export WP_ENV=development
export PS1='[\e[32m][DEV: \u@\h \W]\$[\e[0m] '
fi
c. Example: prod.sh
# ~/.bashrc.d/prod.sh
if [[ $PWD == *"prod-wpstrategist"* ]]; then
export WP_ENV=production
export PS1='[\e[31m][PROD: \u@\h \W]\$[\e[0m] '
fi
When you cd into /home/prod-wpstrategist/public_html,
the prompt automatically changes to red and $WP_ENV updates.
6. Approach 2: Using direnv Tool (Automatic Loading)
direnv is a CLI utility that automatically loads environment variables when entering a directory and unloads them when you leave.
a. Installation
sudo apt install direnv -y
b. Enable for Bash
echo 'eval "$(direnv hook bash)"' >> ~/.bashrc
source ~/.bashrc
c. Setup for WordPress Site
Example directory:
/home/dev-wpstrategist/public_html/.envrc
Content:
export WP_ENV=development
export PATH=$PATH:/home/dev-wpstrategist/tools
export PS1='[\e[32m][DEV: \u@\h \W]\$[\e[0m] '
Authorize:
direnv allow
Output:
direnv: loading ~/dev-wpstrategist/public_html/.envrc
direnv: export +WP_ENV +PATH +PS1
When you leave the directory:
direnv: unloading
✅ Variables revert automatically.
7. Approach 3: Scripted Detection Logic
If you prefer not to install additional tools, Bash itself can detect your current path and adjust behavior.
Add this to .bashrc:
case "$PWD" in
*dev-wpstrategist*)
export WP_ENV=development
export PS1='[\e[32m][DEV: \u@\h \W]\$[\e[0m] '
;;
*stage-wpstrategist*)
export WP_ENV=staging
export PS1='[\e[33m][STAGE: \u@\h \W]\$[\e[0m] '
;;
*prod-wpstrategist*)
export WP_ENV=production
export PS1='[\e[31m][PROD: \u@\h \W]\$[\e[0m] '
;;
esac
Reload:
source ~/.bashrc
Now your prompt automatically reflects the environment whenever you cd into any project directory.
8. WordPress VPS Use Cases
| Use Case | Example | Description |
|---|---|---|
| Environment Identification | Prompt shows [DEV], [STAGE], [PROD] | Prevents running production commands by mistake. |
| Custom Paths | PATH=$PATH:/home/$USER/tools | Loads per-site tools or scripts. |
| WP-CLI Context | alias wp='wp --path=/home/dev-wpstrategist/public_html' | Automatically points WP-CLI to correct path. |
| Backup Directory Mapping | export WP_BACKUP_DIR="/root/wpbackup/$WP_ENV" | Keeps backups organized by environment. |
| Cache Behavior | export LS_CACHE=1 or 0 | Enables or disables LSCache per environment. |
9. Security and Performance Considerations
- Do not load sensitive credentials (like API keys) in
.envrcunless permissions are restricted.
chmod 600 .envrc
- Avoid heavy logic inside
.bashrc— Bash runs it at every session start. - Disable per-directory overrides on shared servers unless isolation is guaranteed.
- Document each directory’s purpose — helps collaborators identify environments safely.
- **Back up
.bashrc.d/and **.envrcfiles before major updates.
10. Troubleshooting
| Problem | Cause | Solution |
|---|---|---|
.envrc not loading | direnv not authorized | Run direnv allow inside the directory. |
| Variables persist after leaving | direnv misconfigured | Re-load eval "$(direnv hook bash)". |
| Slow prompt rendering | Too many conditions in .bashrc | Move logic into .bashrc.d/ or .envrc. |
| PS1 not changing | $PWD not matched properly | Check case patterns or path expressions. |
11. Quick Lab
Objective: Set up automatic prompt and variable change per environment directory.
- Install
direnv:
sudo apt install direnv -y
- Add to
.bashrc:
echo 'eval "$(direnv hook bash)"' >> ~/.bashrc
source ~/.bashrc
- Create project directories:
mkdir -p /home/{dev,stage,prod}-wpstrategist/public_html
- Add
.envrcfiles: Development
export WP_ENV=development
export PS1='[\e[32m][DEV: \u@\h \W]\$[\e[0m] '
Production
export WP_ENV=production
export PS1='[\e[31m][PROD: \u@\h \W]\$[\e[0m] '
- Authorize each:
cd /home/dev-wpstrategist/public_html && direnv allow
cd /home/prod-wpstrategist/public_html && direnv allow
- Verify:
echo $WP_ENV
echo $PS1
✅ Prompt color and environment variable change automatically per directory.
12. Glossary
| Term | Definition |
|---|---|
| Per-Directory Configuration | Applying different settings depending on the current directory. |
| direnv | A tool that loads/unloads variables when entering/leaving directories. |
| .bashrc.d | A folder-based modular approach for user-specific shell configurations. |
| WP_ENV | A custom variable used to identify WordPress environments. |
| PS1 | The primary Bash prompt variable. |
13. Mini-Quiz
- What command authorizes an
.envrcfile to load automatically? - Where should
.bashrc.d/files be located? - What’s the advantage of per-directory configuration for multi-site WordPress VPS setups?
- How does Bash detect which environment you’re in?
- Why should
.envrcfiles have restricted permissions?
14. Summary
- Per-directory management allows Bash to adapt automatically when entering specific directories.
- Methods include modular
.bashrc.d/scripts, path detection in ****.bashrc, ordirenv** automation**. - Ideal for WordPress VPS setups managing multiple environments (DEV, STAGE, PROD).
- Enables safe, contextual automation with unique prompts and environment variables.
- Maintain strict file permissions, avoid heavy logic in
.bashrc, and document each environment for clarity and safety.