Back-end Engineering Articles

I write and talk about backend stuff like Ruby, Ruby On Rails, Databases, Testing, Architecture / Infrastructure / System Design, Cloud, DevOps, Backgroud Jobs, some JS stuff and more...

Github:
/danielmoralesp
Twitter:
@danielmpbp

2024-08-20

How to Use the Terminal or Console as a Developer?

The three main Operating Systems (Linux, Windows and MacOS) come by default with a software program called “Terminal”, “Console” or “Command Line (CLI)”. These are synonyms to talk about the same concept so when we talk about any of these terms we’re talking about the same thing. 

A console application is a computer program designed to be used via a text-only computer interface, such as a text terminal, the command-line interface of some operating systems (Unix, DOS) or the text-based interface included with most graphical user interface (GUI) operating systems, such as the Windows Console in Microsoft Windows, the Terminal in macOS, and xterm in Unix.

A developer typically interacts with a console application using only a keyboard and display screen, as opposed to GUI applications, which normally require the use of a mouse or other pointing device. Many console applications such as command line interpreters are command line tools, but numerous text-based user interface (TUI) programs also exist.

As the speed and ease-of-use of GUIs applications have improved over time, the use of console applications has greatly diminished, but not disappeared. Some users simply prefer console based applications, while some organizations still rely on existing console applications to handle key data processing tasks.

If you want to know more in detail about the concept and theory behind the Terminal please refer to this link: https://en.wikipedia.org/wiki/Console_application

As always, sometimes the theory seems more complicated than how it really is. So let’s diggest a bit better this concepts

  • - It doesn’t matter what OS do you have, you’ll have always a terminal or console installed on it
  • - Inside the console you can give instructions to your computer. 
  • - You can use your computer UI to create a folder or a file. Also you can use the terminal to create a folder or a file

You can do simple things with the command line, but also you can do complex things with it
  • - Create, edit, access or delete folders and files
  • - Manage files and folders and Move them around the system
  • - Install different kind of software in your machine
  • - Access to internet resources
  • - Execute programs created by a certain programming languages
  • - Create, edit, access or delete system configuration files
  • - Give permissions to the computer users
  • - Create software applications that only lives inside the terminal


Accessing to the terminal

First thing we’ve to do is to access to the terminal, and it depends of the operating system that you are running, let’s try it with Linux and Windows

Accessing from Linux


In the search bar, we have just to type terminal


Click on “Terminal” app and then you’ll see next screen prompted out


That's the terminal. That's everything!

Accessing from Windows


In the search bar, we have just to type CMD which meant “Command Prompt”


Then click on “Command Prompt” and then you’ll see next screen prompted out



Note: you can also open the PowerShell from Windows, and there are many differences between both of them, so please refer to this other blog post for more information and context about it: https://www.educba.com/powershell-vs-command-prompt/

One of the things you have to take into account when running commands from Linux/Unix-like systems or Windows systems is that the commands could be different, because they are running in different OSs



Create, edit, access or delete folders and files

These are the most common use cases when you’re inside the terminal, you need to create, edit, access or delete folders or files, se let’s begin

Note: we’ll be using Linux Terminal. 

$ ls

The first command is ls, which helps us to list all folders and files in the path we’re currently located. Example


Next command is 

$ cd <Name Folder>
$ ls

The first command helps us to go inside a folder and stands for “command directory”. We know now what the second command does



Next commands are:

$ mkdir <NewFolderName>
$ ls



As you can see now we’ve created a new folder inside the Desktop folder (current location), and then we list all the files to see the new folder right there


$ cd TerminalPost
$ ls
## nothing appears here, because we haven’t created anything
$ touch index.rb
$ ls
index.rb



Now, we have started to use the last commands and see how useful they are. First we go to the folder called TerminalPost, then we list files and folders and there are nothing, because we haven’t created anything. With the command touch we can now create a file called index.rb. We’ll be using touch to create files instead of folders. And finally we list the file. 

Note: .rb extension is used to create Ruby files

Manage files and folders and Move them around the system



This is another useful use cases with the command prompt, manage and move files and folders

$ cd ..

The two point at the end of cd helps us to go back in the folder (parent directory) which is also quite useful because are moving now around the system
➜  TerminalPost cd ..
➜  Desktop mkdir TerminalPost2
➜  Desktop mv TerminalPost/index.rb TerminalPost2/
➜  Desktop cd TerminalPost
➜  TerminalPost ls
➜  TerminalPost cd ..
➜  Desktop TerminalPost2 
➜  TerminalPost2 ls
index.rb


Now let’s check what we’d done: basically we’ve moved the file index.rb from TerminalPost/ folder to TerminalPost2/ folder, so let’s explain step by step

  • - Go to the parent folder called Desktop with the command cd ..
  • - Once in the parent folder we’ve created a new folder called TerminalPost2 with the command mkdir
  • - Then we used the new command called mv which stands for Move. The first parameter is the original folder/file name and the second parameter (which is separated by an space) is the destination folder/file name
  • - We go to the original folder with cd and display ls and we didn't find anything, se we successfully moved the index.rb file
  • - Now we check the destination folder called TerminalPost2 and the file named index.rb is now right there

