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 775
function fixpermissions() {
find ./ -type d -print0 | xargs -0 chmod 0775
find ./ -type f -print0 | xargs -0 chmod 0644
}# Set all files in the ~/.ssh to 644 permissions and directories to 775
function fixsshpermissions() {
chmod -R 700 ~/.ssh/
chmod 600 ~/.ssh/id_rsa
chmod 600 ~/.ssh/config
chmod 644 ~/.ssh/id_rsa.pub
chmod 600 ~/.ssh/authorized_keys
}# Return public and local IP address
function myip() {
publicip=$(curl -s https://ipecho.net/plain)
localip=$(ip -o -4 addr show | awk -F '[ /]+' '/global/ {print $4}')
echo -e "Public IP: " $publicip
echo -e "Local IP: " $localip
}# Search processes containing a string or command (Usage: psg TERM)
psg() {
if [[ $# -eq 0 ]] ; then
echo -e "\e[0;31mPlease provide a process name.\e[0m"
else
ps 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 ]] ; then
echo -e "\e[0;31mPlease provide a string / search term\e[0m"
else
grep -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 keys
function 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.key
openssl genrsa -out server.key 4096
# ... (see full script for CSR and certificate signing config)
openssl req -new -key server.key -out server.csr -config csr.conf
openssl x509 -req \
-in server.csr \
-CA ca.crt -CAkey ca.key \
-CAcreateserial -out server.crt \
-days 14600 \
-sha256 -extfile cert.conf
rm -f ./cert.conf ./csr.conf
}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.
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. Remember to source your configuration file after adding new functions with
source ~/.bashrc or source ~/.zshrc.