What are some useful bash scripts
- User
What are some useful bash scripts
- Assistant
Here are some examples of useful bash scripts:
- File backup script: This script will backup files from one directory to another, optionally compressing them and keeping multiple versions.
#!/bin/bash # Backup script # Define source and target directories src="/path/to/source" dst="/path/to/backup" # Define number of backup versions to keep versions=5 # Create target directory if it doesn't exist mkdir -p "$dst" # Find and remove old backup versions find "$dst" -type f -name "backup-*" -mtime +$[(versions\*86400)] -exec rm {} \; # Create new backup tar czf "$dst/backup-$(date +%Y-%m-%d-%H-%M-%S).tar.gz" "$src"- Disk usage script: This script will display the disk usage of all the directories in the current directory in a human-readable format.
#!/bin/bash # Disk usage script # Define directory to check dir="." # Print disk usage for each directory du -sh "$dir"/*/ | sort -h- Network monitoring script: This script will continuously monitor the network connection and print the current upload and download speeds.
#!/bin/bash # Network monitoring script # Define interface to monitor interface="eth0" # Print network usage every second while true; do rx=$(cat /proc/net/dev | grep "$interface" | tr -s ' ' | cut -d' ' -f3) tx=$(cat /proc/net/dev | grep "$interface" | tr -s ' ' | cut -d' ' -f11) echo "RX: $rx bytes/sec TX: $tx bytes/sec" sleep 1 rx=$((rx - $(cat /proc/net/dev | grep "$interface" | tr -s ' ' | cut -d' ' -f3))) tx=$((tx - $(cat /proc/net/dev | grep "$interface" | tr -s ' ' | cut -d' ' -f11))) done- System information script: This script will display various system information such as the operating system, kernel version, CPU and memory usage, and disk space.
#!/bin/bash # System information script # Print operating system information echo "Operating System:" uname -a # Print kernel version echo "Kernel Version:" uname -r # Print CPU information echo "CPU Information:" lscpu # Print memory usage echo "Memory Usage:" free -h # Print disk usage echo "Disk Usage:" df -hI hope you find these examples helpful! Let me know if you have any questions.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.