Handy Shell Commands for Beginners

Vivek Maskara
May 10 · 1 min read
Via Wikimedia Commons

Check if a file exists

if [ ! -f "/home/pi/test.sh" ]; then
echo "file exists"
fi

Check if a directory exists

if [ -d "/home/pi/test" ]; then
echo "test dir exists"
fi

Install Apt-Get Package

# update pip
# check if package is installed
# install package if not installed
installPackage() {
retval=0
echo "Installing package $1"
if [ $(dpkg-query -W -f='${Status}' $1 2>/dev/null | grep -c "ok installed") -eq 0 ];
then
echo "Package $1 is not installed"
if echo "yes" | sudo apt-get install $1; then
retval=1
fi
else
echo "Package $1 is already installed"
retval=1
fi
return "$retval"
}

Manually Set Date Time

sudo date -s '2019-05-10 09:07:00'

Setup SSH Keys

mkdir -p /home/pi/.sshsudo echo '' | ssh-keygen -t rsa -q -N '' -f /home/pi/.ssh/id_rsa

Turn Off Strict Host Checking For a Domain

echo -e "Host google.com\n\tStrictHostKeyChecking no\n" >> ~/.ssh/config
Vivek Maskara

Written by

Working on awesome stuff at Zeta.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade