Published on

How to Play With Terminal

Authors

When you interact with a computer, you are most likely to use mouse or tract pad, clicking some icons or buttons beautifully displayed on the screen. But if you are a developer, along with the Graphical User Interface (GUI), you also use Command Line Interface (CLI).

With CLI, you won't use any external devices such as mouse and s track pad but only a keyboard because CLI needs only text inputs. In order to use CLI for the interaction with computer, you need to use a tool called terminal. With terminal, you can do anything that you can do with GUI but not vice versa.

In this article, we are going to learn a basic use of terminal.

NOTE: if you are a Windows user, you should use PowerShell instead of terminal

Introduction to Terminal

First, let's open a terminal. Mac already has a built-in terminal, so you can find it easily with Spotlight. On your keyboard, you can press CMD + Space, which opens the Spotlight. In the textbox, you can type as "terminal" and press return key. And the terminal should start on the screen with the terminal window.

open terminal

Commands

In the terminal, you can write commands, which order the computer to perform a specific task. Technically, computer (more specifically the operating system) cannot understand the commands directly but Shell interprets them instead and directs the computer. So in the terminal, you can make the computer perform a specific task with the help of Shell.

To give it a first try, you can use echo command. With this command, you can also give an input of a line of text like giving "Hello".

echo Hello

And the output should be just "Hello". This is fairly easy.

Hello

INFO: The input given to the command is called argument. So echo command takes one argument.

With commands, you can order your computer to perform a specific task, which I think is very cool! So to play more fun stuff with terminal, you need to learn other commands, but don't worry, you don't have to remember thousands of commands. You can just remember a few basic commands and for the rest, you can always google it.

Work with folders and files with terminal

Everything is folder and file

The operating system is made from folders and files. In fact, Desktop (working area where you start when logging in) is also a folder, which means you are most likely to be in the Desktop folder when you are using your computer.

NOTE: Folder is also called Directory, and they are used interchangeably. Both refer to a container that holds files and other containers. Directory is more like technical term and the other is more like non-technical term.

Root Directory and Home Directory

In the file system, there are two special directories called Root Directory and Home Directory. Root Directory is the top most level of the folders. As we learned, the operating system is made from folders and files, so Root Directory might be regarded as the operating system itself. Home Directory is the dedicated sub directory for the user account. Home Directory is allocated to each user so that the user can have their own dedicated environment in the computer. So when the user starts the terminal, it starts from the Home Directory.

Location of folder/file

Location of folder or file is represented with a path, the folder/file name + / like images/animals/cats/cute_cat.jpeg. You will use a path a lot with command, so it is good to learn a little bit about it.

Absolute Path and Relative Path

There are two types of path, which are Absolute Path and Relative Path. Absolute Path is a path that starts from Root Directory. And Relative Path is a path that starts from the current directory.

Here are the examples both for Absolute Path and Relative Path below:

Absolute Path

/Users/user/Desktop/images/animal/cats/cute-cat.jpeg

Relative Path

./Desktop/images/animal/cats/cute-cat.jpeg

Both paths refer to exactly the same location of the image file, cute-cat.jpeg. Only deference is the start point. In the Absolute Path example, the very first slash / refers to the root directory, so from the root directory, it goes to the cute-cat.jpeg. In the Relative Path example, the very fist dot . refers to the current directory, so it goes from the current directory to cute-cat.jpeg.

NOTE: The dot . referring to the current directory can be skipped. So Desktop/images/animal/cats/cute-cat.jpeg is also valid.

What if you want to refer to the location of a parent directory or even a file in the parent directory? Let's say you are in the /Users/user/Desktop/images/animal, and /Users/user/Desktop/images directory also has food directory that has good-food.jpeg, how would you build the relative path? To do so, you can use two dots ...

../food/good-food.jpeg # current directory is /Users/user/Desktop/images/animal

As you might already notice, .. refers to a parent directory, so by starting from .., you can go 1 level up to /Users/user/Desktop/images. and you can goes to food directory.

Commands for folders and files

ls command

When you start terminal, you are in a folder called Home Directory in which the Desktop folder is located. You can see it by using ls command that displays a content of a directory.

ls

And you can see the content of the Home Directory like below:

ls

Applications Desktop     Documents    Downloads    Library   Movies

Music        Pictures     Public

As you can see, you can see the Desktop folder. With ls command, you can also see the inside of a folder by giving the path to the folder as an argument, so let's see the content of the Desktop folder.

ls ./Desktop

file1.txt folder 1

There are two items, which are file1.text and folder 1. And both items are exactly the same as in my Desktop area like below:

desktop content

cd command

When you want to change the current directory and move into a different directory, you can use cd command. So let's go into Desktop directory. You can write cd + path to the directory.

cd ./Desktop

After you executed the cd command, you might notice in the terminal that ~ changed to Desktop. The part indicates the current directory and ~ means Home Directory, which means you were able to change the current directory to Desktop.

If you want to go back to Home Directory, you can specify the parent directory with .. , so the command will be like below:

cd ..

INFO: If you want to go back to home directory quickly, you can execute cd command without any argument. It always works wherever you are.

mkdir command

When you want to create a new folder, you are mostly likely to use right click on your mouse, which opens a small menu where you can click "New Folder". In the CLI world, there is no mouse so you must make the same thing happen with commands as you expect. To make that happen, you can use mkdir command like below:

cd ./Desktop

mkdir test-folder

To demonstrate, we moved into Desktop directory, and there made a new folder called test-folder.

INFO: You can also specify the path to a directory in which a new folder is created. So you can make test-folder in Desktop from the home directory like mkdir ./Desktop/test-folder

touch command

You may also want to make a new file, and you can do so with a command. It is touch command. You can specify the file name with its file extension.

NOTE: You can't add a content into the created file with this touch command. You need to use an editor to edit the file. If you want to edit it within the terminal, you can open it with vi command that opens vim editor.

touch test-text.txt

mv command

You can move a folder/file with a mv command. mv command requires two arguments, which are path to the folder/file and path to the folder to move the file in.

mv test-text.txt ./Desktop

ls ./Desktop

test-text.txt file1.txt folder 1

rm command

rm command lets you remove a folder or file, and you can use it with one argument that is a path to the file or folder. However, this is a little bit tricky command because it needs an additional input that is -r option. Here are the examples below:

remove a file
rm test-text.txt
remove a folder
rm -rf test-folder

Conclusion

We learned very basic things about the terminal and the terminal commands, so now you should be able to play with terminal. There are still so many commands to learn and technical concepts to learn, but don't worry, your friend, Google, always stands by you, so you do not have to remember everything but just google it every time you run into some trouble or confusion.