Basics of Command Line Interface (CLI): Command Structure, Working with Terminal
1. Introduction to the Linux Command Line
Why the Command Line?
Before diving in, let's answer the big question: why even bother with the command line when Linux has some pretty nice graphical interfaces? The command line (Command Line Interface, CLI) is the tool of pros, system administrators, and programmers. With CLI, you can get tasks done faster, automate boring stuff, and even drop some behind-the-scenes magic, all without lifting your hands off the keyboard. CLI is basically a superpower in the Linux world that lets you work efficiently.
Terminals, Shells, and CLI
Before typing your first command, let’s figure out what’s really happening behind the magic of a terminal.
1. Terminal
A terminal is a program that gives you access to the command line. It allows you to interact with the operating system through text. In Linux, popular terminal options include:
- GNOME Terminal (on GNOME).
- Konsole (on KDE).
- xterm (a legend, but kinda old).
- Windows Terminal for WSL.
2. Shell
A shell is software that interprets commands and passes them to the operating system. In Linux, the most popular shell is Bash (Bourne Again Shell). Alternatives include:
- Zsh: an advanced shell for customization geeks.
- Sh: the classic Unix shell (minimalist).
We’ll start with Bash since it’s the standard.
2. Command Structure
A command in CLI has a strict format, and understanding it is the key to success.
Basic Command Format
Each command consists of three parts:
[Command] [Options] [Arguments]
- Command: basically what you are asking the system to do. For example,
ls
(show a list of files). - Options (or flags): modify how the command works. For instance,
-l
(detailed list for thels
command). - Arguments: extra data you pass to the command, like a path to a file or folder.
Example:
ls -l /home
Here:
ls
— the command;-l
— the option for showing a detailed list;/home
— the argument (the folder whose contents we want to view).
3. Navigation and Basic CLI Commands
Let's start with the simplest commands that show how to interact with the system.
1. Who am I? Where am I?
whoami
: displays the current user.$ whoami student
pwd
(print working directory): displays the current directory.$ pwd /home/student
In most Unix/Linux shells, the $
symbol is used as the terminal's prompt. It indicates that the system is ready to accept a command. The $
symbol is used to separate the commands the user needs to input from their output.
2. Let's look around
ls
(list): displays the contents of the current directory.$ ls Documents Downloads Music
Useful options:
-l
: detailed info about files and folders.-a
: shows hidden files.
Example:
ls -la
3. Moving between folders
cd
(change directory): lets you move between directories.$ cd /home $ pwd /home
4. Terminal Hotkeys
The command line offers a bunch of handy keyboard shortcuts:
Ctrl+C
: interrupts the execution of the current command.Ctrl+D
: ends the current session.Tab
: auto-completes a file or folder name. Super useful feature - use it more often.Arrow Up/Down
: scrolls through your last commands.
Pro tip: if you want to feel like a hacker, pressingCtrl+C
while looking dramatically to the side is the ultimate style.
5. Simple command examples: let’s get creating!
Let’s create a simple practical task and solve it using the CLI.
Task:
- Find out the current user.
- Navigate to the home directory.
- See what's there.
- Create a folder
test_folder
. - Check if the folder has been created.
Solution:
# Finding out the current user
whoami
# Navigating to the home directory
cd ~
# Checking what's in the directory
ls
# Creating a folder
mkdir test_folder
# Verifying its existence
ls -l
Steps explanation:
- Using the
whoami
command, we checked which user we're working under. - With
cd ~
, we moved to the home directory. The~
symbol always points to the current user's home folder. - The
ls
command showed the directory's contents before and after creating the folder.
6. Common Mistakes and How to Avoid Them
Working with the command line is always accompanied by mistakes, especially at the beginning. Here are a few common situations:
Spaces in file/folder names. If you're working with names that contain spaces, always enclose them in quotes. For example:
mkdir "My Folder"
Incorrect paths. If you see the message
No such file or directory
, it means you've specified a path that doesn't exist.cd /unknown/path
Typos in commands. The terminal doesn't tolerate errors:
lss
instead ofls
is a different command (or it might not exist at all).
7. Why do you even need this stuff?
CLI is a powerful tool for managing any system. It’s used everywhere: from configuring servers to writing automated scripts. During interviews, people often ask about commands to complete tasks through the terminal, so the knowledge you're getting now will definitely earn its spot in your skillset.
On top of that, CLI will help you get a handle on version control systems like git
, develop server-side applications, and write scripts for automation. It all starts by understanding the basics like command structure, moving between folders, and basic keyboard shortcuts.
Some Practice
Try out the following task:
- Find the current date.
- Create a folder called
logs
. - Inside it, create a file
log.txt
with a record of the current date.
Solution Example
# Get the current date
date
# Create the logs folder
mkdir logs
# Write the date to log.txt
date > logs/log.txt
# Check the contents of the file
cat logs/log.txt
Now you’re ready for the next level — working with the file system and its contents!
GO TO FULL VERSION