LINUX COMMANDS
Operating System
● It is a program loaded in system by a boot program and manages all the application
programs in system.
● The application programs in systems makes use of OS by making requests for
services through API.
● Users can interact with applications via OS through CLI or GUI.
● It provides a consistent way for applications to interact with H/W without the
applications needing to know any details about the hardware.
● As long as the applications accesses the same resources and services, the OS can
service almost any number of applications. This reduces the amount of time and
coding required to develop and debug an application.
2
Operating System
● An operating system is software that
manages all of the hardware resources
associated with your desktop or
laptop.
● The operating system manages the
communication between your software
and your hardware. Without the
operating system (OS), the software
wouldn’t function.
3
Linux Operating System
Linux is a UNIX clone most widely used OS. Its everywhere since 1990’s: It’s in your phones, your thermostats, in your cars,
refrigerators, devices, and televisions. It also runs most of the Internet, all of the world’s top 500 supercomputers, and the
world’s stock exchanges.
It was created by Linus Torvalds from scratch.
It is free and open-source, that means that you can simply change anything in Linux and redistribute it in your own name.
There are several Linux Distributions, commonly called “distros”. A few of them are:
❖Ubuntu Linux
❖Red Hat Enterprise Linux
❖Linux Mint
❖Debian
❖Fedora
❖Puppy Linux
❖Kali Linux
Linux is Mainly used in Servers. About 90% of the Internet is powered by Linux Servers.
4
Linux is fast, secure, and free.
5
The Linux operating system comprises several different pieces:
Bootloader – The software that manages the boot process of your computer. For most users, this will simply be a
splash screen that pops up and eventually goes away to boot into the operating system.
Kernel/shell/terminal – This is the one piece of the whole that is actually called ‘Linux’. The kernel is the core of the
system and manages the CPU, memory, and peripheral devices. The kernel is the lowest level of the OS.
Init system – This is a sub-system that bootstraps the user space and is charged with controlling daemons. One of the
most widely used init systems is systemd. It is the init system that manages the boot process, once the initial booting is
handed over from the bootloader (i.e., GRUB or GR and Unified Bootloader).
Daemons – These are background services (printing, sound, scheduling, etc.) that either start up during boot or after
you log into the desktop.
Graphical server – This is the sub-system that displays the graphics on your monitor. It is commonly referred to as
the X server or just X.
Desktop environment – This is the piece that the users actually interact with. There are many desktop environments
to choose from (GNOME, Cinnamon, Mate, Pantheon, Enlightenment, KDE, Xfce, etc.). Each desktop environment
includes built-in applications (such as file managers, configuration tools, web browsers, and games).
Applications – .Just like Windows and macOS, Linux offers thousands upon thousands of high-quality software titles
that can be easily found and installed. Most modern Linux distributions (more on this below) include App Store-like
tools that centralize and simplify application installation. For example, Ubuntu Linux has the Ubuntu Software Center
6
(a rebrand of GNOME Software) which allows you to quickly search among the thousands of apps and install them
Linux Shell
It’s a CLI (Command Line Interface) in Linux.
Shell is a program that receives commands from the user and gives it to the OS to process and it shows the output.
Linux’s shell is its main part.
Graphical Shells: Manipulation of programs that are based on the graphical user interface (GUI) by permitting for operations
like moving, closing, resizing, and opening windows and switching focus among windows as well. E.g. Ubuntu OS or Windows
OS
Command-line Shell: Various shells could be accessed with the help of a command-line interface by users. A unique program
known as Command prompt in Windows or Terminal in macOS/Linux is offered for typing in the human-understandable
commands like "ls", "cat", etc and after that, it is being run. The result is further shown to the user on the terminal.
7
Linux Commands
8
sudo command
● Short for superuser do, sudo is one of the most popular basic Linux commands that
lets you perform tasks that require administrative or root permissions.
● When using sudo, the system will prompt users to authenticate themselves with a
password. Then, the Linux system will log a timestamp as a tracker. By default, every
root user can run sudo commands for 15 minutes/session.
● If you try to run sudo in the command line without authenticating yourself, the system
will log the activity as a security event.
9
pwd
pwd stands for Print Working Directory. It prints the path of the working directory, starting from the
root. When you first open the terminal, you are in the home directory of your user. To know which
directory you are in, you can use the “pwd” command. It gives us the Absolute Path, which means the
path that starts from the root. The root is the base of the Linux filesystem. It is denoted by a forward
slash( / ). The user directory is usually something like /home/username.
Syntax: pwd
This command has two flags.
pwd -L: Prints the symbolic path.
pwd -P: Prints the actual path.
10
ls
● ls is a Linux shell command that is used to know what files are there in the directory you are in.
Syntax: ls
The ls command supports various options such as:
ls -a: list all files including hidden files. These are files that start with “.”.
ls -s: list the files along with their size.
ls -t: sort the list by time of modification, with the newest at the top.
ls -l: list the files in long format i.e. with an index number, owner name, group name, size, and
permissions.
ls -R lists all the files in the subdirectories.
ls-lh shows the file sizes in easily readable formats, such as MB, GB, and TB.
cd
“cd” is the command used to go to a directory. For example, if you are in the home folder, and you want to go to the
Downloads folder, then you can type in “cd Downloads”. Remember, this command is case sensitive and you have to
type in the name of the folder exactly as it is. But there is a problem with these commands. Imagine you have a folder
named “Raspberry Pi”. In this case, when you type in “cd Raspberry Pi”, the shell will take the second argument of the
command as a different one, so you will get an error saying that the directory does not exist. Here, you can use a
backward slash. That is, you can use “cd Raspberry\ Pi” in this case. Spaces are denoted like this: If you just
type “cd” and press Enter, it takes you to the home directory. To go back from a folder to the folder before that, you can
type “cd ..” . The two dots represent back.
cd command in linux known as change directory command. It is used to change current working directory.
Syntax: cd [directory]
cd /home/username/Movies
Task: Explore the following:
cd ~[username] goes to another user’s home directory.
cd .. moves one directory up.
cd- moves to your previous directory.
12
mkdir
mkdir command in Linux allows the user to create directories (also referred to as folders in some operating
systems). This command can create multiple directories at once as well as set the permissions for the directories.
It is important to note that the user executing this command must have enough permissions to create a directory
in the parent directory, or he/she may receive a ‘permission denied’ error.
Syntax: mkdir [options] [directories]
For Example, if you want to make a directory called “DIY”, then you can type “mkdir DIY”. Remember, as told
before, if you want to create a directory named “DIY Hacking”, then you can type “mkdir DIY\ Hacking”.
The mkdir command accepts many options, such as:
•-p or –parents create a directory between two existing folders. For example, mkdir -p Music/2020/Songs will make the
new “2020” directory.
•-m sets the file permissions. For instance, to create a directory with full read, write, and execute permissions for all
users, enter mkdir -m777 directory_name.
•-v prints a message for each created directory. 13
rmdir/rm
rmdir is the command used for deleting a directory. But, rmdir can only be used to delete an empty directory.
Remember that the user running this command should have sudo privileges in the parent directory.
For example, you want to remove an empty subdirectory named personal1 and its main folder mydir:
rmdir -p mydir/personal1
14
rm
The rm command is used to delete files within a directory. Make sure that the user performing this command has write permissions.
Remember the directory’s location as this will remove the file(s) and you can’t undo it.
Here’s the general syntax:
rm filename
To remove multiple files, enter the following command:
rm filename1 filename2 filename3
Here are some acceptable options you can add:
-i prompts system confirmation before deleting a file.
-f allows the system to remove without a confirmation.
15
-r deletes files and directories recursively.
touch
● The touch command is used to create a file. It can be anything, from an empty txt file
to an empty zip file. For example – “touch new.txt”
● For example, enter the following command to create an HTML file named Web in the
Documents directory:
touch /home/username/Documents/Web.html
“touch new.txt”
16
man
man command in Linux is used to display the user manual of any command that we can run on the terminal. It
provides a detailed view of the command which includes NAME, SYNOPSIS, DESCRIPTION, OPTIONS,
EXIT STATUS, RETURN VALUES, ERRORS, FILES, VERSIONS, EXAMPLES, AUTHORS and SEE
ALSO.
Syntax: man [COMMAND NAME]
For example, you want to access the manual for the ls command:
man ls
cp stands for copy. This command is used to copy files or group of files or directory. It creates an exact image of a
file on a disk with different file name. cp command require at least two filenames in its arguments.
Syntax: cp [OPTION] Source Destination
Task: cp [OPTION] Source-1 Source-2 Source-3 Source-n Directory
cp [OPTION] Directory1 Directory2
●To copy one file from the current directory to another, enter cp followed by the file name and the destination directory.
For example:
cp filename.txt /home/username/Documents
●To copy files to a directory, enter the file names followed by the destination directory:
cp filename1.txt filename2.txt filename3.txt /home/username/Documents
●To copy the content of a file to a new file in the same directory, enter cp followed by the source file and the destination
file:
cp filename1.txt filename2.txt
●To copy an entire directory, pass the -R flag before typing the source directory, followed by the destination directory: 18
mv
mv stands for move. mv is used to move one or more files or directories from one place to another in file system like
UNIX. It has two distinct functions:
(i) It rename a file or folder.
(ii) It moves group of files to different directory.
No additional space is consumed on a disk during renaming.
Syntax: mv [Option] source destination
Task: Explore how to rename a file using mv. Also explore about the various options.
For example, if we want to rename the file “text” to “new”,
we can use “mv text new”
Simply type mv followed by the filename and the destination directory. For example, you want to move filename.txt to
the /home/username/Documents directory:
mv filename.txt /home/username/Documents.
19
echo
The echo command is a built-in utility that displays a line of text or string using the standard output. Here’s the basic syntax:
echo [option] [string]
For example, you can display the text Hostinger Tutorials by entering:
echo “Hostinger Tutorials”
This command supports many options, such as:
-n displays the output without the trailing newline.
-e enables the interpretation of the following backslash escapes:
\a plays sound alert.
\b removes spaces in between a text.
\c produces no further output.
-E displays the default option and disables the interpretation of backslash escapes.
“echo” is a command that helps us move some data, usually text into a file. For example, if you want to create a new text file or add into an already
made text file, then you just need to type in “echo hello, my name is alok >> new.txt”. You do not need to separate the spaces by using the backward
slash here because we put in two triangular brackets when we finish what we need to write. 20
cat
Concatenate, or cat, is one of the most frequently used Linux commands. It lists, combines, and writes file content
to the standard output. To run the cat command, type cat followed by the file name and its extension. For instance:
cat filename.txt.
Here are other ways to use the cat command:
cat > filename.txt creates a new file.
cat filename1.txt filename2.txt > filename3.txt merges filename1.txt and filename2.txt and stores the output
in filename3.txt.
tac filename.txt displays content in reverse order.
21
locate command
The locate command can find a file in the database system.
Moreover, adding the -i argument will turn off case sensitivity, so you can search for a
file even if you don’t remember its exact name.
To look for content that contains two or more words, use an asterisk (*). For example:
locate -i school*not
The command will search for files that contain the words school and note, whether they
use uppercase or lowercase letters.
22
find command
Use the find command to search for files within a specific directory and perform
subsequent operations. Here’s the general syntax:
find [option] [path] [expression]
For example, you want to look for a file called notes.txt within the home directory and its
subfolders:
find /home -name notes.txt
Here are other variations when using find:
find -name filename.txt to find files in the current directory.
23
grep command
Another basic Linux command on the list is grep or global regular expression print. It lets
you find a word by searching through all the texts in a specific file.
Once the grep command finds a match, it prints all lines that contain the specific pattern.
This command helps filter through large log files.
For example, you want to search for the word blue in the notepad.txt file:
grep blue notepad.txt
The command’s output will display lines that contain blue.
24
df command to report the system’s disk space usage, shown in percentage and kilobyte
(KB). Here’s the general syntax:
df [options] [file]
For example, enter the following command if you want to see the current directory’s system
disk space usage in a human-readable format:
df -h
These are some acceptable options to use:
df -m displays information on the file system usage in MBs.
df -k displays file system usage in KBs.
df -T shows the file system type in a new column.
25
head command
The head command allows you to view the first ten lines of a text. Adding an option lets you change
the number of lines shown. The head command is also used to output piped data to the CLI.
Here’s the general syntax:
head [option] [file]
For instance, you want to view the first ten lines of note.txt, located in the current directory:
head note.txt
Below are some options you can add:
-n or –lines prints the first customized number of lines. For example, enter head -n 5 filename.txt to
show the first five lines of filename.txt.
-c or –bytes prints the first customized number of bytes of each file.
-q or –quiet will not print headers specifying the file name.
26
tail command
The tail command displays the last ten lines of a file. It allows users to check whether a file
has new data or to read error messages.
Here’s the general format:
tail [option] [file]
For example, you want to show the last ten lines of the colors.txt file:
tail -n colors.txt
27
diff command
Short for difference, the diff command compares two contents of a file line by line. After analyzing them,
it will display the parts that do not match.
Programmers often use the diff command to alter a program instead of rewriting the entire source code.
Here’s the general format:
diff [option] file1 file2
For example, you want to compare two text files – note.txt and note_update.txt:
diff note.txt note_update.txt
Here are some acceptable options to add:
-c displays the difference between two files in a context form.
-u displays the output without redundant information.
-i makes the diff command case insensitive.
28
ping command
● The ping command is one of the most used basic Linux commands for checking
whether a network or a server is reachable. In addition, it is used to troubleshoot various
connectivity issues.
● Here’s the general format:
ping [option] [hostname_or_IP_address]
● For example, you want to know whether you can connect to Google and measure its
response time:
ping google.com
29
wget command
● The Linux command line lets you download files from the internet using the wget
command. It works in the background without hindering other running processes.
● The wget command retrieves files using HTTP, HTTPS, and FTP protocols. It can
perform recursive downloads, which transfer website parts by following directory
structures and links, creating local versions of the web pages.
● To use it, enter the following command:
wget [option] [url]
● For example, enter the following command to download the latest version
of WordPress:
wget https://wordpress.org/latest.zip
30
uname command
● The uname or unix name command will print detailed information about your Linux
system and hardware. This includes the machine name, operating system, and kernel. To
run this command, simply enter uname into your CLI.
● Here’s the basic syntax:
uname [option]
● These are the acceptable options to use:
-a prints all the system information.
-s prints the kernel name.
-n prints the system’s node hostname.
31
history command
● With history, the system will list up to 500 previously executed commands, allowing
you to reuse them without re-entering. Keep in mind that only users
with sudo privileges can execute this command. How this utility runs also depends on
which Linux shell you use.
● To run it, enter the command below:
history [option]
● This command supports many options, such as:
-c clears the complete history list.
32
apt-get command
● apt-get is a command line tool for handling Advanced Package Tool (APT) libraries in
Linux. It lets you retrieve information and bundles from authenticated sources to
manage, update, remove, and install software and its dependencies.
● Running the apt-get command requires you to use sudo or root privileges.
Here’s the main syntax:
apt-get [options] (command)
● These are the most common commands you can add to apt-get:
• update synchronizes the package files from their sources.
• upgrade installs the latest version of all installed packages.
• check updates the package cache and checks broken dependencies.
33
vi
The VI editor is the most popular and classic text editor in the Linux family. To work on VI editor, you
need to understand its operation modes. They can be divided into two main parts.
Command mode:
● The vi editor opens in this mode, and it only understands commands
● In this mode, you can, move the cursor and cut, copy, paste the text
● This mode also saves the changes you have made to the file
● Commands are case sensitive. You should use the right letter case.
Insert mode:
● This mode is for inserting text in the file.
● You can switch to the Insert mode from the command mode by pressing 'i' on the keyboard
● Once you are in Insert mode, any key would be taken as an input for the file on which you are currently
working.
● To return to the command mode and save the changes you have made you need to press the Esc key
34
vi
Starting the vi editor
To launch the VI Editor -Open the Terminal (CLI) and type
vi <filename_NEW> or <filename_EXISTING>
vi editing commands:
i Insert at cursor (goes into insert mode)
a Write after cursor (goes into insert mode)
u Undo last change
o Open a new line (goes into insert mode)
cw Change word
x Delete character at the cursor
r Replace character
Shift+zz Save the file and quit
:w Save the file but keep it open
:q Quit without saving
:wq Save the file and quit
35
su command
● The switch user or su command allows you to run a program as a different user. It
changes the administrative account in the current log-in session. This command is
especially beneficial for accessing the system through SSH or using the GUI display
manager when the root user is unavailable.
● Here’s the general syntax of the command:
su [options] [username [argument]]
● When executed without any option or argument, the su command runs through root
privileges. It will prompt you to authenticate and use the sudo privileges temporarily.
36
ps command
● The process status or ps command produces a snapshot of all running processes in your system. The static results
are taken from the virtual files in the /proc file system.
● Executing the ps command without an option or argument will list the running processes in the shell along with:
• The unique process ID (PID)
• The type of the terminal (TTY)
• The running time (TIME)
• The command that launches the process (CMD)
● Here are some acceptable options you can use:
• -T displays all processes associated with the current shell session.
• -u username lists processes associated with a specific user.
• -A or -e shows all the running processes.
37
clear
clear is a standard Unix computer operating system command that is used to clear the terminal screen.
This command first looks for a terminal type in the environment and after that, it figures out the
terminfo database for how to clear the screen. And this command will ignore any command-line
parameters that may be present.
Syntax: clear
exit
exit command in linux is used to exit the shell where it is currently running. It takes one more
parameter as [N] and exits the shell with a return of status N. If n is not provided, then it simply returns
the status of last command that is executed.
Syntax: exit [n]
38
date
date command is used to display the system date and time. date command is also used to set date
and time of the system. By default the date command displays the date in the time zone on which
unix/linux operating system is configured.You must be the super-user (root) to change the date
and time.
Syntax: date [OPTION]... [+FORMAT]
Task: Explore following options : -u, -d, -s, -f
39
gedit
Text Editor (gedit) is the default GUI text editor in the Ubuntu operating system. It is UTF-8
compatible and supports most standard text editor features as well as many advanced features. These
include multi language spell checking, extensive support of syntax highlighting, and a large number of
official and third party plugins. While opening gedit via the command line, if a path is not included in
the startup command, gedit will look for the file in the current directory. If the file is not found, gedit
will open a blank file with the file name entered on the command line.
Syntax: gedit [OPTION] [FILE]
Task: Open gedit manual and study thoroughly. Also explore various options.
40
less
“less” command is used to view files instead of opening the file. less command is linux utility which
can be used to read contents of text file one page(one screen) per time. It has faster access because if
file is large, it don’t access complete file, but access it page by page.
Syntax: less filename
Task: Explore various options.
41
free
free displays the total amount of free and used physical and swap memory in the system, as well as the
buffers and caches used by the kernel.
Syntax: free [OPTIONS]
Task: Study the output of these commands thoroughly and explore various options.
https://www.hostinger.in/tutorials/linux-commands
42
Additional Commands:
zip, unzip commands
useradd, userdel commands
mke2fs
43