├── README.md ├── .bashrc └── .shell_functions /README.md: -------------------------------------------------------------------------------- 1 | # Linux Shell Snippets 2 | 3 | These are my Linux shell configuration files, which contain helpful snippets that others can use to add functionality to their shell. -------------------------------------------------------------------------------- /.bashrc: -------------------------------------------------------------------------------- 1 | ### Place at the bottom of your .bashrc file 2 | 3 | # Include custom functions 4 | if [ -f ~/.shell_functions ]; then 5 | . ~/.shell_functions 6 | fi 7 | 8 | # Initialize Linux 9 | linux_initialize -------------------------------------------------------------------------------- /.shell_functions: -------------------------------------------------------------------------------- 1 | # ~/.shell_functions: included in ~/.bashrc, contains custom POSIX-compliant shell functions and aliases 2 | 3 | projects_dir="projects" 4 | 5 | # Formatting output 6 | 7 | # Font weight 8 | bold=$(tput bold) 9 | normal=$(tput sgr0) 10 | 11 | # Font color 12 | black=$(tput setaf 0) 13 | red=$(tput setaf 1) 14 | green=$(tput setaf 2) 15 | yellow=$(tput setaf 3) 16 | blue=$(tput setaf 4) 17 | magenta=$(tput setaf 5) 18 | cyan=$(tput setaf 6) 19 | white=$(tput setaf 7) 20 | reset=$(tput setaf 9) 21 | 22 | 23 | ### 24 | ### Linux / General 25 | ### 26 | 27 | # Project shortcuts 28 | alias cd_projects="cd ~/$projects_dir" 29 | 30 | # Creates an alias for every project in ~/$projects_dir/ 31 | for dir in ~/$projects_dir/*/ 32 | do 33 | dir=${dir%*/} # remove the trailing "/" 34 | project="${dir##*/}" # print everything after the final "/" 35 | alias cd_$project="cd ~/$projects_dir/$project" 36 | done 37 | 38 | # Initialize Linux 39 | linux_initialize () { 40 | echo "${bold}Initializing Linux...${normal}" 41 | echo "" 42 | 43 | # Update IP address in Windows and Linux hosts files (IP changes on reboot) 44 | hosts_files_update 45 | echo "" 46 | 47 | # Start Apache2 and MySQL 48 | apache_mysql_start 49 | echo "" 50 | 51 | # Enable WordPress CLI autocompletion 52 | wp_cli_enable_autocompletion 53 | echo "" 54 | 55 | # Check for upgrade 56 | linux_upgrade 57 | } 58 | 59 | # Upgrade Linux 60 | # update: downloads package information 61 | # upgrade: install upgrades using updated package data 62 | linux_upgrade () { 63 | echo "${bold}Upgrading Linux${normal}" 64 | sudo apt update 65 | sudo apt upgrade 66 | } 67 | 68 | # Update IP address in Windows and Linux hosts files (IP changes on reboot) 69 | hosts_files_update () { 70 | echo "${bold}Updating IP Address in Windows and Linux Hosts Files${normal}" 71 | 72 | # Set hosts files variables 73 | linux_hosts_file="/etc/hosts" 74 | windows_hosts_file="/mnt/c/Windows/System32/drivers/etc/hosts" 75 | 76 | # Get current IP address in Linux hosts files 77 | linux_old_ip=$(sudo cat ${linux_hosts_file} | grep -E -o "([0-9]{1,3}[\.]){3}[0-9]{1,3}" | tail -1) 78 | windows_old_ip=$(sudo cat ${windows_hosts_file} | grep -E -o "([0-9]{1,3}[\.]){3}[0-9]{1,3}" | tail -1) 79 | 80 | # Get IP address 81 | ip=$(hostname -I | cut -d' ' -f1) 82 | 83 | # Update Linux hosts file 84 | echo "- Updating Linux hosts file $linux_hosts_file" 85 | if [ $linux_old_ip != $ip ]; then 86 | echo "❕ Replacing old IP address $linux_old_ip with new IP addresss $ip in $linux_hosts_file" 87 | sudo sed -i "s/$linux_old_ip/$ip/g" $linux_hosts_file 88 | else 89 | echo "✅ No updated needed in $linux_hosts_file" 90 | fi 91 | 92 | # Update Windows hosts file 93 | echo "- Updating Windows hosts file $windows_hosts_file" 94 | if [ $windows_old_ip != $ip ]; then 95 | echo "❕ Replace old IP address $windows_old_ip with new IP addresss $ip in $windows_hosts_file" 96 | echo "Open cmd.exe as Administrator and run:" 97 | echo "notepad.exe C:/Windows/System32/drivers/etc/hosts" 98 | read -p "Press ENTER to continue to continue and reload Apache" input 99 | sudo service apache2 reload 100 | #echo "Replacing old IP address $windows_old_ip with new IP addresss $ip in $windows_hosts_file" 101 | #sudo sed -i "s/$windows_old_ip/$ip/g" "$windows_hosts_file" 102 | else 103 | echo "✅ No updated needed in $windows_hosts_file" 104 | fi 105 | } 106 | 107 | ### 108 | ### Apache and MySQL 109 | ### 110 | 111 | alias apache_status="sudo service apache2 status" 112 | alias apache_restart="sudo service apache2 restart" 113 | alias apache_reload="sudo service apache2 reload" 114 | alias apache_start="sudo service apache2 start" 115 | alias apache_stop="sudo service apache2 stop" 116 | 117 | alias mysql_status="sudo service mysql status" 118 | alias mysql_restart="sudo service mysql restart" 119 | alias mysql_reload="sudo service mysql reload" 120 | alias mysql_start="sudo service mysql start" 121 | alias mysql_stop="sudo service mysql stop" 122 | 123 | # Get Apache2 and MySQL status 124 | apache_mysql_status () { 125 | echo "${bold}Getting Apache2 and MySQL status${normal}" 126 | 127 | # Get Apache2 status 128 | echo "- Getting Apache status" 129 | if sudo service apache2 status | grep -q "apache2 is not running"; then 130 | echo "❌ Apache2 is not running" 131 | else 132 | echo "✅ Apache2 is running" 133 | fi 134 | 135 | # Get MySQL status 136 | echo "- Getting MySQL status" 137 | if sudo service mysql status | grep -q "MySQL is stopped"; then 138 | echo "❌ MySQL is not running" 139 | else 140 | echo "✅ MySQL is running" 141 | fi 142 | } 143 | 144 | # Restart Apache2 and MySQL 145 | apache_mysql_restart () { 146 | echo "${bold}Restarting Apache2 and MySQL${normal}" 147 | 148 | # Restart Apache2 149 | echo "- Restaring Apache" 150 | sudo service apache2 restart 151 | 152 | # Restart MySQL 153 | echo "- Restarting MySQL" 154 | sudo service mysql restart 155 | } 156 | 157 | # Reload Apache2 and MySQL (don't restart, just re-read configuration files) 158 | apache_mysql_reload () { 159 | echo "${bold}Reloading Apache2 and MySQL${normal}" 160 | 161 | # Reload Apache2 162 | echo "- Reloading Apache" 163 | sudo service apache2 reload 164 | 165 | # Reload MySQL 166 | echo "- Reloading MySQL" 167 | sudo service mysql reload 168 | } 169 | 170 | # Start Apache2 and MySQL 171 | apache_mysql_start () { 172 | echo "${bold}Starting Apache2 and MySQL${normal}" 173 | 174 | # Start Apache2 if not running 175 | echo "- Staring Apache" 176 | if sudo service apache2 status | grep -q "apache2 is not running"; then 177 | sudo service apache2 start 178 | else 179 | echo "✅ Apache2 is already running" 180 | fi 181 | 182 | # Start MySQL if not running 183 | echo "- Starting MySQL" 184 | if sudo service mysql status | grep -q "MySQL is stopped"; then 185 | sudo service mysql start 186 | else 187 | echo "✅ MySQL is already running" 188 | fi 189 | } 190 | 191 | # Stop Apache2 and MySQL 192 | apache_mysql_stop () { 193 | echo "${bold}Stopping Apache2 and MySQL${normal}" 194 | 195 | # Stop Apache2 if running 196 | echo "- Stopping Apache" 197 | if sudo service apache2 status | grep -q "apache2 is not running"; then 198 | echo "✅ Apache2 is not running" 199 | else 200 | sudo service apache2 stop 201 | fi 202 | 203 | # Stop MySQL if running 204 | echo "- Stopping MySQL" 205 | if sudo service mysql status | grep -q "MySQL is stopped"; then 206 | echo "✅ MySQL is not running" 207 | else 208 | sudo service mysql stop 209 | fi 210 | } 211 | 212 | ### 213 | ### GitHub 214 | ### 215 | 216 | # Open the current repository on github.com 217 | function gh { 218 | this_repo=`git config --get remote.origin.url 2>&1` 219 | open "https://github.com/`expr "$this_repo" : '.*:\(.*\).git'`/" 220 | } 221 | 222 | ### 223 | ### WordPress 224 | ### 225 | 226 | # WGet latest version of WordPress 227 | alias wp_wget="wget https://wordpress.org/latest.tar.gz; tar -xzvf latest.tar.gz" 228 | 229 | # Set WordPress file permissions 230 | wp_set_permissions () { 231 | echo "${bold}Setting WordPress File Permissions${normal}" 232 | 233 | if [ $# = 1 ]; then 234 | echo "- Changing permissions for directories in $1 to 755" 235 | find $1 -type d -exec sudo chmod 755 {} \; 236 | echo "- Changing permissions for files in $1 to 644" 237 | find $1 -type f -exec sudo chmod 644 {} \; 238 | echo "- Changing permissions for $1/wp-config.php to 660" 239 | sudo chmod 660 $1/wp-config.php; 240 | else 241 | echo "❗ Usage: wp_fix_permissions [/full/path/to/WordPress/website], e.g. wp_fix_permissions ~/$projects_dir/wordpress" 242 | fi 243 | } 244 | 245 | # Enable WordPress CLI autocompletion 246 | # https://github.com/wp-cli/wp-cli/blob/master/utils/wp-completion.bash 247 | wp_cli_enable_autocompletion () { 248 | echo "${bold}Enabling WordPress CLI Autocompletion${normal}" 249 | 250 | # Enable WordPress CLI autocompletion 251 | _wp_complete() { 252 | local OLD_IFS="$IFS" 253 | local cur=${COMP_WORDS[COMP_CWORD]} 254 | 255 | IFS=$'\n'; # want to preserve spaces at the end 256 | local opts="$(wp cli completions --line="$COMP_LINE" --point="$COMP_POINT")" 257 | 258 | if [ "$opts" =~ \\s* ] 259 | then 260 | COMPREPLY=( $(compgen -f -- $cur) ) 261 | elif [ $opts = "" ] 262 | then 263 | COMPREPLY=( $(compgen -f -- $cur) ) 264 | else 265 | COMPREPLY=( ${opts[*]} ) 266 | fi 267 | 268 | IFS="$OLD_IFS" 269 | return 0 270 | } 271 | 272 | complete -o nospace -F _wp_complete wp 273 | } 274 | --------------------------------------------------------------------------------