🗂️ Configuration Directories and Files in Shell Scripting
🧾 1. Introduction
In Linux/Unix systems, configuration files and directories play a vital role in
system behavior, application settings, and shell environments. Shell scripts often
read, modify, or back up these files for automation, deployment, or system
administration tasks.
📂 2. What Are Configuration Files?
• Configuration files store settings and preferences for:
o System behavior
o User environments
o Application-specific parameters
• Most configuration files are plain text files located in:
o /etc (system-wide configs)
o /home/username (user-level configs like .bashrc)
o Application-specific folders like /var/lib/, /usr/local/etc/
📁 3. Important Configuration Directories
Directory Purpose
/etc/ Main directory for system-wide configurations
/etc/init.d/ Scripts for starting/stopping services
/etc/network/ Network configurations
/etc/cron.d/ Scheduled job definitions
/home/user/ User's personal configuration (dotfiles)
📝 4. Common Configuration Files
File Description
/etc/passwd User account information
File Description
/etc/fstab Disk and filesystem mount info
/etc/hosts Static hostname-to-IP mapping
/etc/resolv.conf DNS settings
~/.bashrc User shell behavior and aliases
~/.profile Login shell configuration
/etc/crontab System-wide cron jobs
🧪 5. Shell Script Example: Backup Key Configuration Files
Let’s write a shell script to back up critical configuration files.
#!/bin/bash
# Directory to store backups
BACKUP_DIR="/home/user/config_backup_$(date +%F_%T)"
mkdir -p "$BACKUP_DIR"
# List of important config files to back up
CONFIG_FILES=(
"/etc/passwd"
"/etc/fstab"
"/etc/hosts"
"/etc/resolv.conf"
"/etc/ssh/sshd_config"
"/etc/crontab"
)
# Copying config files
echo "Backing up configuration files..."
for file in "${CONFIG_FILES[@]}"; do
if [ -f "$file" ]; then
cp "$file" "$BACKUP_DIR"
echo " Backed up $file"
else
echo " $file not found"
fi
done
# Logging the backup
echo "Backup completed on $(date)" >> "$BACKUP_DIR/backup_log.txt"
echo "Files stored in: $BACKUP_DIR"
🧠 6. Explanation of the Script
• Creates a timestamped backup folder.
• Iterates through a list of common configuration files.
• Checks if each file exists and copies it to the backup folder.
• Logs the activity.
This script can be scheduled using cron for regular backups.
🧰 7. Advanced Use Case: Modify .bashrc to Add Alias
#!/bin/bash
BASHRC="$HOME/.bashrc"
# Add custom alias if not already present
if ! grep -q "alias ll='ls -alF'" "$BASHRC"; then
echo "alias ll='ls -alF'" >> "$BASHRC"
echo " Alias added to .bashrc"
else
echo " Alias already exists in .bashrc"
fi
After editing .bashrc, use:
source ~/.bashrc
⚠️ 8. Best Practices
• Always backup before modifying config files.
• Use version control (like git) for tracking config changes.
• Use full paths in shell scripts for reliability.
• Use chmod to protect sensitive config files.
🧵 9. Real-Life Applications in Shell Scripting
Task How Shell Scripts Help
Automated backups Copy files to backup locations
Configuration management Read/modify configs during app setup
Remote server setup Push config files to target machines
Setting aliases and PATH Modify .bashrc or .profile
Cron job setup Modify /etc/crontab or user crontabs