Various Commands

Below are some useful commands for those either just learning Ubuntu 14.04 or a resource for various commands. I will continually expand on them.

Directory Exists:
if [ ! -d "##DIRECTORY##" ]; then
fi
Format a Number as two digits even if it is 1:
$(printf %02d $variable)
Increment a Number:
variable=`expr $variable + 1`
Create a new file:
touch ##FILENAME##
Check if a file contains a string:

This will return 0 if it is not found.

$(echo `grep -c '##TextToSearch##' ##FileNameToSearch##`)
Create a link to a file:
ln -s ##OriginalFile## ##LinkedFile##
List all packages installed:
dpkg -l
Remove installed package:
sudo apt-get --purge remove ##PACKAGENAME##
Install Package:
sudo apt-get install ##PACKAGENAME##
Package Exists:

This will return 0 if it is not found.

$(echo `dpkg-query -l | grep -c ##PACKAGE_NAME##`)
Python Package Exists:

This will return 0 if it is not found.

$(echo `pip freeze | grep -c ##PACKAGE_NAME##`)
Get IP Address:
ip addr show
Find a file:
find / -type f -name "##FILENAME##"
Restart Service:
/etc/init.d/postgresql restart
invoke-rc.d postgresql restart
Kill a process:
kill ##PID##
Terminal Loop:

In the terminal let’s say you want to loop and display a value. You can do it like below. I am just printing free memory. But really you can do anything.

while true; do free -m | grep /+ | gawk '{ print $4 }'; sleep 2; done
Switch to root:
sudo su root
Add Text to File:
echo "##TextToAdd##" >> ##FileNameToAddTo##
Add Text Above Other Text:
sed -i 's/^##LookForText##/##TextToAdd##\n&/' ##FileNameToAddTo##
Loop:
until [ $Variable -gt 3 ]
do
done