This can be more complicated to understand if you usted to use the User Interface of the Operating Systems, because it is a matter of copy and paste. But now we’re doing everything from the terminal which is something cool, but more than that I recommend to you to try to use the terminal a lot, because (along with the Text Editor) is one of the tools we use single day

Next command is Remove

➜  TerminalPost2 rm index.rb      
➜  TerminalPost2 ls


With rm command now we have removed the index.rb file
➜  TerminalPost2 cd ..
➜  Desktop rm -rf TerminalPost2


Finally we’ve removed the entire folder with whatever is inside with the command rm -rf

As you can see we can do almost anything from the console, it's a matter of knowing basic and advanced commands

Note: if you want to know an exhaustive list of terminal commands, please go to the next link: https://ss64.com/bash/

Install different kind of software in your machine


Now that we know some initial commands, it is fair to say that there are more things to do with the terminal, like installing software in the machine. In previous posts we’ve installed a lot of things from the terminal like Ruby programming language or RubyGems to install gems using commands like these

$ rvm install ruby-2.6.8
$ rvm install ruby-2.7.0
$ rvm install ruby-3.0.3
$ gem install devise

You can see that we’ve been installing this software programs using the reserved word “install” from the terminal


Access to internet resources


The most well known tool to use from the terminal to access a web resource is called CURL. cURL supports HTTPS and performs SSL certificate verification by default when a secure protocol is specified such as HTTPS. When cURL connects to a remote server via HTTPS, it will obtain the remote server certificate, then check against its CA certificate store the validity of the remote server to ensure the remote server is the one it claims to be

For more info about Curl please refer to this page: https://en.wikipedia.org/wiki/CURL


So we can install resources that lives on the web with Curl and a command line similar to this syntax

$ curl -sSL https://get.rvm.io | sudo bash -s stable

And also we can make http requests to an API via curl (later we’ll see APIs in detail)


$ curl -v --location --request GET 'http://mysite.com/api/events' \        
--header 'Content-Type: application/json' \
--header 'Authorization: Token eyJhbGciOiJIUzI1NiJ9.eyJpZCI6MSwiZXhwIjoxNjY0MjEwODgwfQ.k5O0Nvp6gcvcXdjbaitgHk3Hja5Lg70eSHtsUWFNFmA' \
--header 'Cookie: __profilin=p%3Dt'

A bit more complicated, but you’re basically accessing to an external resource (hosted in the web) using curl via your own terminal (all of this without the necessity of a web browser)


Execute programs created by a certain programming languages

In the last blog post called What is a Text Editor for Coding? We learned about the execution of a file (with an extension .rb) that contains ruby code from the terminal. The steps were quite simple


Step #1- Using the Terminal


This is the screenshot of my terminal


Step #2- Execute Ruby code with IRB (as we’ve learned in previous posts)

This is the previous post about IRB. This is the screenshot of my IRB running in the terminal



Step #3- Using a Text Editor

Here you need to create a folder in any place of your local machine, then a file with the .rb extension and then the code you want to execute


Step #4- Executing the code that has been created inside the text editor


But as you can see, the output wasn’t executed (we don't have the output equal to 3), so we have to come back to our terminal, and inside the folder execute the code like so

$ ruby file_name.rb


Now we have the number 3 printed in a different way. However this looks unnecessary but it's not.


Create, edit, access or delete system configuration files


This is one of the most useful things you can do with the terminal, but at the same time you have to take care about it, because you can break your system

For instance, in linux we can go to our main folder doing this

➜  Desktop cd                                                                           
➜  ~ cd ..
➜  /home cd ..
➜  / ls
bin  boot  cdrom  core  dev  etc  home  initrd.img  initrd.img.old  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  snap  srv  sys  tmp  usr  var  vmlinuz  vmlinuz.old



As you can see we executed a couple of times a command that we already known: “.. cd”. With this command we go to the parent folders until we get here “/”. That means that we’re in the rooth directory of our system, so if we delete the wrong folder or file we can broke up our entire machine, so you have to be alert on the command you’re typing here. Just list with “ls” command the folders and then go inside /bin directory… these are all the binaries installed on my system


As you can imagine you could create, edit, delete or access to any of these configuration files


Give permissions to the computer users

This is another useful and cool thing you can do inside the terminal. Normally if you’re the owner of the system you can access almost all files. But if you want to grant access to other users you can do that. The main user in a Unix-like system is called “sudo”. With this word at the beginning of any command you can have access to edit, delete or create some critical files. 

  ~ sudo apt-get install preload

This command, for instance, uses the admin user (sudo) to install a package inside my Linux system. If you try to install it using this command

 ~ apt-get install preload

You'll probably get an error depending on the permissions assigned. So you have to take care of this. Here you can find a good article about how to manage users in Linux: https://www.maketecheasier.com/manager-users-linux-command-line/


Create software applications that only lives inside the terminal


Last but not least, we can create our own programs to live, run and execute actions inside the terminal. This obviously is the most advanced usage of the terminal. For this you have to use a programming language like Ruby or others. By now we’re not going to see this in detail, but you can have a context here: https://www.rubyguides.com/2018/12/ruby-argv/
I hope you learned a lot with this tutorial

Thanks for reading
DanielM