Bash Snippets and Aliases
Table of Contents
File Commands
Command | Description | Example |
---|---|---|
file | Returns the file type | file filename.txt |
head | Output first part of a file | head -n 10 (-n are lines) |
tail | Read the bottom of a file | tail log.txt |
fold | Wrap each input line to fit a specific width | fold -w 80 (-w is width) |
cat | Concatenate files and print it out | cat log.txt |
Get a Files Basename
location="/var/log/apache2/error.log"
echo "${location##*/}"
# error.log
basename -- $location
# error.log
Get a Files Extension
location="/var/log/apache2/error.log"
echo "${location##*.}"
# log
Get a File without the Extension
location="/var/log/apache2/error.log"
tmp="${location##*/}"
echo ${tmp%.*}
# error
Creating Files and Folders
Create a Nested Folders
mkdir -p assets/images
Create Multiple Nested Folders
mkdir -p assets/{images,scripts,styles}
Make Multiple Folders With The Same Prefix
mkdir -p repo_{dev,staging,prod}
Install Packages Shorter Syntax
# Instead of this
sudo apt install php8.1 php8.1-mysql php8.1-curl php8.1-gd
# Do this
sudo apt install php8.1 php8.1-{mysql,curl,gd}
Create Random Strings
Generate a Random OpenSSL String
openssl rand -hex 20
openssl rand -base64 256
Generate an MD5 Checksum
# We pipe the output of the command into md5sum
echo $STR | md5sum | head -c 20; echo;
Generate a Base64 Value
echo $STR | base64 | head -c 20; echo
Generate Many Random Strings
#
cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 50 | head -n 10
Bash Aliases
Create a Folder and Go Into It
mkdirgo() { mkdir -p $1; cd $1 }
Count Files in a Folder
alias count='find . -type f | wc -l'
Assign Variable with One Line Ternary
Although I find this sloppy, you can still do it.
GRC="" && [ -x "$(command -v grc)" &> /dev/null ] && GRC='grc'
something="default" && [[ 1 > 2 ]] && myvar="non-default"
Simplify Required Sudo Commands
# ╔══════════════════════════════════════════╗
# ║ Sudo ║
# ╚══════════════════════════════════════════╝
alias chgrp='sudo chown'
alias chmod='sudo chmod'
alias chmox='sudo chmod' # always make this typo
alias chown='sudo chown'
alias service='sudo service'
alias systemctl='sudo systemctl'
# Super User 'sudo' shortcuts
alias apt='sudo apt'
alias apt-get='sudo apt-get'
alias su='sudo -i' # Become root
alias root='sudo -i' # Become root
alias service='sudo service'
alias sudo='sudo ' # Enables aliases to be sudo-ed
Simplyify System Updating
# ╔══════════════════════════════════════════╗
# ║ System ║
# ╚══════════════════════════════════════════╝
alias autoclean='sudo apt autoclean'
alias autoremove='clean'
alias clean='sudo apt autoremove; sudo apt autoclean'
alias update='sudo apt-get update'
alias updatey='sudo apt-get update && \
sudo apt-get upgrade -y && \
sudo apt autoremove -y && \
sudo apt autoclean'
alias upgrade='sudo apt-get upgrade'
alias ppa='sudo apt-add-repository'
Simplify Directory Navigation
# ╔══════════════════════════════════════════╗
# ║ Navigation ║
# ╚══════════════════════════════════════════╝
alias l="ls -lh --color=auto"
alias la="ls -lah --color=auto" # Long List
alias ll="ls -lah --color=auto" # Long List
alias ls="ls --color=auto"
alias l.="ls -d .* --color=auto | column -s='\n'" # List Hidden files
alias lh="l." # <List Hidden Files>; Alias for Alias
alias ll.="ls -lhd .* --color=auto" # Long List Hidden
alias llh="ll." # <List Hidden Files; Alias for Alias
# Navigation
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
Colorize Some Built In Functions
# Grep
alias grep='grep --color=auto'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
Various Utility Aliases
alias mkdir='mkdir -pv' # Allows to Create nested dirs by default
alias xclip='xclip -sel clip' # Copy to Clipboard $ cat file.txt | xclip
alias wget="wget -c" # Resume if failed by default
alias findfile="find . -name " # Make find a little easier
alias myip="curl -s https://checkip.amazonaws.com"
alias myips="hostname -I"
Show Colored Output with GRC.g
GRC is a lightwight and useful syntax highlighter for all sorts of shell commands, including many 3rd-party vendors. You can also make your own if you like.
sudo apt install grc -y
The folders where GRC colorfiles are found, is in this order:
~/.grc/
- This means your custom files would overwrite any others if you set them./usr/local/share/grc/
/usr/share/grc/
There is a standard grc
command and a grcat
for concatenate command are available. You can colorize any command with grc
and grcat
.
### Nginx Shortcuts
```bash
# ╔══════════════════════════════════════════╗
# ║ Nginx ║
# ╚══════════════════════════════════════════╝
function ngensite { sudo ln -s /etc/nginx/sites-available/$1 /etc/nginx/sites-enabled; }
function ngdissite { sudo rm /etc/nginx/sites-enabled/$1; }
alias ngaccess='tail -n 50 /var/log/nginx/access.log'
alias ngdisstartup='sudo service nginx stop && sudo update-rc.d nginx disable && echo "nginx set to NOT autostart"'
alias ngenstartup='sudo service nginx start && sudo update-rc.d nginx enable && echo "nginx set to autostart"'
alias nglog='tail -n 50 /var/log/nginx/error.log'
alias ngls-sites-en='ls /etc/nginx/sites-enabled'
alias ngls-sites='ls /etc/nginx/sites-available'
alias ngpath='cd /etc/nginx/'
alias ngreload='sudo service nginx reload'
alias ngrestart='sudo service nginx restart'
alias ngsites='cd /etc/nginx/sites-available'
alias ngstart='sudo service nginx start'
alias ngstatus='systemctl status nginx.service'
alias ngstop='sudo service nginx stop'
alias ngtest='sudo nginx -t'
Apache2 Shortcuts
# ╔══════════════════════════════════════════╗
# ║ Apache2 ║
# ╚══════════════════════════════════════════╝
alias a2ls-mods-en='ls /etc/apache2/mods-enabled'
alias a2ls-mods='ls /etc/apache2/mods-available'
alias a2ls-sites-en='ls /etc/apache2/sites-enabled'
alias a2ls-sites='ls /etc/apache2/sites-available'
alias a2graceful='sudo /usr/sbin/apachectl -k graceful'
alias a2modules='sudo apachectl -t -D DUMP_MODULES'
alias a2path='cd /etc/apache2/'
alias a2reload='sudo service apache2 reload'
alias a2restart='sudo service apache2 restart'
alias a2start='sudo service apache2 start'
alias a2status='systemctl status apache2.service'
alias a2stop='sudo service apache2 stop'
alias a2test='sudo apachectl -t'
alias a2version='sudo apachectl -v'
alias a2vhosts='sudo apachectl -t -D DUMP_VHOSTS'
alias a2disconf='sudo a2disconf'
alias a2dismod='sudo a2dismod'
alias a2dissite='sudo a2dissite'
alias a2enconf='sudo a2enconf'
alias a2enmod='sudo a2enmod'
alias a2ensite='sudo a2ensite'
alias a2graceful='sudo a2graceful'
alias a2modules='sudo a2modules'
Get Hardware
Get your computers serial number
sudo dmidecode -s system-serial-number
Get BIOS Info
sudo dmidecode -s bios-release-date
sudo dmidecode -s bios-version
# Get All BIOS info
sudo dmidecode -t bios
Get Computer Make/Model and Serial
sudo dmidecode -s system-version
sudo dmidecode -s system-product-name
sudo dmidecode -s system-product-name chassis-type
# Get All SYSTEM info
sudo dmidecode -t system
Get CPU Details
sudo dmidecode -t processor
Get Memory Details
sudo dmidecode -t memory
Get Motherboard Details and Serial
sudo dmidecode -t baseboard
Get All Input Ports
sudo dmidecode -t connector
Check Battery
sudo dmidecode -t 22
sudo apt install acpi
acpi -ib
Get PCI Devices
lspci
lspci -v # Verbose
Get USB Devices
lsusb
lsusb -v # Verbose
Get All Hardware
sudo lshw
Check Hard Drive Partitions
lsblk
Monitoring System
You may have to apt install htop
which is a nicer versio with color.
tophtop
htop
You can also get more information on your computer by going to Applications > System Profiles.