1. Automating cool things
Why spend 5 minutes doing it manually when you can spend 5 hours automating it?
Shell Navigation
Helpful Notes
- The shell is what we use to send commands directly to the kernel.
- 'Shell' and 'Terminal' are generally the same thing.
- The term
directorymeans the same thing as folder. - The
~symbol represents the home directory in both Linux and Unix (MacOS is UNIX). - Everything in Linux is a file.
- Every command begins with a program name.
- You can learn more about the program using the
manprogram.
You can open your terminal in MacOS by searching for it in Spotlight.
For windows users, do Win+X, I to open the terminal.
- It will probably open in the default Powershell/CMD profile, click the dropdown and switch to Ubuntu.
man cdPress q to quit. You can put almost any program after the man command to learn more about it. You can even do man man.
We recommend typing in the commands by hand rather than copy-pasting, because:
- You'll learn quicker
- You'll be quicker
- You'll retain the information better
- You'll become more comfortable with the terminal
See current location
To see where we currently are, we use pwd.
pwdYour output can be different, but will definitely have slashes like this:
/home/yourusernamepwd means 'print current directory'. This is our current location.
See files in current location
To see what files and directories are around us, we use ls.
lsls means 'list'.
ls -a will show us all of our files and directories, including hidden ones.
ls -aGo to the home directory
To move between directories, we use the cd command.
cd ~cd means 'change directory'. You can use pwd again to see where you are.
Make a directory called workspace
To create directories, we use the mkdir command.
mkdir workspacemkdir means (make directory) command
Go into the workspace directory
cd workspacemkdir hack-the-tunnels-git-workshopTry the echo command
echo "Hack the Tunnels"You will then see the output:
Hack the TunnelsThis command is used to print things, just like print() in Python, and console.log() in JavaScript. Replace the text between the double quotes with anything you like and try again :)
man echo if you want to learn more. You can quit man pages by pressing q.
Edit the file
Let's open VS Code in this directory so that we can edit the file.
code -r .The -r flag means re-use the same VS Code window if one exists. The . means this current directory.
Tip
Using lowercase letters and hyphens with no spaces for file and folder names will make script automation much easier.
Make a Bash Script
At this point, your file tree (Explorer) in VS Code should look like this:
Open VS Code terminal
You can open/close the VS Code terminal with ⌘/Ctrl + ~.
Edit file.sh
Add the following:
#!/bin/bashThis indicates to Linux/Unix that this is a bash script.
If you see a dot next to filename in the tab, that means it's not saved.
You can save the file with ⌘/Ctrl + S. Always ensure to save before moving on.
Check if echo works in the VS Code terminal
echo "Hack the Tunnels"You will then see the output:
Hack the TunnelsIt works, let's add it to the file.
#!/bin/bash
echo "Hack the Tunnels" // [!code highlight]Run the file
./file.shYou should see:
bash: permission denied: ./file.shLet's fix it by giving it permissions.
sudo chmod +x ./file.shRun it again.
./file.shYou should see:
Hack the TunnelsMake a cow say "Hack the Tunnels"
brew install cowsaycowsay "Hack the Tunnels" __________________
< Hack the Tunnels >
------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||fastfetch, cmatrix, and lolcat
Rather than installing programs one by one, we can install multiple in a single command.
brew install fastfetch cmatrix lolcatWhen someone asks:
What are your specs?
We show them this.
fastfetchOutput varies on your OS.

Very cool. Now, let's allow ourselves to feel like hackers.
cmatrixPress q to quit.
Now, let's allow ourselves to feel like hackers but in RGB.
cmatrix | lolcatPress q to quit.
Let's make a cow say "Hack the Tunnels".
cowsay "Hack the Tunnels"Let's make a cow say "Hack the Tunnels" but in a RGB.
Notes
You can hit the up arrow key on your keyboard to see past commands.
cowsay "Hack the Tunnels" | lolcatChange the text inside the quotes and try it a few more times to see what happens :)
Automate away
Notes
Any command you can run in the terminal, you can also write into the file and run.
Let's try cowsay.
#!/bin/bash
echo "Hack the Tunnels"
cowsay "Hack the Tunnels" | lolcat // [!code highlight]Let's spam cowsay because why not.
#!/bin/bash
echo "Hack the Tunnels"
cowsay "Hack the Tunnels" | lolcat // [!code highlight]
cowsay "Hack the Tunnels" | lolcat // [!code highlight]
cowsay "Hack the Tunnels" | lolcat // [!code highlight]
cowsay "Hack the Tunnels" | lolcat // [!code highlight]
cowsay "Hack the Tunnels" | lolcat // [!code highlight]
cowsay "Hack the Tunnels" | lolcat // [!code highlight]Save, and run the file again :)
./file.shBash commands review
| Action | Command |
|---|---|
| Print 'Hello' to the terminal | echo 'Hello' |
| See your username | whoami |
| Print working directory | pwd |
| Create a directory | mkdir your-directory-name |
| Change into directory | cd your-directory-name |
| Go one directory back | cd .. |
| Remove directory | rm -rf your-directory-name |
| List contents of directory | ls |
| List contents of directory including hidden files | ls -a |
| Create file | touch your-file-name |
| Remove file | rm your-file-name |