The three main Operating Systems (Linux, Windows, and macOS) come by default with a software program called "Terminal," "Console," or "Command Line (CLI) Tool." These are synonyms, so we're talking about the same thing when we talk about any of these terms.
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)
This text-based interface is 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 generally require using 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 exist.
As the speed and ease-of-use of GUIs applications have improved over time, the use of console applications has dramatically diminished but not disappeared. Some users prefer console-based applications, while some organizations still rely on existing console applications to handle critical data processing tasks.
As always, sometimes the theory seems more complicated than it really is. So let's diggest a bit better these concepts:
- * It doesn't matter what OS you have; you can access the Terminal or console installed by default on it
- * Inside the console you can give instructions to your computer.
You can do simple things with the command line, but also you can do complex things like these:
- * Create, edit, access, or delete folders and files
- * Manage files and folders and move them around the system
- * Install a different kind of software on your machine
- * Access to internet resources
- * Execute code created by specific 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 the Terminal
We've to do to access the Terminal, and it depends on 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 the "Terminal" app, and then you'll see the following screen prompted out.
That's the Terminal. That's everything!
Accessing from Windows
We have just to type CMD in the search bar, which means "Command Prompt."
Then click on "Command Prompt," and then you'll see the following screen prompted out.
One of the things you have to take into account when running commands from Unix-like systems or Windows systems is that the orders could be different because they are running in other OSs. But, of course, if something goes wrong with your Windows commands, you can always google it!
For this blog post, we'll be using Unix-like commands.
Create, edit, access, or delete folders and files
When you're inside the Terminal, these are the most common use cases; you need to create, edit, access, or delete folders or files, so let's begin.
$ ls
The first command is ls, which helps us list all folders and files currently located. Example:
The following command is cd.
$ cd <FolderName>
$ ls
The first command helps us go inside a folder and stands for "command directory." We know now what the second command does.
The following command is mkdir:
$ mkdir <NewFolderName>
$ ls
mkdir stands for "Make directory."
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.
The following command is touch:
$ cd TerminalPost
$ ls
## nothing appears here, because we haven’t created anything
$ touch index.rb
$ ls
index.rb
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 is nothing because we haven't created anything yet. 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 helpful use case with the command prompt, manage and move files and folders.
$ cd ..
The blank space after cd and the two points at the end of cd helps us to go back in the folder (parent directory), which is also helpful because we are moving now around the system.
$ cd ..
$ mkdir TerminalPost2
$ mv TerminalPost/index.rb TerminalPost2/
$ cd TerminalPost
$ ls
$ cd ..
$ 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 a space) is the destination folder/filename
- * We go to the original folder with cd and display ls, and we didn't find anything, so 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 used 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, something extraordinary. Still, more than that, I recommend you try to use the Terminal more often because (along with the Text Editor) is one of the tools we use every day.
The following command is rm (Remove)
$ rm index.rb
$ ls
With rm command, now we have removed the index.rb file
$ cd ..
$ rm -rf TerminalPost2
Finally, we've removed the entire folder with the command rm -rf with whatever is inside.
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 following link:
https://ss64.com/bash/
Install different kinds of software on 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 stuff 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
We've installed software programs using the keyword "install" from the Terminal; you can see a pattern here. Each time you use this keyword, you're using the Terminal to do that task.
Access to internet resources
The most well-known tool to use from the Terminal to access a web resource is 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
So we can install resources that live on the web with cURL and a command line similar to this syntax.
$ curl -sSL https://get.rvm.io | sudo bash -s stable
With that command, we've accessed the website https://get.rvm.io and downloaded rvm
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 an external resource (hosted in the web) using Curl via your own Terminal (all of this without the necessity of a web browser)
Execute code created by specific programming languages
In the last blog post called What is a Text Editor for Coding? We learned about executing 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)
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 result equal to 3), so we have to come back to our Terminal, and inside the folder, run the code like so
$ ruby file_name.rb
Now we have the number 3 printed differently. However, this looks unnecessary, but it's not. You'll be executing ruby code in this way a lot of times.
Create, edit, access, or delete system configuration files
This is one of the most valuable things you can do with the Terminal, but at the same time, you have to take care of it because you can break your system.
For instance, in Linux, we can go to our main folder.
$ cd
$ cd ..
$ 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 command that we already knew a couple of times: "cd ..". With this command, we go to the parent folders until we get here "/." That means that we're in the root directory of our system, so if we delete the wrong folder or file we can break up our entire machine, so you have to be alert on the command you're typing here. Just list with the "ls" command. This will list the folders and then go inside /bin directory with the cd bin command… these are all the binaries installed on my system.
You could create, edit, delete or access any of these configuration files as you can imagine. So watch your step here!
Give permissions to the computer users
This is another valuable and incredible thing you can do inside the Terminal. Usually, you can access almost all files if you're the system owner. But if you want to grant access to other users, you can also do that. For example, the primary 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, like:
$ sudo apt-get install preload
This command, for instance, uses the admin user (sudo) to install a package inside my Linux system.
$ apt-get install preload
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 bit of context here:
https://www.rubyguides.com/2018/12/ruby-argv/.
I hope you learned a lot from this tutorial.
Thanks for reading
Daniel Morales