Skip to content

Instantly share code, notes, and snippets.

@shiftyp
Last active June 24, 2018 21:05
Show Gist options
  • Save shiftyp/bf598d5e47c3458761e8 to your computer and use it in GitHub Desktop.
Save shiftyp/bf598d5e47c3458761e8 to your computer and use it in GitHub Desktop.
#############################
# Some basic shell commands #
#############################
# NOTE: You could run this script file, but you probably don't want to. Copy
# and paste the commands if you want to follow along. Also, we won't cover it
# here, but if at any time you want to quit something in the terminal you can use
# ctrl + c. Now on to the tutorial!
# In a nutshell (see what I did there ;) the shell is a program that allows you to
# interact with the core of your operating system by executing commands. You may
# think that this is an antiquated way to work on a computer given that we have
# graphical interfaces that perform similar functions. There is a reason why
# we continue to use it though. If you have some working knowledge of how to use
# the shell you'll find that many tasks can be performed much more efficiently
# compared to navigating menus and clicking with the mouse.
# There are different shell programs, but the most commonly used and the one that
# you most likely have on your system is called "bash". Some parts of this tutorial
# only relate to bash, so let's check first that you're using bash. If you're on
# Cygwin this may return something else, but the tutorial should still apply.
echo $0
# If it says "bash" then you're good to go.
# First, say hello to yourself
echo 'Hello me!'
# echo is a more useful command than it might seem at first blush. We'll see
# it again later.
# Now that we've said hello, it might be a good idea to figure out where
# we are. When you're working in the shell you're always working in a
# directory on the filesystem. This directory is called your working
# directory, and by default the shell will start in what's called your
# "home" directory. We can tell the shell to print out our current
# working directory by using the pwd command for "print working directory":
pwd
# This should output something like /Users/ryan for example, if you happen
# to be on an OSX machine and your username is "ryan". Let's take a look
# around and see what what's in this directory.
ls
# ls displays a list of files and directories in the current working
# directory. Like many commands it has additional options that determine
# how it behaves. Many commands come with built in manuals, which we can
# access using the man command, followed by the command we want info on.
man ls
# If you see a command somewhere and want more info, check and see if it
# has a man page. Many commands have help that's accessible by passing an
# argument to the command, usually -h or --help. Let's try man --help.
man --help
# Even if a command doesn't have a man page, it may have a help argument
# to display basic usage information. Let's try passing one of the most
# used options for ls, -l for "long form".
ls -l
# the "l" flag tells ls that we want to display the list of files and
# directories in the long form, which shows additional information.
# Let's try another.
ls -a
# This time it probably displayed some additional files and directories
# starting with a ".". These are called dotfiles, and they usually perform
# system functions. They are hidden from ls output normally for that reason.
# We can also combine them together by using both "l" and "a":
ls -la
# Now we can see the dotfiles in the long form. Many commands allow you to
# combine options like this to configure how you want commands to behave.
# So we're in your home directory, and it's traditionally the place on the
# system where you have free reign to put stuff. Since that's the case,
# let's create a new directory to put stuff in. (if you see a stuff directory
# in the ls output above pick a different name).
mkdir stuff
# What just happened? With the shell no output is often good ouput, as some
# commands will only tell you something if there was an error. We did make
# a directory though, so let's check and see.
ls -l
# You should see a stuff directory listed along with your other directories.
# Very cool. So now we have a directory, let's do stuff in it. While we could
# do things to the directory from up here in our home directory, it would be
# easier to do if we changed our working directory to the new one that we
# created. So let's do that.
cd stuff
# Where are we now?
pwd
# Ah, where in the stuff directory. cd changes your working directory, and you
# give it a path relative to where you are currently. Let's try going back and
# forth a few different ways.
cd ../
# Where are we now?
pwd
# We're back in our home directory. Let's go back to stuff, but this time we'll
# specify a path using a dot. This is another way to specify a relative path other
# than just typing in the name of the directory.
cd ./stuff
# Where are we now?
pwd
# We're back in stuff. Let's go back to home one more time, only this time we'll use
# a tilda, which is an alias that the system has for the current user's home directory.
cd ~
# Where are we now?
pwd
# We're back in the home directory. Let's use the tilda again, only this time we'll use
# it to get back to stuff.
cd ~/stuff
# Where are we now?
pwd
# We're back in stuff. Let's stay here for a while and do some stuff. We made a directory,
# but it's lonely in here.
ls -la
# Let's make a file. We make a file by touching it.
touch foo.txt
# And we'll check to see if it's there
ls -l
# Sweet, we made a file. I wonder what's in the file. We can find out by printing it
# using cat, which concatenates the contents of the file to the terminal.
cat foo.txt
# Commands that take a path, like cd, cat, or any of the commands that we'll
# soon cover; can take any valid path on the file system, so we could also
# write the previous command like this since it points to the same file:
cat ../stuff/foo.txt
# So touch made an empty file. How boring. We can fix that though. Remember echo? By
# default echo outputs text to the terminal, but we can tell it to go somewhere else
# by redirecting it using right angle brackets (>). One bracket will overwrite the file,
# while two will append to the file. Let's try it out:
echo "Hello me!" >> foo.txt
# and let's see what's in the file
cat foo.txt
# and let's try it again.
echo "Hello back at ya!" >> foo.txt
# And check the results
cat foo.txt
# This works for any command that has output, not just echo. Let's try it
# with the help output for less, a command we'll use in a second:
less --help > foo.txt
# Notice this time I used one angle bracket, Which will overwrite the file.
cat foo.txt
# And we should see the help for less. This has all sorts of uses, from
# creating files that log the output of long running commands to quickly
# appending a line of text to a configuration file. The one instance where
# I use it all the time is to add directories and files to .gitignore files.
# Sometimes files are too big to view using cat, but there are other commands
# We can use to view files.
# less allows you to scroll and navigate large files. You can scroll through
# files using the up and down arrows, and quit by typing "q". You can also search
# for something in the file by typing "/" followed by your search. You can
# type "n" and "N" to move to the next and previous search results respectively.
# Try searching for "search" in the file.
less foo.txt
# We can also print out only the beginning or end of the files using head
# (for the beginning) or tail (for the end). You can specify how many lines
# you'd like to print using the -n flag. These are used most often to look
# at log files. For example, if your program crashed and you want to check
# the end of the log, you would use tail. Log files can be very large, so
# often this is the best way to get information from them. Tail can also
# be used with the -f flag for "follow" in the case where a file is actively
# being written to.
head -n 35 foo.txt
tail -n 20 foo.txt
# Let's say you were interested in lines that contain a particular word, like
# an error in your log files. To find it we can use a utility called grep
# (don't ask why it's called grep, I don't personally know). Let's use it to
# search the directory for "search". We'll use the "r" flag for recursive even
# though we don't have any subdirectories to search, just so you know how to
# use it if that's the case. Let's search for "search" in the directory.
grep -r "search" ./
# Notice the arguments. First there are the flags, then the string we want to
# search for, then the directory we want to search.
# Just like we can use the right angle brackets to redirect to files, we can use
# left angle bracket (<) to send files into some commands. Let's do this with grep
# to search just the file we're interested in.
grep "search" < foo.txt
# You can also tell grep to print out lines after or before the match using
# the A and B options respectively.
grep -A 1 "search" < foo.txt
# Foo is kind of a silly name for the file. Now that it contains help,
# let's rename the file by moving it. Moving with mv is how you
# rename files or move them from one directory to another.
mv foo.txt help.txt
# And let's check the results
ls -l
# Perfect. mv works on directories as well. What if we wanted
# to make a copy of the file, like a backup while we make changes
# to the original file. Let's use cp to do that.
cp help.txt help.txt.bak
# And check the results
ls -l
# cp works on directories when used with the -r flag, which stands
# for "recursive". We're done with our changes, and we've decided
# that we no longer need the backup file, so let's remove it with rm.
rm help.txt.bak
# And check the results
ls -l
# rm also works on directories when used with the r flag (rm -r) "recursive".
# You may have noticed that commands in the shell are similar to functions
# in other languages; like JavaScript. They take arguments and return
# output. The shell also has variables as well, just like other programming
# langauges. You can access variables by putting a "$" before their name,
# and set them using the export command. There's a built in variable called
# "HOME" that contains the path to your home directory. Let's echo that and
# check the value.
echo $HOME
# There is another built in variable called "PS1" that can be used to configure
# your command prompt, which is the text that appears to the left of your cursor
# in the shell. By default on most systems it doesn't display any useful information.
# On OSX it starts out displaying the bash version along with a "$" to tell you
# where the prompt ends. You customize your prompt by using various escape characters
# ( a "\" followed by a letter is an escape character) to specify what data you'd like
# to appear in the prmpt. You can see what escape characters you can use here:
# http://www.cyberciti.biz/tips/howto-linux-unix-bash-shell-setup-prompt.html
# Let's set up our command prompt to show our current directory and some other useful
# info, although feel free to configure it any way you like. Notice that when I set
# a variable I don't use the "$" before it.
export PS1="\T \u@\h:\w ❯ "
# Exporting the variable will only set it in the current session though. If we want
# it to be this way every time we open up the terminal, we'll need to add it to a
# script that runs each time you do so. It's a dotfile in your home directory called
# ".bashrc". We can quickly add this line to our bashrc using our old friend echo.
# Notice how I use single quotes instead of double quotes for the string. This is so
# we don't have double quotes inside double quotes.
echo 'export PS1="\T \u@\h:\w ❯ "' >> ~/.bashrc
# And let's take a look at our bashrc
cat ~/.bashrc
# Your hostname might be something like "Ryans-Macbook-Pro", which is fine, but if
# you'd like to give your computer another name you can use the 'hostname' command.
# In order to change the hostname we need to run the command as the root user, which
# has the power to do anything on the system. Be very careful running commands as
# root for that reason. We run the command as root using "sudo" before the command.
# It will ask for your user password. It won't show the password as you type.
# I'm going to set my hostname to "lechuck"...a character from my favorite old
# computer game "The Secret of Monkey Island." Don't judge. ;)
sudo hostname lechuck
# You'll need to start a new session if you want to see your new hostname in your
# prompt.
# Now that we're done with doing stuff in ~/stuff, let's remove the directory. To
# do that we'll need to cd back to home, and then rm the directory. You can do both
# in a single command by putting them on the same line with "&&" between them. You
# can think of "&&" like a boolean AND in other languages like JavaScript. It will
# only run the second command if the first command returns succssfully. You can also
# use "&" if you want both commands to run regardless.
cd ../ && rm -r stuff
# We've learned some basic terminal commands that are useful in many situations
# and will make you more productive as you start to remember them and use them
# frequently. So in closing:
echo "You've taken your first step on the path of shell foo. Congrats!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment