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.
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
.
Your output can be different, but will definitely have slashes like this:
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
.
ls
means 'list'.
ls -a
will show us all of our files and directories, including hidden ones.
Go to the home directory
To move between directories, we use the cd
command.
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
means (make directory) command
Try the echo
command
You will then see the output:
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
touch
creates files. man touch
to learn more.
Check that file was created
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.
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 + ~.
Open file.sh
You'll see the tab open in your VS Code.
Edit file.sh
Add the following:
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
You will then see the output:
It works, let's add it to the file.
Run the file
You should see:
Let's fix it by giving it permissions.
Run it again.
You should see:
fastfetch
, cmatrix
, and lolcat
Rather than installing programs one by one, we can install multiple in a single command.
When someone asks:
What are your specs?
We show them this.
Output varies on your OS.
Very cool. Now, let's allow ourselves to feel like hackers.
Press q to quit.
Now, let's allow ourselves to feel like hackers but in RGB.
Press q to quit.
Let's make a cow say "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.
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.
Let's spam cowsay because why not.
Save, and run the file again :)
Bash 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 |
Last updated on