Intro

Once you have built a web application, you will have to set up and configure a server before it can be deployed in a production environment. However, before diving into server configurations, learning how to use the terminal is very important.

Terminal

The terminal is used to execute commands to your server. You can use it to move files, search for text, and install software. It can basically do anything that your normal graphical interface can. It also allows you to automate many things and can accomplish tasks faster as compared to using the graphical interface.

For example, in Windows Explorer, if you wanted to move all files with the letter S to another folder, you might have to find and drag the files multiple times if there are other files in there without the letter S. However, the command line can accomplish this with one line of code.

Other resources

This article will go through all the most common commands that is used in the terminal to help you get familiar with it. However, I would also recommend doing a quick tutorial on this to grasp the basics here: https://www.learnshell.org/

Note:

To access the terminal in Ubuntu, press CTRL + ALT + T.

List files and folders

Command: ls

List the contents of current folder

ls

List the contents of a folder named test

ls test

List the contents of a folder named test3 within another folder

ls test/test2/test3

List all the contents in the current folder

ls -a

List all the contents in a folder named test

ls -a test

Note:

The letter with a dash is called an argument. It lets the computer know to run the command with certain options enabled. Most commands will have the arguments, you can use the “man” command to bring up a manual with a list of Instructions on how to use commands. For example, “man ls”.

Some files and folders won’t be listed by ls without the -a argument. For example, folders that start with a “.” like the “.git” folder.

Changing folders/directories

Command: cd

Change to folder called test

cd test

Change to folder called test3 within multiple folders

cd test/test2/test3

Change to the folder in the previous level

cd ..

Change to your home directory

cd ~

Note:

When you are typing the name of a file or folder, you can press tab and it will autocomplete the name for you. For example, if you have these folder names in your current directory: test, sample, abc, abcdef, example

Then you can type “cd t” and then press tab and it will auto-complete to “cd test/”. You can also type cd “abcd” then tab to autocomplete to the abcdef folder. However, if you have two or more similar named files/folders, then you will have to keep typing until there is only one option. In that case, you can also press tab twice to see the different names of folders.

You can also type “pwd” to check your current directory.

Moving, renaming, creating, removing and copying

Command: mv, cp, touch, mkdir, rm

Move a file called test.txt to the folder test3

mv test.txt test3/

Rename a file called test.txt to test3.txt

mv test.txt test3.txt

Move all files with the letter “s” to a folder called test3

mv *s* test3

Copy a file called text3.txt to a file called text4.txt

cp text3.txt text4.txt

Create a file called test5.txt

touch test5.txt

Delete a file called test5.txt

rm test5.txt

Create a folder called test2

mkdir test2

Remove a folder called test2

rmdir test2

Remove a directory called test2 that contains files/folders

rm -r test2

Note:

Using the rm functions will remove the files from the system, it does not simply move them to the recycle bin.

Output text and values

Command: echo

Output the text “Hello World”

echo "Hello World"

Output the current user

echo $USER

Note:

You can also set variables for your session by. You can type TESTVAR=”hi”, press enter then type echo $TESTVAR to see the result.

Installing a program

Command: apt

Install a program called curl that helps transfer data

sudo apt install curl

Note:

sudo means to run the command with elevated privileges. Commands such as apt install that install software may require more permissions than a normal user.

apt is a package manager for Ubuntu (it is a program that helps install programs). In this case, we are telling apt to install a program called curl. Now that you have curl, you can use it to get website contents. Try typing “curl http://icanhazip.com” to get your ip address. You can also visit this website in your browser to see the results.

Finding text within a file

Command: grep

Find the word “hello” in a file called test.txt

grep "hello" test.txt

Find the world “hello” in a file called test.txt, but case insensitive

grep -i "hello" test.txt

Find the word “hello” in all the files within this folder and its subfolders, and also case insensitive

grep -ri "hello" *

These are some of the most common functions that you will find yourself using when programming. As you install new programs, you will also learn their commands. For example, Git and Rails will have their own functions that you will eventually learn. When you want to learn how to do something with the terminal, you can just Google something like “how to write to file in Bash” or “how to manage permissions with ubuntu terminal” (Bash is the scripting language that we covered here).

Script File

Once you have found all the commands that you want to do for a repetitive task, you can save all those commands together in a file, and tell the the computer to run it as a script. For example, if you wanted a script to automatically install some programs, you can create a new text file called myScript.sh and put the following inside:

#!bin/bash
sudo apt install git
sudo apt install curl
echo "Finished installing!

Then save the file and run it by running this in the terminal

sh myScript.sh

That command should then run the commands from top to bottom. You can also pass different arguments in the sh command for the script to use by adding text to the end. You can access each argument in order with $1, $2, $3, and so on. For example, if your file looked like

#!bin/bash
echo $1
echo $2

You can run “sh myScrip.sh myfirstarg mysecondarg” and watch it print out the arguments. Congratulations, you just created a script!

Note:

Windows also has a command line interface called Powershell or cmd. However, their commands are different and it doesn’t come with a package manager like Ubuntu does.

Navigation

The next article can be found here. Previous article is here.