Power User: 10 Command Substitutions in Linux to improve your productivity

Power User: 10 Command Substitutions in Linux to improve your productivity

Command substitutions in Linux allow you to use the output of a command as an argument in another command. This can be incredibly powerful for scripting and managing servers by automating tasks and making decisions based on dynamic data.

Here are 10 useful command substitution examples for monitoring and maintaining your server:

  1. Check Disk Usage of a Specific Directory:
    Use the output of du to find out which directory is using the most disk space and pipe it into sort and head to get the top directories.
   echo "Most disk usage in $(du -sh /var/* | sort -rh | head -n1)"
  1. Backup a Folder to a Timestamped Directory:
    Create a backup of a folder with a directory name that includes the current timestamp.
   tar -czf backup-$(date +"%Y-%m-%d_%H-%M-%S").tar.gz /path/to/folder
  1. Find the Most Memory-Intensive Process:
    Display the process using the most memory.
   echo "Highest memory process: $(ps axch -o cmd:15,%mem --sort=-%mem | head -1)"
  1. Restart a Service if It Is Not Running:
    Check if a service is running and restart it if it’s not.
   systemctl status nginx | grep "active (running)" > /dev/null 2>&1 || (echo "Restarting Nginx"; systemctl restart nginx)
  1. Check Available Disk Space Before Downloading a Large File:
    Ensure there is enough disk space before attempting a large download.
   if [[ $(df --output=avail / | tail -1) -lt 10000000 ]]; then echo "Not enough disk space"; else wget http://example.com/largefile.zip; fi
  1. Automatically Kill Processes Exceeding Memory Limits:
    Find and kill any process exceeding a given memory threshold.
   ps axch -o cmd,pid,%mem --sort=-%mem | awk '{ if ($3 > 50.0) system("kill -9 " $2) }'
  1. Backup and Rotate Logs:
    Compress old logs and remove very old backups automatically.
   tar -czf /backup/logs-$(date +"%Y-%m-%d").tar.gz /var/log && find /backup -name "*.tar.gz" -type f -mtime +30 -exec rm {} \;
  1. Display a List of Open Ports and Associated Processes:
    Use netstat to find open ports and link them with processes using command substitution.
   echo "Open ports: $(netstat -tulpn | grep LISTEN)"
  1. Automate Updates and Clean Up:
    Update the system and automatically remove unnecessary packages.
   sudo apt update && sudo apt upgrade -y && sudo apt autoremove -y
  1. Send an Email If a Directory’s Size Changes:
    Monitor the size of a directory and send an email if it changes.
    bash SIZE_BEFORE=$(du -s /path/to/directory) sleep 3600 # wait an hour SIZE_AFTER=$(du -s /path/to/directory) if [ "$SIZE_BEFORE" != "$SIZE_AFTER" ]; then echo "Directory size changed" | mail -s "Alert: Directory Size Change" user@example.com; fi

These examples illustrate how command substitution can automate tasks, making server maintenance more manageable. They show how dynamic and responsive your server management can be when leveraging the power of the Linux command line.

Leave a Reply

Your email address will not be published. Required fields are marked *


Translate »