Surviving the Black Screen: Essential Linux Commands for Beginners
I know that starting out as a fresher in a tech company, they assign you a project so that you can learn and understand what’s happening, but they tell you to connect to it via SSH to the server, because all their code is there on the server. Now that can be so scary the first time, because come on, all we learned is Python, HTML, CSS, JS and things not Linux commands. I felt this same thing, so I’m trying to share these commands that I think would help you navigate your way through.
The Basics
Well, it’s always good to have a strong understanding of the basic commands before we get into other levels.
1. Understanding your location
First, once we enter the zone, we need to analyze the place we are in, so we’ll start with the commands that help you understand and navigate your location.
- pwd
pwdRunning this command gives you your directory, which is the location where you are right now. Very useful so that you can know your current working directory.
- ls
lsThis is one of the most commonly used commands. Running this command lists out everything that is present in the current working directory. The thing about this command is it can be paired with multiple flags and that would enhance its capabilities.
Common flags:
-
-a: Show all files including hidden ones -
-l: Long format (permissions, size, date) -
-h: Human-readable sizes (KB, MB, GB) -
-t: Sort by modification time -
-r: Reverse order -
cd
cd <directory>This is also one of the most common commands that will be used by you. cd stands for change directory, and is used, as the name suggests, to change the directory you are working in. cd requires the <directory> (the entire folder path), or you can also use shortcuts to route around.
shortcuts for cd
..: To go one level up from where you are../..: To go two levels up from where you are~: To go to the home directory-: To go to the previous directory
Example: If you’re in /home/user/projects/web/frontend, using cd ../.. takes you to /home/user/projects
Pro tip: Use Tab for auto-completion while typing directory names!
2. Modifying your location
- mkdir
mkdir <folder> <folder2>The mkdir, which stands for make directory, is used to make directories (folders). This is one of the useful commands to create folders when you’re connected to the server via terminal. You can create one folder or multiple folders at a time in the working directory.
A cool trick for you You can create a nested directory with a simple flag:
mkdir -p folder1/folder2/folder3This would create folder3 inside folder2, which will be inside folder1. Cool, right?
- rm
rm <file or folder>The rm is used to remove files and folders. One thing that should be remembered while using this command is: Once deleted with rm, files are gone forever.
The rm command can also delete one file or folder, or multiples by spacing the files and folders.
This fellow also has some flags that can be paired and used, but remember they are very dangerous:
-r: Remove directory and all contents (recursive)-f: Force remove without confirmation
Be extremely careful with rm -rf - never run rm -rf / or rm -rf /*!
The Intermediate
Okay, now that we have an understanding of how to get our way through the black screen, it’s time to learn some commands that are useful for engineering.
1. Understanding the specs
It is essential to understand what we are working with. This helps us to be mindful about our resources, like how much storage space we have, what our RAM is, and how much is being used and by what.
- du
du <flag> <directory>The du stands for disk usage, and tells us how much disk space has been used and how much is free. The du command has its own flags that it comes with for different purposes:
-s: which stands for summarize, shows the total for the directory and does not give a breakdown of the sub-directories-h: which stands for human-readable, prints the sizes in the format we understand (KB, MB, GB, etc.)
We use these flags either paired or just one.
du -sh DocumentsWould return something like:
398M DocumentsThis summarizes all the file sizes in the Documents folder as one.
du -h DocumentsWould return something like:
20M Documents/folder1/folder110M Documents/folder1/folder2200M Documents/folder1/folder3.... and so onThe -h flag ensures the sizes are presented in human-readable format.
- free
free -hThe free command is used to understand how much RAM and swap the system is using right now. Running the command would display something like this:
total used free shared buff/cache availableMem: 11Gi 632Mi 7.7Gi 5.6Mi 3.6Gi 11GiSwap: 0B 0B 0BThe -h flag is used to ensure the output is in a human-readable format.
- top
topThe top command is the task manager, it shows a real-time view of system processes and resource usage. Running this would take you to a screen where you can see each and every task and how much resources they are consuming. Once inside the top command, you can also run these commands within the screen:
# Inside top:# q - quit# k - kill process# M - sort by memory# P - sort by CPU# u - filter by userTo come out of the screen, you can use q or press CTRL+C.
- ps
psThe ps stands for Process Status. This command shows the currently running processes. This command comes very handy and is used mostly in real-time. It also has certain flags to pair up with:
-
ps aux: Shows all the processes -
ps -u username: Show processes for a specific user -
ps aux | grep python: Find a specific process (the|symbol pipes the output to grep, which searches for “python”) -
kill
kill <process_id>The kill command is used to kill/stop any running process based on their process ID, which is found by running the ps command. You can pair this command with -9 to force kill any process.
The kill also has another version, pkill, which lets you kill the process by name.
Examples:
pkill python: Kill all Python processespkill "training_script.py": Kill all processes matching the pattern
Similar to -9, you can also use -f with pkill for matching the full command line.
- screen
screen -S screen_nameThis is one of the most majorly used commands as an engineer. When we are working and suddenly we disconnect from the system or anything happens, we need to resume our work, not start over. So putting them in screens is one of the most efficient ways, it’s like isolating your process in a separate place so that it’s running in the background and you can come visit it later.
This command also has flags:
-S: This flag is used to create a new screen with a name-r: This flag is used to reattach or enter the screens that you have created-ls: This command lists out all the screens that are present right now
Important: Once inside a screen, press Ctrl+A then D to detach from it (this lets it keep running in the background). You can then reattach later using screen -r screen_name.
I use screens all the time so that I can run processes like data generation, training and all. It ensures that the process is running and nothing happens even when I disconnect from the system.
All right, I think that’s all for now, I do not want to overwhelm you with too much knowledge. There is a long way to go, but learning these commands and adding them to your toolkit would ensure you have the necessary skills to navigate and understand the black screen, so you can focus more on executing rather than panicking. Until next time. ☕️