Linux One-Liners for Automation
These are practical one-liners you can paste into a terminal or drop into scripts. They’re grouped by what they help you do, not by command name. Most are Raspberry Pi–friendly: lightweight, loggable, and designed for real ops work.
1. Quick System Status Snapshots
uptime
free -h && df -h /
top -b -n 1 | head -n 20
ps aux --sort=-%cpu | head -n 15
ps aux --sort=-%mem | head -n 15
uname -a && lsb_release -a 2>/dev/null
2. Temperature, Throttling, Power (Raspberry Pi)
vcgencmd measure_temp
vcgencmd get_throttled
watch -n 1 'vcgencmd measure_temp; vcgencmd get_throttled'
dmesg | egrep -i "voltage|thrott|under-voltage|over-current" | tail -n 50
3. Disk Space, Biggest Files, SD Card Safety
df -h | sort -k5 -h
du -xhd 1 /home/pi | sort -h
find /var/log -type f -printf '%s %p\n' | sort -n | tail -n 20
find /home/pi -type f -size +100M -print
df -i
sudo sync
4. Find Stuff Fast
find . -type f -iname "*report*" 2>/dev/null
find / -type f -name "config.txt" 2>/dev/null | head
grep -Rni "TODO" .
grep -Rni "password" /etc 2>/dev/null | head
locate sshd_config | head
5. Log Hunting and Live Debugging
tail -n 200 /var/log/syslog
tail -f /var/log/syslog
journalctl -b -p err
journalctl -u ssh -b --no-pager | tail -n 200
journalctl -f
dmesg | tail -n 100
6. Networking “Is It Down or Is It DNS?”
ip addr
ip route
ping -c 3 8.8.8.8
ping -c 3 google.com
getent hosts google.com
cat /etc/resolv.conf
ss -tuln | head
ss -tuln | grep -E ":22|:80|:443"
7. Who’s Using That Port / Why Won’t It Start?
sudo ss -tulpn | grep ":8080"
sudo lsof -i :8080
systemctl status myservice --no-pager
journalctl -u myservice -b --no-pager | tail -n 200
8. Safe Batch Delete (Preview First, Then Delete)
Preview files older than 14 days:
find /home/pi/data -type f -mtime +14 -print
Delete them (only after preview):
find /home/pi/data -type f -mtime +14 -delete
Remove empty dirs afterward:
find /home/pi/data -type d -empty -delete
9. Quick Backups (Rsync “Good Defaults”)
Copy a directory (preserve, verbose):
rsync -aH --info=progress2 /home/pi/project/ /mnt/usb/project/
Mirror + delete removed files:
rsync -aH --delete /home/pi/project/ /mnt/usb/project/
Dry-run first:
rsync -aH --delete --dry-run /home/pi/project/ /mnt/usb/project/
Remote sync over SSH:
rsync -aH --delete -e ssh /home/pi/data/ user@host:/srv/pi/data/
10. Quick “Poor Man’s Monitoring” (Append-only logs)
CPU temp to log every minute (cron-friendly):
date --iso-8601=seconds; vcgencmd measure_temp; vcgencmd get_throttled
Write it to a file:
( date --iso-8601=seconds; vcgencmd measure_temp; vcgencmd get_throttled ) >> /home/pi/health.log 2>&1
Disk usage snapshot:
( date --iso-8601=seconds; df -h / ) >> /home/pi/disk.log 2>&1
11. Locking (Prevent Overlapping Runs)
Simple lock directory approach:
mkdir /tmp/myjob.lock 2>/dev/null || exit 0; trap 'rmdir /tmp/myjob.lock' EXIT; your_command_here
This avoids “two backups at once” problems.
12. Robust Download + Verification Patterns
Download to a temp file, then move into place:
tmp=$(mktemp) && curl -fsSL "URL" -o "$tmp" && mv "$tmp" ./file.bin
Retry downloads (network flaky):
curl -fsSL --retry 5 --retry-delay 2 "URL" -o file.bin
13. JSON / API Quick Reads (If jq installed)
Pretty-print:
cat data.json | jq .
Extract a field:
curl -s https://api.example.com/status | jq -r '.status'
14. CSV / Text Quick Analytics
Count lines:
wc -l file.csv
Unique values in column 1:
cut -d, -f1 file.csv | sort | uniq -c | sort -nr | head
Average numeric column 2 (skip header):
awk -F, 'NR>1{sum+=$2; n++} END{if(n) print sum/n}' file.csv
15. Permissions “Fix My Project Folder”
Make user own everything:
sudo chown -R pi:pi /home/pi/project
Make scripts executable:
find /home/pi/project -type f -name "*.sh" -exec chmod +x {} \;
Safer default perms for a directory tree:
find /home/pi/project -type d -exec chmod 755 {} \; && find /home/pi/project -type f -exec chmod 644 {} \;
16. Service/Troubleshooting One-Liners
Restart and immediately follow logs:
sudo systemctl restart myservice && journalctl -u myservice -f
See what failed this boot:
systemctl --failed
Show boot performance culprits:
systemd-analyze blame | head -n 20
List active timers:
systemctl list-timers --all | head -n 30
17. Cron “What Actually Ran?” and Debugging
Show your cron:
crontab -l
Test cron-like minimal environment:
env -i PATH=/usr/bin:/bin /home/pi/bin/job.sh
Run script and capture everything:
/home/pi/bin/job.sh >> /var/log/my-scripts/job.log 2>&1
18. Mount/USB “Why Won’t It Unmount?”
What’s mounted where:
findmnt | head
Who is using a mountpoint:
sudo fuser -vm /mnt/usb
Lazy unmount (use only if you understand the risk):
sudo umount -l /mnt/usb
19. SSH Convenience and Safety
Copy your public key:
ssh-copy-id pi@raspberrypi.local
Keep SSH alive on flaky links:
ssh -o ServerAliveInterval=30 -o ServerAliveCountMax=3 pi@host
Run a command remotely:
ssh pi@host 'uname -a && uptime'
20. “If This, Then That” One-Liners (Tiny Automation)
If a process isn’t running, start it:
pgrep -x myprog >/dev/null || /usr/local/bin/myprog &
If disk usage is high, log a warning:
p=$(df / | awk 'NR==2{gsub("%","",$5);print $5}'); [ "$p" -gt 90 ] && echo "$(date) WARNING disk $p%" >> /home/pi/alerts.log
If a host is down, record it:
ping -c 1 -W 2 192.168.1.10 >/dev/null || echo "$(date) host down" >> /home/pi/net-alerts.log
21. Camera/IoT File Management Patterns
Newest 10 photos:
ls -1t /home/pi/cam | head -n 10
Delete photos older than 7 days:
find /home/pi/cam -type f -name "*.jpg" -mtime +7 -delete
Create a daily tarball:
tar -czf "/home/pi/archive/cam-$(date +%F).tar.gz" /home/pi/cam
22. “Sanity Checks” Before Dangerous Commands
See what a wildcard matches:
printf "%s\n" *.log
Confirm you’re not about to delete root (habit):
pwd; ls
23. The Most Useful Pipe Patterns
Page long output:
some_command | less
Filter for errors:
some_command 2>&1 | grep -i error
Count matches:
grep -R "pattern" . | wc -l
Top 20 most common words in a file:
tr -cs '[:alnum:]' '\n' < file.txt | tr '[:upper:]' '[:lower:]' | sort | uniq -c | sort -nr | head -n 20
24. Turn Any Script into a “Run Once” systemd Unit (Quick Hack)
Run a script under systemd without writing files:
sudo systemd-run --unit=oneshot-test --property=Type=oneshot /usr/bin/bash /home/pi/bin/job.sh
Then view logs:
journalctl -u oneshot-test --no-pager
25. Mini “Ops Toolkit” Install (Pi OS)
sudo apt update && sudo apt install -y htop iotop iftop rsync curl wget jq tree
