
Why is it useful to know Linux commands?
It’s not very common that when you start working in the world of data science and artificial intelligence, you need to use virtual machines or run commands in a Linux console. Those of us who are statisticians or industrial engineers often don’t learn these kinds of things at university. Nowadays, I think there are quite a few universities with data science departments, but few actually teach practical skills.
This week, during my master’s in Computer Science, we reviewed these kinds of topics, and I thought: why not write a bit about this?
In general, these things are learned once you start working or when you face such problems. In data science, they’re not used daily either, but it’s good to learn them little by little. If you forget a command, just use ChatGPT (it’s good at this).
About five years ago, I made a small decision that, over time, has helped me a lot to practice Linux commands: I partitioned my personal computer. I have one part in Linux and another in Windows. I’m constantly downloading and installing programs, along with running other commands. Nowadays, Windows already has native Linux integration, so there’s no excuse not to learn it.
When I start my computer, the first image I see is:
But will you really use Linux in your job?
In most of your work, you probably won’t. If you work with data — and especially if you work as a Data Scientist — you’ll likely focus on developing and implementing models. Your models will usually be deployed on serverless services such as Cloud Run (Google), Function App (Microsoft), and so on. However, you might eventually have the chance to install your services on a virtual machine or create a proof of concept (POC) in that kind of environment, so it’s never a bad idea to know more about it.
Let’s get our hands dirty.
Here, I’ll list some commands that are useful to know — you don’t need to keep them written down like a Bible. If you forget them, ChatGPT knows them very well.
This tutorial was made based on a VM deployed in Google Cloud. However, all the commands are platform-agnostic (they all work in Linux).
a) Connecting to a Virtual Machine (VM)
One of the most common methods to connect to a virtual machine is through the SSH (Secure Shell) protocol. This method allows you to connect securely and in an encrypted way to another computer. In this case, the first step is to create a folder named .ssh on your computer (using PowerShell), with the following command:
Next, you need to run the following command to create the .ssh key.
On my system, the username is Francisco_macaya. The previous command generates both a public and a private key. If you want to check the key, you should run the following command:
After creating the key, I created an instance (VM) in Google Cloud. Within the instance’s configuration, specifically in the connections section, I added the public key (id_rsa.pub) generated in the previous step, allowing the cloud to certify and validate that my connection is secure.
Finally, the connection to the virtual machine is established using the following command:
b) Current path directory
A good practice after connecting to the VM is to check the terminal’s current directory. Usually, the user doesn’t know exactly where they are located, so it’s recommended to use the pwd (Print Working Directory) command at the beginning. This command will display the directory path where the user is currently located on the computer.
c) User (add and sudo)
Users are an important element in Linux configuration. In general, a good practice when setting up a VM is to create a new user and grant them privileges to install, update, and download programs as well as libraries. The creation of a new user is done as follows:
The previous command will prompt you to enter a password during the creation process, which you can either leave blank or set if you prefer. If the user is created successfully, you’ll return to the terminal. The command to grant the user privileges is:
These privileges mainly allow the user to move between directories and folders without any issues. In addition, the user is authorized to install programs, libraries, and packages within the machine. Since VMs are usually deployed with default configurations, it’s common for clients to need to install several packages or systems before running their services or products.
d) Create a folder, read its files, and move between directories.
The command to create a new directory in a VM is:
Prueba represents the name of the created folder. To navigate to this new directory, use the cd command as shown below:
If you want to return to the original path, you can use the cd command followed by two dots, as shown below:
Finally, if you need to see which files are inside a directory, simply use the ls command.
d) Update and install services
It’s common to need to install programs or packages when deploying your virtual machine. These VMs usually come by default without any preinstalled software such as Docker, Docker Compose, or even the package manager for Python libraries (pip).
As a good practice, it’s recommended to run the following command when you start your VM:
This command updates all the packages on the VM, installing the latest available versions for each of them. Afterwards, if you want to install a program or library, you should use the command sudo apt install followed by the name of the library you wish to install.
e) Create/read/edit/remove text files (.txt)
One way to create a text file is by using the nano editor, which should already be installed. The command is:
The newly created file will open in the console, where you can edit and write text. To save your changes, press CTRL + S. Then, to exit the editor, use CTRL + X.
In addition, you can display the contents of the file directly in the console using the following command:
My thoughts
It’s always better to know. Learning Linux isn’t difficult, and nowadays, ChatGPT is an excellent tool to guide you and help you learn more about its commands. Your focus shouldn’t be on memorizing the commands, but on practicing them — so that if you ever work in these environments, you’ll know how to handle them confidently and without major issues.