Bash one-liners worth keeping

/HD
Public by a guest 17h ago 1.4 KB 6 views
Raw Download
Report
# FIND BIG THINGS
du -sh * | sort -rh | head -20            # biggest items here
du -h --max-depth=2 / 2>/dev/null | sort -rh | head
find . -type f -size +100M                # files over 100MB
df -h                                      # disk free, human readable

# FIND AND ACT
find . -name '*.log' -mtime +30 -delete    # delete logs older than 30 days
find . -type f -name '*.txt' -exec grep -l 'needle' {} +
grep -rn 'TODO' --include='*.php' .        # recursive, with line numbers

# TEXT
sort file | uniq -c | sort -rn | head      # most common lines
awk '{print $1}' access.log | sort | uniq -c | sort -rn | head   # top IPs
sed -i 's/old/new/g' file                  # in-place replace
tr -s ' '                                  # squeeze repeated spaces
cut -d: -f1 /etc/passwd                    # field by delimiter

# PROCESSES
ps aux --sort=-%mem | head                 # top memory users
lsof -i :80                                # what is on this port
ss -tulpn                                  # listening sockets

# NETWORK
curl -sI https://example.com               # headers only
curl -s -o /dev/null -w '%{http_code}\n' URL    # just the status code
curl -sL URL                               # follow redirects

# SAFETY
rm -i                                      # prompt before each delete
cp -n src dst                              # never overwrite
<command> --dry-run                        # if it offers one, use it first