Shell Functions
Here you'll find a few handy functions to use in ZSH and Bash Shell. Tested on Linux - likely work on Mac!
# Set all files in a directory to 644 permissions and directories to 775function fixpermissions() {find ./ -type d -print0 | xargs -0 chmod 0775find ./ -type f -print0 | xargs -0 chmod 0644}
# Set all files in the ~/.ssh to 644 permissions and directories to 775function fixsshpermissions() {chmod -R 700 ~/.ssh/chmod 600 ~/.ssh/id_rsachmod 600 ~/.ssh/configchmod 644 ~/.ssh/id_rsa.pubchmod 600 ~/.ssh/authorized_keys}
# Return public and local IP addressfunction myip() {publicip=$(curl -s https://ipecho.net/plain)localip=$(ip -o -4 addr show | awk -F '[ /]+' '/global/ {print $4}')echo -e "Public IP: " $publicipecho -e "Local IP: " $localip}
# Search processes containing a string or command (Usage: psg TERM)psg() {if [[ $# -eq 0 ]] ; thenecho -e "\e[0;31mPlease provide a process name.\e[0m"elseps aux | grep -v grep | grep -i -e VSZ -e "$@"fi}
# Search current directory for files containing specified string (Usage: search "Search Term")search() {if [[ $# -eq 0 ]] ; thenecho -e "\e[0;31mPlease provide a string / search term\e[0m"elsegrep -rni "$@" .fi}
# Backup a file by copying the file and prepending .bak to it (Usage: backup file.txt)backup() {cp -- "$1"{,.bak};}
# Generate self-signed CA, intermediate, and server certificates and keysfunction certgen() {openssl req -x509 \-sha256 \-newkey rsa:4096 \-days 14600 \-nodes \-subj "/C=GB/ST=Localhost/L=Localhost/O=Localhost Certificate Authority/OU=Certificate Authority Certificate Issuer/CN=localhost" -out ca.crt -keyout ca.keyopenssl genrsa -out server.key 4096cat > csr.conf <<EOF[ req ]default_bits = 4096prompt = nodefault_md = sha256req_extensions = req_extdistinguished_name = dn[ dn ]C = GBST = LocalhostL = LocalhostO = Localhost Server CertificateOU = Localhost Server Certificate IssuerCN = localhost[ req_ext ]subjectAltName = @alt_names[ alt_names ]DNS.1 = localhostIP.1 = 127.0.0.1EOFopenssl req -new -key server.key -out server.csr -config csr.confcat > cert.conf <<EOFauthorityKeyIdentifier = keyid, issuerbasicConstraints = CA:FALSEkeyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEnciphermentsubjectAltName = @alt_names[alt_names]DNS.1 = localhostIP.1 = 127.0.0.1EOFopenssl x509 -req \-in server.csr \-CA ca.crt -CAkey ca.key \-CAcreateserial -out server.crt \-days 14600 \-sha256 -extfile cert.confrm -f ./cert.conf ./csr.confread -p "Do you want to add the CA certificate to your operating system trusted certificates? (y/N) " -n 1 -rif [[ $REPLY =~ ^[Yy]$ ]]thenif [ -d "/etc/pki/ca-trust/source/anchors" ]; thensudo cp ca.crt /etc/pki/ca-trust/source/anchors/ca.crtupdate-ca-trust extractfiif [ -d "/usr/local/share/ca-certificates" ]; thensudo cp ca.crt /usr/local/share/ca-certificates/ca.crtupdate-ca-certificatesfiif [ -d "/etc/ca-certificates/trust-source/anchors" ]; thensudo cp ca.crt /etc/ca-certificates/trust-source/anchors/ca.crttrust extract-compatfiif [ -d "/usr/share/pki/trust/anchors" ]; thensudo cp ca.crt /usr/share/pki/trust/anchors/ca.crtupdate-ca-certificatesfifi}
Conclusion
These shell functions provide powerful automation for common system administration and development tasks. From fixing file permissions to generating SSL certificates, each function encapsulates complex command sequences into simple, memorable commands. By adding these to your ~/.bashrc or ~/.zshrc configuration, you'll have a robust toolkit available whenever you need it, reducing the cognitive load of remembering exact command syntax for infrequent but important tasks.
Start by implementing the functions most relevant to your workflow, whether that's the myip function for network diagnostics, the search function for code exploration, or the certgen function for local development HTTPS. Each function includes error checking and user-friendly prompts where appropriate, making them safe and practical for everyday use. Remember to source your configuration file after adding new functions, and don't hesitate to customize these examples to better suit your specific needs and preferences.