LINUX_Basic_Commands
LINUX_Basic_Commands
As of writing this, Linux has a worldwide market share of 2.68% on desktops, but over 90% of
all cloud infrastructure and hosting services run in this operating system. For this reason
alone, it is crucial to be familiar with popular Linux commands.
Whether you’re an experienced Sysadmin or a Linux newcomer, you can take advantage of
this guide.
Let’s begin!
What Is a Linux Command?
A Linux command is a program or utility that runs on the command line. A command line is an
interface that accepts lines of text and processes them into instructions for your computer.
Any graphical user interface (GUI) is just an abstraction of command-line programs. For
example, when you close a window by clicking on the “X,” there’s a command running behind
that action.
A flag is a way we can pass options to the command you run. Most Linux commands have a
help page that we can call with the flag -h. Most of the time, flags are optional.
An argument or parameter is the input we give to a command so it can run properly. In most
cases, the argument is a file path, but it can be anything you type in the terminal.
You can invoke flags using hyphens (-) and double hyphens (--), while argument execution
depends on the order in which you pass them to the function.
Now let’s dive into the 40 most-used Linux commands. Many of these have multiple options
you can string to them, so make sure to check out the commands’ manual.
1. ls Command
ls is probably the first command every Linux user typed in their terminal. It allows you to list
the contents of the directory you want (the current directory by default), including files and
other nested directories.
ls
It has many options, so it might be good to get some help by using the --help flag. This flag
returns all the flags you can use with ls.
For example, to colorize the output of the ls command, you can use the following:
ls --color=auto
Now the ls command output is colorized, and you can appreciate the difference between a
directory and a file.
But typing ls with the color flag would be inefficient; that’s why we use the alias command.
2. alias Command
The alias command lets you define temporary aliases in your shell session. When creating
an alias, you instruct your shell to replace a word with a series of commands.
For example, to set ls to have color without typing the --color flag every time, you would
use:
alias ls="ls --color=auto"
As you can see, the alias command takes one key-value pair parameter: alias
NAME="VALUE". Note that the value must be inside quotes.
If you want to list all the aliases you have in your shell session, you can run the alias
command without argument.
alias
As the name suggests, the unalias command aims to remove an alias from the already
defined aliases. To remove the previous ls alias, you can use:
unalias ls
4. pwd Command
The pwd command stands for “print working directory,” and it outputs the absolute path of the
directory you’re in. For example, if your username is “john” and you’re in your Documents
directory, its absolute path would be: /home/john/Documents.
pwd
# My result: /home/kinsta/Documents/linux-commands
5. cd Command
The cd command is highly popular, along with ls. It refers to “change directory” and, as its
name suggests, switches you to the directory you’re trying to access.
For instance, if you’re inside your Documents directory and you’re trying to access one of its
subfolders called Videos, you can enter it by typing:
cd Videos
cd /home/kinsta/Documents/Videos
There are some tricks with the cd command that can save you a lot of time when playing
around with it:
cd
2) Move a level up
cd ..
3) Return to the previous directory
cd -
6. cp Command
It’s so easy to copy files and folders directly in the Linux terminal that sometimes it can
replace conventional file managers.
To use the cp command, just type it along with the source and destination files:
cp file_to_copy.txt new_file.txt
You can also copy entire directories by using the recursive flag:
cp -r dir_to_copy/ new_copy_dir/
7. rm Command
Now that you know how to copy files, it’ll be helpful to know how to remove them.
You can use the rm command to remove files and directories. Be careful while using it,
though, because it’s very difficult (yet not impossible) to recover files deleted this way.
rm file_to_copy.txt
If you want to delete an empty directory, you can use the recursive (-r) flag:
rm -r dir_to_remove/
On the other hand, to remove a directory with content inside of it, you need to use the force (-
f) and recursive flags:
rm -rf dir_with_content_to_remove/
Info
Be careful with this — you can erase a whole day of work by misusing
these two flags!
8. mv Command
You use the mv command to move (or rename) files and directories through your file system.
To use this command, you’d type its name with the source and destination files:
mv source_file destination_folder/
mv command_list.txt commands/
mv /home/kinsta/BestMoviesOfAllTime ./
You also can use mv to rename files while keeping them in the same directory:
mv old_file.txt new_named_file.txt
9. mkdir Command
To create folders in the shell, you use the mkdir command. Just specify the new folder’s
name, ensure it doesn’t exist, and you’re ready to go.
For example, to make a directory to keep all of your images, just type:
mkdir images/
To create subdirectories with a simple command, use the parent (-p) flag:
mkdir -p movies/2004/
Another essential Linux command is man. It displays the manual page of any other command
(as long as it has one).
man man
— The manual page of “man.”
The touch command allows you to update the access and modification times of the specified
files.
For example, I have an old file that was last modified on April 12th:
— Old date.
To change its modification date to the current time, we need to use the -m flag:
touch -m old_file
Now the date matches today’s date (which at the time of writing was August 8th).
— New date
Nonetheless, most of the time, you won’t use touch to modify file dates, but rather to create
new empty files:
touch new_file_name
12. chmod Command
The chmod command lets you change the mode of a file (permissions) quickly. It has a lot of
options available with it.
r (read)
w (write)
x (execute)
One of the most common use cases for chmod is to make a file executable by the user. To do
this, type chmod and the flag +x, followed by the file you want to modify permissions on:
chmod +x script
You use this to make scripts executable, allowing you to run them directly by using the ./
notation.
13. ./ Command
Maybe the ./ notation isn’t a command itself, but it’s worth mentioning in this list. It lets your
shell run an executable file with any interpreter installed in your system directly from the
terminal. No more double-clicking a file in a graphical file manager!
For instance, with this command, you can run a Python script or a program only available in
.run format, like XAMPP. When running an executable, make sure it has executable (x)
permissions, which you can modify with the chmod command.
Here’s a simple Python script and how we would run it with the ./ notation:
#! /usr/bin/python3
# filename: script
for i in range(20):
Here’s how we’d convert the script into an executable and run it:
chmod +x script
./script
The exit command does exactly what its name suggests: With it, you can end a shell
session and, in most cases, automatically close the terminal you’re using:
exit
15. sudo Command
This command stands for “superuser do,” and it lets you act as a superuser or root user while
you’re running a specific command. It’s how Linux protects itself and prevents users from
accidentally modifying the machine’s filesystem or installing inappropriate packages.
Sudo is commonly used to install software or to edit files outside the user’s home directory:
sudo cd /root/
It’ll ask you for the administrator’s password before running the command you typed after it.
As you may guess, the shutdown command lets you power off your machine. However, it
also can be used to halt and reboot it.
To power off your computer immediately (the default is one minute), type:
shutdown now
You can also schedule to turn off your system in a 24-hour format:
shutdown 20:40
shutdown -c
htop is an interactive process viewer that lets you manage your machine’s resources directly
from the terminal. In most cases, it isn’t installed d by default, so make sure to read more
about it on its download page.
htop
— The “htop” interface.
The unzip command allows you to extract the content of a .zip file from the terminal. Once
again, this package may not be installed by default, so make sure you install it with your
package manager.
unzip images.zip
19. apt, yum, pacman commands
No matter which Linux distribution you’re using, it’s likely that you use package managers to
install, update, and remove the software you use every day.
You can access these package managers through the command line, and you’d use one or
another depending on the distro your machine is running.
The following examples will install GIMP, a free and open source software usually available in
most package managers:
The echo command displays defined text in the terminal — it’s that simple:
# Hey kinsta
Cat, short for “concatenate,” lets you create, view, and concatenate files directly from the
terminal. It’s mainly used to preview a file without opening a graphical text editor:
cat long_text_file.txt
22. ps Command
With ps, you can take a look at the processes your current shell session is running. It prints
useful information about the programs you’re running, like process ID, TTY (TeleTYpewriter),
time, and command name.
ps
— The ps command.
In case you want something more interactive, you can use htop.
It’s annoying when a program is unresponsive, and you can’t close it by any means.
Fortunately, the kill command solves this kind of problem.
Simply put, kill sends a TERM or kill signal to a process that terminates it.
You can kill processes by entering either the PID (processes ID) or the program’s binary
name:
kill 533494
kill firefox
Be careful with this command — with kill, you run the risk of accidentally deleting the work
you’ve been doing.
24. ping Command
ping is the most popular networking terminal utility used to test network connectivity. ping
has a ton of options, but in most cases, you’ll use it to request a domain or IP address:
ping google.com
ping 8.8.8.8
vim is a free and open source terminal text editor that’s in used since the ’90s. It lets you edit
plain text files using efficient keybindings.
Some people consider it difficult to use — exiting Vim is one of the most-viewed
StackOverflow questions — but once you get used to it, it becomes your best ally in the
command line.
vim
— The vim text editor.
history
— The history command.
passwd allows you to change the passwords of user accounts. First, it prompts you to enter
your current password, then asks you for a new password and confirmation.
It’s similar to any other change of password you’ve seen elsewhere, but in this case, it’s
directly in your terminal:
passwd
— The passwd command
Be careful while using it — you don’t want to mess up your user password!
The which command outputs the full path of shell commands. If it can’t recognize the given
command, it’ll throw an error.
For example, we can use this to check the binary path for Python and the Brave web browser:
which python
# /usr/bin/python
which brave
# /usr/bin/brave
If you ever wanted a file to be almost impossible to recover, shred can help you with this
task. This command overrides the contents of a file repeatedly, and as a result, the given file
becomes extremely difficult to recover.
Here’s a file with little content in it:
— File to shred.
Now, let’s have shred do its thing by typing the following command:
shred file_to_shred.txt
— Overwritten content.
If you want to delete the file right away, you can use the -u flag:
shred -u file_to_shred.txt
less (opposite of more) is a program that lets you inspect files backward and forward:
less large_text_file.txt
The neat thing about less is that it includes more and vim commands in its interface. If you
need something more interactive than cat, less is a good option.
31. tail Command
Similar to cat, tail prints the contents of a file with one major caveat: It only outputs the last
lines. By default, it prints the last 10 lines, but you can modify that number with -n.
For example, to print the last lines of a large text file, you’d use:
tail long.txt
tail -n 4 long.txt
— tail four lines.
This one is complementary to the tail command. head outputs the first 10 lines of a text file,
but you can set any number of lines you want to display with the -n flag:
head long.txt
head -n 5 long.txt
— The head command.
Grep is one of the most powerful utilities for working with text files. It searches for lines that
match a regular expression and print them:
You can count the number of times the pattern repeats by using the -c flag:
# 2
The whoami command (short for “who am i”) displays the username currently in use:
whoami
# kinsta
You would get the same result by using echo and the environmental variable $USER:
echo $USER
# kinsta
whatis prints a single-line description of any other command, making it a helpful reference:
whatis python
whatis whatis
36. wc Command
Wc stands for “word count,” and as the name suggests, it returns the number of words in a
text file:
wc long.txt
37 lines
207 words
1000 byte-size
The name of the file (long.txt)
wc -w long.txt
207 long.txt
uname(short for “Unix name”) prints the operative system information, which comes in handy
when you know your current Linux version.
Most of the time, you’ll be using the -a (–all) flag, since the default output isn’t that useful:
uname
# Linux
uname -a
Neofetch is a CLI (command-line interface) tool that displays information about your system
— like kernel version, shell, and hardware — next to an ASCII logo of your Linux distro:
neofetch
In most machines, this command isn’t available by default, so make sure to install it with your
package manager first.
39. find Command
The find command searches for files in a directory hierarchy based on a regex expression.
To use it, follow the syntax below:
To search for a file named long.txt in the current directory, enter this:
To search for files that end with a .py (Python) extension, you can use the following
command:
wget (World Wide Web get) is a utility to retrieve content from the internet. It has one of the
largest collections of flags out there.
Here’s how you would download a Python file from a GitHub repo:
wget https://raw.githubusercontent.com/DaniDiazTech/Object-Oriented-Progra
There’s actually a series of basic commands that are perfect for anyone who is getting started
with Linux:
There are thousands of commands (and new ones are being written daily). But don’t worry:
there’s no need to remember any of them. You can always search for them online.
It’s possible. You can find solid resources online to help you get started. But if you feel in
need of a hand, here are some well-recommended courses:
Linux Mastery
The Linux Command Line Bootcamp
Learn The Linux Command Line (free)
Summary
It can take some time to learn Linux, but once you master some of its tools, it becomes your
best ally, and you won’t regret choosing it as your daily driver.
One of the remarkable things about Linux is that even if you’re an experienced user, you’ll
never stop learning to be more productive using it.
There are a lot more helpful Linux commands. If we’ve left something out, please share your
favorite Linux commands in the comments below!