DEV Community

Cover image for Linux Notes
Rishabh Prakash singh
Rishabh Prakash singh

Posted on • Edited on

Linux Notes

*sudo su - cammand for root access, to become root user. ( Sudo -l )

Folders = directory in linux
Administrator = root user
Software = package

File system hierarchy

Forward slash ( / ) = top level root directory

( Top level root directory - It is the foundation or starting point of the entire storage structure on a device. )

/Root = home directory for root user

/Home = home directory for other user

/Boot = contains bootable files for linux

( POST - Power on self test )

/Etc - contains all configuration files

/Usr - by default software are installed in this directory

/Bin ( full form - binary) = contains commands used by all users including root user.

/Sbin ( full form - System binary ) = contains commands used only by root user

/Opt = optional application software package ( eg - installing chrome in linux )

/Dev = essential device files , includes terminal devices , usb or any device attached to the system ( eg - printer , pen drive )

*Linux commands and it's use cases

*Cat Command -

Uses - 1 - create file - create single file

2 - concatenate file - to add more than 1 file into a single file

3 - Copy files - To copy the content of X file into Y file

4 - Tac - To see content in bottom to top ( neeche se uppar )

( Linux is case sensitive ( use small letters)

To make single file -

cat > file1 ( enter )
Write content ( for exit press cntrl D )

ls ( to view folder )

Cat file1 ( to view file content )

To add content in file -

cat >> file1 ( enter )
Write content

ls ( to view file )

cat file1

To copy data from 1 file to another - cammand -
Cat file1 >file2 ( it will copy file data from file1 to file 2 and delete file 2 data.

For data to be inverted or from bottom to top - cammand -
tac file1

Now to add both file1 and file2 -

cat file1 file2 > all
ls ( enter )
cat all ( enter )
Content of both file1 and file2 visible

Next command for creating file

*Touch command ( main purpose is Timestamp of file )

Uses -

1 - create empty file
2 - create multiple empty file
3 - change all Timestamp of a file ( total 3 )
4 - update only 1 Timestamp out of 3

What is Timestamp - time when files was created modified. 3 types -

1 - access time - last time when a file was access. ( touch -a )

2 - modify time - last time when a file was modified.( Edited , added anything in file ) ( touch -m )

3 - change time - last time when file metadata was changed. ( Like size , properties if it's pdf file , word file any changes in that , location etc )

Cammand - ( stat ) eg - stat file1 ( this cammand is for checking timestamp )

Cammand for creating file using touch cammand -

touch file1 ( for creating single file )

touch file1 file2 file3 ( for creating multiple files )

touch -a ( file name ) for editing access stat
touch -m ( file name ) for editing modify stat

*Vi editor

  • a programmer text editor
  • can be used to edit all kinds of plain text, usefull for editing programs mainly used for Unix programs

:w - to save
:wq or X - to save and quit
:q - quit
:q! - force quit , no save

Vi is available in all linux distro , nano is not

Cammand - vi ( file name you want then press enter )
Type anything for closing the file - ( :wq ) to save and quit

*Nano cammand -

Cammand - nano ( file name then press enter )
Write the content then press - ( cntrl + x ) for quitng editor and then press - shift + Y or capital Y for saving the file. Then press enter for coming to terminal.

Now if we want to edit something in this file using nano or vi ,enter file with cammand - vi or nano file name enter

Cammands -

ls -l ( long list , details of all the files we have created.
ll ( also works ) two times L

ls -a ( for hidden files )

history ( for history of all commands used )

*How to create a directory

mkdir ( name of directory) eg - rishabh folder ( mkdir rishabhfolder)

To make directories inside directories ( folders inside folders ) at once - cammand -

Mkdir - p rishabhfolder 1/rishabh folder2/rishabhfolder3

rishabhfolder1 is the main folder that we will see after typing ls to view , and rishabhfolder2 and rishabhfolder3 are inside rishabhfolder1

(- here the forward slashes are used for separation not indicating root. For root slashes are written at the start. )

Now a new cammand is there to see the folders inside of rishabhfolder1 - cammand Is -

Cd ( meaning change directory)

Eg - cd rishabhfolder1

Now type - ls to see the items inside directory rishabhfolder1
We will see rishabhfolder2 and rishabhfolder3

Again to enter - type cd

Eg - cd rishabhfolder2

Now we will enter rishabhfolder2

To go directly into a folder type all folders name and putting slashe in between -

Eg - folder names are - rishabhfolder1 ,rishabhfolder2 , rishabhfolder3

Cammand - cd rishabhfolder1/rishabhfolder2/rishabhfolder3

Type - ls to see content of this folder.

To get out of this directory ( folder ) - cammand -

cd.. ( to go back to parent directory)

( Dotes batayegi ki kitne or konse folder pe return ayenge )
Eg - cd.. me double dot hai to agar hum rishabhfolder3 me hai to is cammand ke baad rishabhfolder2 me ayenge )

Agar direct bahar aana root user pe then we can also run cammantin one go by knowing the internet which directory we are in to utne dot lagane hai

To know in which directory we are in this cammand is used -

pwd - ( print working directory) used to know the patch ( path kya hai hamara current)

By using this we will know in which directory we are in and help us get of that.

Eg - if we are in rishabhfolder3 then cammand will be -

Cd/../../.. ( enter )

After this we will be on root user.

*How to hide a file -

Just add a DOT before a file or a directory ( folder ).

Eg - touch .rishabhfolder1

To see hidden file or directory - ls -a ( A stands for All )

*How to copy a file -

Cammand - cp

Eg - if want to copy data from 1 file to another -

( Eg two folders names - sherry123 and sherry1234 , copy 1st folder data into another ) ( Note - this will override 2nd folder data with 1st folder)

  • cp sherry123 sherry1234

*How to move ( cut and paste ) a file - ( this happens in case of directory ( folder ) not file )

Cammand - mv

If want to move folder/directory sherry1234 into directory ( folder ) named for example sherry1 then type -

mv sherry1234 sherry1

For moving a file into a directory -

Cammand - ( file123 ( is a file with content written) and folder123 ( is folder / directory where the file is to be moved )

Eg - mv file123 folder123/ newfolder123 ( this is new file renamed file of file123 where the content will be copied and moved into the directory ( folder123 )

*For remaining also same cammand is used -

mv

*How to remove ( delete ) a file or directory -

Cammands -

rmdir - this cammand is used to remove the specified directory ( this cammand only works in empty directory)

rmdir -p - this cammand removes both the parent and child directory ( p - parent )

rmdir -pv - removes all the parent and subdirectories along with verbrose. ( Anything that Is written will also be deleted)

( Add directory or file name in front of the cammands)

Cammands for removing ( delete) files -

Cammands -

rm -rf - removes even non - empty file and directory ( f - stands for "forcefully" ) it will delete all content forcefully

Eg - rm -rf file1

rm - rp - removes non empty directories including parent and subdirectories

rm -r - removes empty directories.

( Here "r" is representing file ) ( Dir - directory)

*To know the difference in directory or file -

Type cammand - ls -l to get long list and you will see the name starting letters ( starting ke letters agr "d" hai to directory hai or "-" hai to file hai )
Refer the 1st image for example

*To see all hidden files details as well type -

ls -al or la ( l = list , a = all )

*Cammands to see content -

less (file name ) - will show all content of 1st page

head ( file name ) - will show first 10 lines of a page

( head -n1 “file name" will print the starting line of that file )

tail ( file name ) - will show last 10 lines of a page

( tail -n 1 - “file name" will print the last line of that file , similarly - tail -n 2 “file name". Will print last 2 lines of that file.

These used mostly in logs to identify problems

more ( file name ) - will show all pages

*How to install packages -

yum cammand doesn't work in modern ubuntu ( after 18 version ) it works in redhat or centos )

yum - ( yellowdog update modify )

New cammand - apt

Like sudo apt

Sudo apt update ( It only updates the package list not the packages )

*To see what can be upgraded -

Cammand - apt list --upgradable

*For upgrading the packages -

Cammand -

sudo apt upgrade ( all the packages will be updated)

For searching in terminal - sudo apt search browser ( eg )

Youtube and chrome type apps are downloaded from official sites as - (.deb ) packages

Through sudo apt install cammand apps present in repositories are only installed

For removing the package first check the 📦 name -

sudo dpkg -l | grep chrome

Below you will see package name , get the name and run this cammand to remove -

Cammand - sudo apt remove ( package name )

After this run this also - sudo apt autoremove ( always)

hostname - tells you your device hostname

hostname -i ( tells you IP address of your device )

ifconfig - / ip a ( tells you information of your system like IP address, ethernet ports , ports etc )

cat /etc/os-release - ( tells you os version and name ) ( This is an absolute path ( path jo confirmed pakka hota hai )

sudo apt search - ( and any reletive package name we want ) it will show results form repository

sudo apt update - ( to see how many packages need update ) ( kind of refresh function like in windows)

sudo apt upgrade - ( updates all the packages in once )

*Running a service - ( eg apache server)

Cammand -

sudo systemctl start apache2 ( or any other service name) ( for start the service)

sudo systemctl status apache2 ( or any other service name ) ( for checking if the service is running or not )

sudo systemctl stop apache2 ( or any other service name ) ( for stopping the service)

sudo systemctl restart apache2 ( or any other service name) ( for restarting the service)

*To configure that the service starts running after booting up the machine everytime -

systemctl enable ( service name ) ( eg apache2 )

systemctl disable ( service name )

*To see the list of all installed apps -

Cammand - apt list --installed

*To see a particular app is installed or not -

Cammand - which

( Eg - which firefox ( enter ) )

*To see tree format patch of all directories and files -

Cammand - tree

( It prints the directory structure in a tree format , starting from the current or specified directory)

( Eg - tree ( enter )
Shows path from parent directory to subdirectories, refer image 2

More cammands for tree -

tree -a ( show files including hidden files )

tree -d ( show only directories)

tree -L 2 ( show show only 2 level deep )

tree -h ( show file sizes )

whoami ( tells you if you are a root user or whatever you are inside the system )

*Cammand to display massage in a file of directory sometimes disclaimer or something else -

Cammand - echo

It can be used to add content also in file

Use " " and write the text in between inverted commas

Eg - echo "rishabhsherry" >file1

When single > is put than the previous content of that file ( here file 1 ) will be overwritten and new content will be added

2 - Eg - echo "rishabhsherry" >>file1

When double >> is used than the content written in between the commas is added with the previous file content ( here file 1 )

3 - eg - echo >file1

This will remove the content of file , when empty cammand is run with echo and > this symbol the content of that file is erased

*Cammand to search a word or pattern inside text or files - grep ( just like ctrl + F in windows)

Cammand - grep

Eg - grep ( word name that you want to find ) ( file name in which you want to find , you have to give a particular path to it to find it )

  • grep sherry file1

*Uses -

  • to find errors in log files
  • search configuration values
  • debug services
  • filter cammand output

*Cammand to sort content in a file alphabetical order - sort

( It is a cammand line utility used to arrange lines of text or numbers in a specified order such alphabetical kr numeric )

Cammand - sort

Eg - sort file1

Cammands -

Sort -n ( numeric)
Sort - r ( reverse)
Sort -u ( unique)

*Cammand to check if a software/ package is installed and where it is located -

Cammand - which

Eg - which tree
( Shows if tree package is installed or not and if Installed it will show path also )

*More cammands -

*How to add a "user in linux" -

Cammand - useradd

Eg - useradd sherry

*To see if the user is created or not -

Cammand - cat /etc/passwd

( At the bottom of code you will see the username)

*How to make a group -

Cammand - groupadd

Eg - groupadd sherry

*To see if the group is created or not -

Cammand - cat /etc/group

( Now point to be noted - when a new user is created it's group is automatically created ( jab kisi user ko create kia jaata hai to uska group bhi automatically ban jaata hai bhale hi uska ya usko kisi group me add nai kia hua hai tab bhi )

( But the same doesn't happen in the case of group creation, when a group is created the user is not automatically created ( jab group banate hai to user automatically create nai hota hai )

*Cammand to add a "single user" or "multiple users" into a group ( users ban hone chahiye pehle se )

Cammand - gpasswd -a ( to add a single user )

Eg - gpasswd -a fraand sherry1 (sherry1 is the group name)

Cammand - gpasswd -M ( with capital M , to add multiple users into a group)

Eg - gpasswd -M fraand, shonty, chote, oreo sherry1 ( sherry1 is the group name)

( Usage: gpasswd [option] GROUP

Options:

-a, -add USER

-d, -delete USER

-h, --help

-Q,--root CHROOT_DIR

-r--remove-password

-R, --restrict

-M,-members USER,...

--extrausers

-A, --administrators ADMIN,.

add USER to GROUP

remove USER from GROUP

display this help message and exit

directory to chroot into

remove the GROUP's password

restrict access to GROUP to its members

set the list of members of GROUP

use the extra users database )

*Cammand to create link or shortlink -

Denoted by "l" when we see files and directories using ls -l or ll

Cammand - ln ( meaning link )

ln - ( hardlink )

ln -s - ( softlink ) ( just like shortcut in windows)

Is also works like a file

Eg - ln -s file1 softfile1

( Here file1 is the main file whose softlink ( shortcut) is to be created and "softfile1" is the name given to the softfile ( shortcut).

*Now hardlink - in hardlink a "Backup" is created of the original file and it is automatically synced with the original file and is updated automatically when we write or add anything in the original file , It helps protects the data if the original file is deleted.

Eg - ln file2 backupfile2

( Here file2 is the original file whose backup we want to take and backupfile2 is the "Backup" we are creating and it will be automatically updated) ( This will not happen if we just copy pasted the file as everytime the data was to be copy pasted but here it will be updated automatically.

The same happens when we add something in backup file also it will be updated in the original file also.

*Cammand to combine multiple files into one ( zip ) - tar ( tape archive)

It is always a "file" not directory

Cammand - tar -cvf

( C = create , v = verbrose , f = forcefully)

( Tar is an archiver used to combine multiple files into one file. ) ( Just Zip files in windows)

Eg - there is directory named - "oreodir" , in that directory there 2 more sub directory named oreodir1 and oreodir2 , now I want to combine all of them and make a tar file to send.

Cammand - tar -cvf oreo.tar oreodir

= Oreo.tar

( tar.cvf ke baad us file ka naam likhna hoga jis naam ki tar ( zip ) file banani hai and " .tar " lagana hai uske saath and then uske baad us directory wagera ka naam likhna hai jiski file ko tar file banana hai ) ( oreo.tar us file ka naam hai jo me dena chahta tha tar file ko )

*Cammand used to reduce size of a file -

Cammand - gzip ( file name )

( Gzip is a compression tool that is used to reduce the size of a file ) ( Just like compressing a file in windows to reduce its size )

Eg - there is a tar file named - "oreodir.tar" and we want to compress it and reduce the size so the cammands will be -

gzip oreodir.tar

Output in ls - oreo.tar.gz

*Now we want to uncompress the file afterwards or the receiver wants to do it -

Cammand - gunzip oreo.tar.gz

Output - oreo.tar

*Now we or the receiver wants to unzip ( extract) the data from " oreo.tar" for accessing the files -

Cammand - tar -xvf oreo.tar ( x = extract)

*Cammand to download a file , software etc on our linux machine -

Cammand - wget < url >

( Url is a must , get the url from browser or anywhere and paste it , then after downloading install using sudo apt install)

( Wget is the non-interactive network downloader )

( It will download the file vo file download karta rahega even if the user is not active and login , or agar internet slow and ya network me issue , network khraab hota hai rehta toot ta rehta hai tab bhi ye try karta rehta hai download karne ka )

*To delete all files at once -

Cammand - rm -rf *

( * Means delete everything ( files and directories but not the installed apps and packages )

*Cammand for permission -

Cammand - chmod

( r = 4 w = 2 x = 1 )

( r = read , w = write , x = execute )

*Linux Permissions and File Details -

In Linux, every file and directory is assigned access modes or permissions that determine who can read, write, or execute them. When using the command ls -l, a detailed list is displayed where the first character indicates the type of item:

  • Represents a file ·d: Represents a directory. ·I: Represents a link.

Following this character are nine others divided into three
sets of three, representinig the Owner (User), the Group, and Others

Core Commands and Definitions

chmod (Change Mode): Used to change the access
permissions of a file or directory.

chown (Change Owner): Used to change the ownership
of a file or directory.

chgrp (Change Group): Used to change the group
ownership.

·Permission Types:

·r (Read): Permission to view the contents (Value: 4).
。w (Write): Permission to modify or delete (Value: 2).
·x (Execute): Permission to run a file or enteer a directory
(Value:1).

first 3 letters represent - root user ,
The next 3 letters represent - group
The last 3 letters represent - other users

Permission | Calculation | Total Value
rwx. 4 + 2 + 1. = 7
rw. 4 + 2 + 0. = 6
r-x 4 + 0 + 1. = 5
r-- 4 + 0 + 0. = 4
--- 0 + 0 + 0. = 0

*Examples -

  • chmod 777 file_name: Grants full read, write, and execute permissions to everyone.

  • chmod 754 file_name: chmod 754 file_name: Owner has full rights (7), Group can read and execute (5), and Others can only read (4).

  • chmod 700 directory_name : Only the owner has full access; everyone else has no permissions.

*Another method to give and remove permissions ( using letters )

u - user / owner
g - group
O - others

Mainly 3 sub methods are used - ( + , - , = )

For updating permissions in a existing file or directory - " = "
Equals sign is used

Eg - u = rwx ( the user will get all permissions

g = w ( the group will get only write permissions)

o = wx ( others will get only write and execute permissions)

Eg - chmod u-wx,g+w,o=wx file1

*Ownership and Group Management -

To change who owns a file or which group it belongs to, use the following commands:

chown rishabh file1: Changes the owner of "file1" to the user "rishabh".

( The owner have all the rights by default)

To change the group

( Group name for example - techops )

chgrp techops file1: Changes the group of "file1" to "techops".

chmod 000 file1: Removes all permissions from every user for "file1".

*new command for “getting things like url and api “ -

curl = Client URL
It is used to:
Fetch data from URLs

Call APIs

Download files

Test web servers

Command - curl

Eg - curl google.com

Most Used curl Options (Cheat Sheet)
Option
Meaning
-O
Save file (original name)
-o
Save file (custom name)
-I
Headers only
-X
Request type (GET/POST/etc.)
-d
Send data
-H
Add header
-L
Follow redirects
-s
Silent mode

Golden Rule to Remember
curl shows output → wget saves files

Linux stores the logs in this directory and path

/var/log

Eg - we want to see the logs of a particular file or directory -

Type - cd /var/log “enter"

Directory will be changed, now to check the files -
ls , pick the log file ( for eg - " syslog” )

See the content inside this file or it's log using nano or vi cammand. To see hidden file type - ls -al

less (file name ) - will show all content of 1st page

head ( file name ) - will show first 10 lines of a page

( head -n1 “file name" will print the starting line of that file )

tail ( file name ) - will show last 10 lines of a page

( tail -n 1 - “file name" will print the last line of that file , similarly - tail -n 2 “file name". Will print last 2 lines of that file.

*Cammand to see the word count of a file -

wc ( eg - wc file1 )

This will show the word count of that file -

1st numbers are - “ how many lines are there"
2nd numbers are - “ how many words are there"
3rd numbers are - “ how many characters are there"

*Important - “. | " - this is called a pipe operator.

Meaning of the Pipe Operator (|) — in Simple Terms
The pipe operator (|) connects two commands so that:
Output of the first command becomes input of the second command
Think of it like a water pipe 🚰:
Water comes out of one pipe

Flows into the next pipe
This is called “piping” as well in linux

Eg - suppose a file named - sherryfile1 , commands are as follows -

Eg - less sherryfile1 | wc
Output - “number of lines” “number of words” “number of characters”

Eg 2 - head -n5 sherryfile1 | wc
Output - 5 “lines” “number of words” “number of characters”

*List command variations -

l - list long listing
a - list all
r - reverse sorting
t - arrange by time

ls -a
ls -r
ls -t

ls -R - ( lists files and directories recursively. )

means:
It shows everything inside a directory, including all subdirectories and their contents.
List the current directory, then go inside each folder, and list everything there too.

Combine with Other Options
Long listing
ls -lR

Show hidden files
ls -aR

Human-readable sizes
ls -lhR

*echo cammand alternative - ( mostly preferred )

printf “\n”

( anything that you want to print write it inside and between “ \n” )

Eg - printf “sherry\n”

Output - sherry

*Cammand used to monitor system performance -

Command - top

top Command in Linux
top is used to monitor system performance in real time.
It shows:
Running processes

CPU usage

Memory usage

System load

Process ID (PID)

It updates automatically every few seconds.

What You See in top
Upper Section (System Summary)
Load average → System workload

Tasks → Running / sleeping processes

%Cpu(s) → CPU usage

KiB Mem / Swap → RAM and swap usage

Lower Section (Process List)
Columns include:
PID → Process ID

USER → Owner

%CPU → CPU usage

%MEM → Memory usage

COMMAND → Program name

Important Interactive Keys (While top is Running)
Press these keys inside top:
Key
Function
q
Quit
k
Kill a process
M
Sort by memory usage
P
Sort by CPU usage
u
Show specific user processes

*Cammand to terminate a running process

Command - kill

kill Command in Linux
The kill command is used to send a signal to a process.
Most commonly, it is used to stop (terminate) a running process.

Variations and uses -

Command
Purpose

kill
Send signal to specific PID

pkill
Kill by process name

killall
Kill all processes by name

Force kill cammand - kill -9 “file name”

*cammand to view all processes on a machine -

Command - ps

ps stands for Process Status.
It is used to view running processes on the system.
Unlike top, it shows a snapshot (not real-time updates).

ps

Ps -a ( displays all processes running in the background as well.

Difference Between ps and top
ps
top
Snapshot
Real-time
Static output
Dynamic updating
Used in scripts
Used for monitoring

*To see any file or process using PID ( process identifier ) number -

Command - ps -p

Example - ps -p 1
Output - shows the 1st PID process , that which process is of PID 1 and it is - systemD

PID 1 is very important in Linux.
It is the first process started by the kernel during boot.
This process is usually:
systemd (in modern Linux systems like Ubuntu)

Or init (in older systems)

PID TTY TIME CMD
1 ? 00:00:02 systemd

Why Is PID 1 Important?
PID 1:
Starts all other services

Manages system processes

Controls service lifecycle

Is responsible for system shutdown and reboot

If PID 1 crashes → system becomes unstable.

*Disk space monitoring cammand

df → Disk free space

-h → Human readable (auto chooses KB, MB, GB)

-B → Force a specific block size

K → Kilobytes

G → Gigabytes

Cammand - df -h

What -h means:
-h = human-readable

Shows sizes in KB, MB, GB instead of bytes

Example - df -h
Output -

Filesystem Size Used Avail Use% Mounted on
/dev/sda1 50G 20G 28G 42% /
tmpfs 2.0G 0 2.0G 0% /dev/shm

Meaning of the above -

1- Filesystem - disk or partition name

2- Size - total size

3 - Used - used space

4 - Avail - free space

5 - Use% - Percentage used

6 - Mounted on - Mount location

What It Is Used For
Check if disk is full

Monitor server storage

Troubleshoot “No space left on device” errors

*cammand - df -h -BK

What it does:
Shows disk size only in Kilobytes (KB)
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda1 52428800 20971520 29360128 42% /

Everything is converted into KB

*cammand - df -h -BG

What it does:
Shows disk size only in Gigabytes (GB)

Example output:

Filesystem 1G-blocks Used Available Use% Mounted on
/dev/sda1 50G 20G 28G 42% /

Command - du ( disk utilisation/usage )
( shows size of all files and folders )

It is used to check how much space files and directories are using.
Unlike df, which shows whole disk usage,
du shows file/folder size.

*cammand - du -h

du → Disk Usage

-h → Human-readable format (KB, MB, GB)

It shows the size of files and directories in the current directory.

Usage and what it does -
Go through the current directory

Show size of each subdirectory

Show total size at the end

*To know all file sizes inside folders as well individual files -

Command - du -ah

du → Disk usage

-a → Show all files and directories

-h → Human readable (KB, MB, GB)

What It Does
It shows the size of:
Every file

Every folder

In the current directory

Also - du -ahc

Shows same results as above just adds a total volume at the end

*free command

Command - free

is used to check RAM and swap memory usage.

It shows:
Total memory

Used memory

Free memory

Available memory

Swap usage

*cammand - free -h

Shows memory in human-readable form ( shows in MB, GB )

          total        used        free      shared  buff/cache   available
Enter fullscreen mode Exit fullscreen mode

Mem: 7.6G 3.2G 1.1G 200M 3.3G 4.0G
Swap: 2.0G 0B 2.0G

*cammand - free -m

Shows memory in MB

*cammand - free -g

Shows memory in GB

*cammand - free -h -t

Adds total line (RAM + Swap combined)

*cammand - free -h -s N -( N is number of seconds you want to put )

*cammand - watch free -h

Updates every 2 seconds.
( for continuous Monitoring )

Swap Meaning
Swap is:
Disk space used as extra RAM

Slower than RAM

If swap usage is high → system may be under memory pressure.

*Linux Shell scripting

First how to check your shell -

Command - echo $0

To make a file run like script ,first the below mentioned command is written at the top when editing file with vi or nano -

!/bin/bash

Then after this write what you want to display after “echo” command.

Example - file is made named - test-scripts

Enter the file using nano or vi

Then write -

#!/bin/bash ( called shebang )

echo hello world

Get out of the file ( :wq )

*the file don’t have execute rights always check using ll

Then - chmod u+x test-scripts

*cammand to run the script -

Rxeg 9

Example - ./test-scripts

Output - hello world

*To identify the files to ot get confused - .sh extension is used

What is a Variable in Shell Scripting?
A variable is:
A name used to store some value.
Think of it like a box with a label.
The label = variable name

What’s inside the box = value

Example - name=Sherry

( no space after = )
You created:
A box called name

It contains the word Sherry

Now if we type - echo $name

( Use $ to print the value )

Output - sherry

The $ means:
“Give me the value inside the box.”

Use $ to print the value

Example 2 -

*now if you want to use any linux cammand as variable so .

Just write it like before but inside brackets

Eg - if we want to use hostname command as a variable

host=$(hostname)

*cammand for taking input from user

Now let’s build a small script which will require user input.

Example -

!bin/bash

echo what is your name?
read name ( when read is written like this that means that’s where the user will put the input and after that only it will move forward )

( and name will store the values )

echo Hi $name how old are you.
read old

*if-else command

What is if-else?
if-else is used to make decisions in a script.
It means:
If something is true → do this
Otherwise → do something else
2️⃣ Basic Structure
if [ condition ]
then
commands
else
commands
fi

Script build using if-else

!/bin/bash

echo entry for flight according to weight of passenger please share your weight>

echo what is your name?
read name

echo Hi $name what is your weight?
read weight

if [ $weight -gt 70 ]

then
echo sorry but we cannot allow you on the flight

else
echo welcome to our plane

fi

*case Command in Bash *

What is case?

case is used to match a value against multiple options and run different commands based on the match.
Think of it like a menu system — "if user picks option 1, do this... if option 2, do that..."
esac = "case" spelled backwards → it closes the case block (like done closes while)

Important Symbols
Symbol Meaning
) ends the pattern
;; ends that option (like break)

  • default case (matches anything) esac ends the whole case block

Example -

!/bin/bash

echo press option of your choice
echo 1=date
echo 2=all files list
echo 3=hostname
echo 4=ip address

read choice

case $choice in
1) date ;;
2)ls -a ;;
3)hostname ;;
4)ifconfig ;;
*)echo invalid input
esac

Example 2 -

!/bin/bash

echo please find the options below

echo 1=time
echo 2=apache status

read choice

case $choice in

1) date ;;
2) systemctl status apache2 ;;
*) echo invalid input
esac

Output -

root@rishabh-VirtualBox:/home/rishabh/scripts# ./case21feb.sh
please find the options below
1=time
2=apache status
1
Wednesday 25 February 2026 06:29:29 AM IST

Comparison Operators
🔹 For Numbers
Operator
Meaning
-eq
equal
-ne
not equal
-gt
greater than
-lt
less than
-ge
greater or equal
-le
less or equal

!/bin/bash

for num in (1..3)
do
echo Number is $num
done
for task in Read Write Blog Video Sleep Eat
do
echo My task is $task
done

Top comments (0)