cuHacking logocuHacking DevDocs
Knowledge BaseHack the Tunnels

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 directory means 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 man program.

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.
Terminal
man cd

Press 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.

Terminal
pwd

Your output can be different, but will definitely have slashes like this:

/home/yourusername

pwd 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.

Terminal
ls

ls means 'list'.

ls -a will show us all of our files and directories, including hidden ones.

Terminal
ls -a

Go to the home directory

To move between directories, we use the cd command.

Terminal
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.

Terminal
mkdir workspace

mkdir means (make directory) command

Go into the workspace directory

Terminal
cd workspace
Terminal
mkdir hack-the-tunnels-git-workshop

Go into the hack-the-tunnels-git-workshop directory

Terminal
cd hack-the-tunnels-git-workshop

Try the echo command

Terminal
echo "Hack the Tunnels"

You will then see the output:

Hack the Tunnels

This 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.

Create a file

Terminal
touch file.sh

touch creates files. man touch to learn more.

Check that file was created

Terminal
ls -a

You'll now see file.sh show up in the output.

Edit the file

Let's open VS Code in this directory so that we can edit the file.

Terminal
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:

file.sh

Open VS Code terminal

You can open/close the VS Code terminal with ⌘/Ctrl + ~.

Open file.sh

file.sh
code -r file.sh

You'll see the tab open in your VS Code.

Edit file.sh

Add the following:

file.sh
#!/bin/bash

This 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

Terminal
echo "Hack the Tunnels"

You will then see the output:

Hack the Tunnels

It works, let's add it to the file.

file.sh
#!/bin/bash
echo "Hack the Tunnels"

Run the file

Terminal
./file.sh

You should see:

bash: permission denied: ./file.sh

Let's fix it by giving it permissions.

Terminal
sudo chmod +x ./file.sh

Run it again.

Terminal
./file.sh

You should see:

Hack the Tunnels

Make a cow say "Hack the Tunnels"

Terminal
brew install cowsay
Terminal
cowsay "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.

Terminal
brew install fastfetch cmatrix lolcat

When someone asks:

What are your specs?

We show them this.

Terminal
fastfetch

Output varies on your OS.

Fastfetch on MacOS

Very cool. Now, let's allow ourselves to feel like hackers.

Terminal
cmatrix

Press q to quit.

Now, let's allow ourselves to feel like hackers but in RGB.

Terminal
cmatrix | lolcat

Press q to quit.

Let's make a cow say "Hack the Tunnels".

Terminal
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.

Terminal
cowsay "Hack the Tunnels" | lolcat

Change 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.

file.sh
#!/bin/bash
echo "Hack the Tunnels"
cowsay "Hack the Tunnels" | lolcat

Let's spam cowsay because why not.

file.sh
#!/bin/bash
echo "Hack the Tunnels"
cowsay "Hack the Tunnels" | lolcat
cowsay "Hack the Tunnels" | lolcat
cowsay "Hack the Tunnels" | lolcat
cowsay "Hack the Tunnels" | lolcat
cowsay "Hack the Tunnels" | lolcat
cowsay "Hack the Tunnels" | lolcat

Save, and run the file again :)

Terminal
./file.sh

Bash commands review

ActionCommand
Print 'Hello' to the terminalecho 'Hello'
See your usernamewhoami
Print working directorypwd
Create a directorymkdir your-directory-name
Change into directorycd your-directory-name
Go one directory backcd ..
Remove directoryrm -rf your-directory-name
List contents of directoryls
List contents of directory including hidden filesls -a
Create filetouch your-file-name
Remove filerm your-file-name

Last updated on

On this page

Edit on GitHubMade with 🩶 for Hackers by Hackers