Professional Documents
Culture Documents
What is Kernel
The kernel is a computer program that is the core of a computer’s operating system,
with complete control over everything in the system. It manages following resources
of the Linux system –
what is Shell
A shell is special user program which provide an interface to user to use operating
system services. Shell accept human readable commands from user and convert
them into something which kernel can understand. It is a command language
interpreter that execute commands read from input devices such as keyboards or
from files. The shell gets started when the user logs in or start the terminal.
linux shell
Shell is
a)Command Line Shell
Shell can be accessed by user using a command line interface. A special program
called Terminal in linux/macOS or Command Prompt in Windows OS is provided
to type in the human readable commands such as “cat”, “ls” etc. and then it is being
execute. The result is then displayed on the terminal to the user. A terminal in
Ubuntu 16.4 system looks like this –
keywords are the words whose meaning has already been explained to the shell.the keywords
cannot be used as variable names because of it is a reserved words with containing reserved
meaning.
fi else while do
A shell variable is a variable that is available only to the current shell. In contrast, an
environment variable is available system wide and can be used by other applications on the
system. A shell is the operating system's command interpreter.
ist of the commonly used variables in Linux
Set X display
DISPLAY echo $DISPLAY export DISPLAY=:0.1
name
export EDITOR=/usr/bin/vim
printenv command – Print all or part of
environment.
Set name env command – Display all exported
EDITOR of default environment or run a program in a
text editor. modified environment.
set command – List the name and value of each
shell variable.
Linux operator
Each shell supports various basic operations. The most popular shell is the Bourne Again
Shell, or bash, which comes as a default with most Linux distributions.
There are five basic operations that one must know to use the bash shell:
1. Arithmetic Operators
2. Relational Operators
3. Boolean Operators
4. Bitwise Operators
5. File Test Operators
1) Arithmetic operators
2
3
4
5
6
7
8
9
a=4
b=5
echo "a + b = $((a + b))"
echo "a - b = $((a - b))"
Run
2) Relational operators
Bash supports relational operators that work on variables with numeric values or strings
that are numeric. Relational operators do not work on strings if their values are not numeric.
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
a=100
b=10
if(( $a == $b ))
then
echo "a and b are equal"
fi
if(( $a != $b ))
then
echo "a and b are not equal"
fi
if(( $a > $b ))
then
echo "a is greater than
b" else
echo "a is not greater than b"
fi
if(( $a >= $b ))
then
echo "a is greater or equal to than
b" else
echo "a is not greater or equal to than b"
fi
Run
3) Boolean operators
Run
4) Bitwise operators
Run
Bash provides operators to test for various properties of files; these operators are known
as file test operators.
test.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
read -p "Enter filename " FileName
if [ -e $FileName ]
then
echo "$FileName exists"
if [ -r $FileName ]
then
echo "$FileName has read access"
else
echo "$FileName does not have read access!"
fi
if [ -w $FileName ]
then
echo "$FileName has write access"
else
echo "$FileName does not have write access!"
fi
if [ -x $FileName
] then
echo "$FileName has execute access"
else
echo "$FileName does not have execute access!"
fi
if [ -s $FileName ]
then
echo "$FileName size is non-zero"
else
echo "$FileName is empty"
fi
echo command in linux is used to display line of text/string that are passed as an argument .
This is a built in command that is mostly used in shell scripts and batch files to output status
text to the screen or a file.
Syntax :
echo [option] [string]
Displaying a text/string :
Syntax :
echo [string]
Example :
Options of echo command
NOTE :- -e here enables the interpretation of backslash escapes
1. \b :
it removes all the spaces in between the text
Example :
2. \c :
suppress trailing new line with backspace interpretor ‘-e‘ to continue without emitting
new line.
Example :
echo -e "Geeks \cfor Geeks"
In above example, text after \c is not printed and omitted trailing new line.
a) read command in Linux with Examples
in Linux system is used to read from a file descriptor. Basically, this command read up the
total number of bytes from the specified file descriptor into the buffer. If the number or
count is zero then this command may detect the errors. But on success, it returns the
number of bytes read. Zero indicates the end of the file. If some errors found then it returns
-1.
Syntax:
read
Examples:
In the following example we are acquiring the user’s name and then showing the user’s
name with a greeting.
echo "what is your name..?";read name;echo "hello $name"
eval command in Linux with Examples
eval is a built-in Linux command which is used to execute arguments as a shell command. It
combines arguments into a single string and uses it as an input to the shell and execute the
commands.
Syntax
eval [arg ...]
Example: In the below figure, you can see that cd Desktop command is stored in a variable
“CD” as a shell command. Now you can use this “CD” as an argument with eval command.
tput command is used to query the terminfo terminal database and check if that terminal
supports a specific feature.
tput command accepts the terminal commands and output the control code sequences for that
terminal. Using tput you can control the color and cursor of your terminal as explained in this
article.
$ tput cup 2 3
$ tput clear
$ tput cols
$ tput lines
4. Execute Multiple tput Commands
tput allows you to run set of commands in a single time. For example, if you want to clear the
screen and set cursor to a particular position, do the following.
$ tput -S <<END
> clear
> cup 2 4
> END
$ tput setb 4
$ tput setf 4
Note: If you set foreground and background to the same color you cannot see the cursor. So,
to reset, execute “tput reset”.
$ tput bold
When you turn it off, new text in the terminal returns to normal display.
$ tput sgr0
In the below example, it bolds the particular text ‘guide’ by turning on and turning off
highlighting accordingly.
guide
$ tput smul
$ tput rmul
Control instruction
There are three components that we need to understand in any structured programming
methodology:
1. Sequential execution
2. Conditional execution
3. Looping constructs
Sequential Execution
Sequential execution means that each command in a program script executes in the order in
which it is listed in the program. The first command in the sequence executes first and when
it is complete, the second command executes, and so on. The presence of functions in the
code does not negate sequential execution; we can still follow the sequential flow of the
instructions. As technology advances there is more parallel programming being run on
smaller computers, but how that impacts sequential execution is beyond the scope of this
course.
Conditional Execution
Conditional Statements: There are a total of five conditional statements which can be
used in bash programming.
1. if statement
2. if-else statement
3. if..elif..else..fi statement (Else If ladder)
4. if..then..else..if..then..fi..fi..(Nested if)
5. switch statement
1) if statement
if [ expression ] then
statement
fi
if [ $a == $b ] then
#If they are equal then print this echo "a is equal to b"
else
#else print this
echo "a is not equal to b"
fi
2) if-else statement
If specified condition is not true in if part then else part will be executed.
Syntax:
if [ expression ] then
statement1 else
statement2
fi
3) _Switch Statement
A case statement works as a switch statement, if specified values match with the pattern then
it will execute a block of that particular pattern. When a match is found all of the associated
statements until the double semicolon (;;) are executed. A case will be terminated when the
last command is executed. If there is no match, the exit status of the case is zero.
Syntax:
case in
Pattern 1) Statement 1;; Pattern n) Statement n;;
To use multiple conditions in one if-else block, then the elif keyword is used in shell. If
expression1 is true then it executes statement 1 and 2, and this process continues. If none of
the conditions is true then it processes else part.
Syntax:
if [ expression1 ] then
statement1 statement2
.
.
elif [ expression2 ] then
statement3
statement4
.
.
else
statement5
fi
chapter :-4 control structure
Conditional Statements: There are total 5 conditional statements which can be used in
bash programming
1. if statement
2.if-else statement
4 .if..then..else..if..then..fi..fi..(Nested if)
5 switch statement
1) if statement
Syntax:
if [ expression ]
then
statement
fi
2) if-else statement
If specified condition is not true in if part then else part will be execute.
Syntax
if [ expression ]
then
statement1
else
statement2
fi
Syntax
if [ expression1 ]
then
statement1
statement2
.
.
elif [ expression2 ]
then
statement3
statement4
.
.
else
statement5
fi
4) if..then..else..if..then..fi..fi..(Nested if)
Nested if-else block can be used when, one condition is satisfies then it again checks another
condition. In the syntax, if expression1 is false then it processes else part, and again
expression2 will be check.
Syntax:
if [ expression1 ]
then
statement1
statement2
.
else
if [ expression2 ]
then
statement3
.
fi
fi
5) switch statement
case statement works as a switch statement if specified value match with the pattern then it
will execute a block of that particular pattern
When a match is found all of the associated statements until the double semicolon (;;) is
executed.
A case will be terminated when the last command is executed.
If there is no match, the exit status of the case is zero.
Syntax:
case in
Pattern 1) Statement 1;;
Pattern n) Statement n;;
Esac
Example Programs
Example 1:
Implementing if statement
#Initializing two variables
a=10
b=20
if [ $a == $b ]
then
fi
if [ $a != $b ]
then
fi
Output
$bash -f main.sh
a is not equal to b
Example 2:
Implementing if.else statement
#Initializing two variables
a=20
b=20
if [ $a == $b ]
then
else
fi
Output
$bash -f main.sh
a is equal to b
Example 3:
Implementing switch statement
CARS="bmw"
#case 1
#case 2
#case 3
esac
Output
$bash -f main.sh
Headquarters - Chennai, Tamil Nadu, India.
1. while statement
2. for statement
3. until statement
To alter the flow of loop statements, two commands are used they are,
1. break
2. continue
Their descriptions and syntax are as follows:
1) while statement
Here command is evaluated and based on the result loop will executed, if command
raise to false then loop will be terminated
Syntax
2) for statement
The for loop operate on lists of items. It repeats a set of commands for every item in a
list.
Here var is the name of a variable and word1 to wordN are sequences of characters
separated by spaces (words). Each time the for loop executes, the value of the variable
var is set to the next word in the list of words, word1 to wordN.
Syntax
c) until statement
The until loop is executed as many as times the condition/command
evaluates to false. The loop terminates when the condition/command
becomes true.
Syntax
Example Programs
Example 1:
Implementing for loop with break statement
php
for a in 1 2 3 4 5 6 7 8 9 10
do
if [ $a == 5 ]
then
break
fi
done
Output
$bash -f main.sh
Iteration no 1
Iteration no 2
Iteration no 3
Iteration no 4
Example 2:
Implementing for loop with continue statement
php
for a in 1 2 3 4 5 6 7 8 9 10
do
if [ $a == 5 ]
then
continue
fi
done
Output
$bash -f main.sh
Iteration no 1
Iteration no 2
Iteration no 3
Iteration no 4
Iteration no 6
Iteration no 7
Iteration no 8
Iteration no 9
Iteration no 10
Example 3:
Implementing while loop
php
a=0
10 while [ $a -lt 10 ]
do
echo $a
a=`expr $a + 1`
done
Output:
$bash -f main.sh
0
1
2
3
4
5
6
7
8
9
Example 4:
Implementing until loop
php
a=0
10 until [ $a -gt 10 ]
do
echo $a
a=`expr $a + 1`
done
Output:
$bash -f main.sh
0
1
2
3
4
5
6
7
8
9
10
Note: Shell scripting is a case-sensitive language, which means proper syntax has to be
followed while writing the scripts.
Example 5:
# the for loop continues until it reads all the values from
the COLORS
do
done
Output:
$bash -f main.sh
COLOR: red
COLOR: green
COLOR: blue
Example 6:
We can even access the positional parameters using loops
We execute a shell script named sample.sh using three parameters
Script Execution: main.sh sample1 sample2 sample3We can access above three parameters
using $@
for SAMPLE in $@
do
done
Output:
$bash -f main.sh sample1 sample2 sample3
Executing script
The given sample is sample1
The given sample is sample2
The given sample is sample3
Example 7: Infinite loop
PHP
while true
do
# Command to be executed
sleep 1
done
Output:
$bash -f main.sh
Hi, I am infinity loop
Hi, I am infinity loop
Hi, I am infinity loop
.
.
.
.
It continues
Example 8: Checking for user input
PHP
CORRECT=n
do
CORRECT done
Output:
$bash -f main.sh
Enter your name:Ironman
Is Ironman correct? n
Enter your name:Spiderman
Is Spiderman correct? Y
Chapter :-5 disk management
Disk formatting:-
is a process to configure the data-storage devices such as hard-drive, floppy disk and flash
drive when we are going to use them for the very first time or we can say initial usage. Disk
formatting is usually required when new operating system is going to be used by the user. It
is also done when there is space issue and we require additional space for the storage of
more data in the drives. When we format the disk then the existing files within the disk is
also erased.
We can perform disk formatting on both magnetic platter hard-drives and solid-state drives.
When we are going to use hard-drive for initial use it is going to search for virus. It can
scan for virus and repair the bad sectors within the drive. Disk formatting has also the
capability to erase the bad applications and various sophisticated viruses.
As we know that disk formatting deletes data and removes all the programs installed with
in the drive. So it can be done with caution. We must have the backup of all the data and
applications which we require. No-doubt disk formatting requires time. But the frequent
formatting of the disk decreases the life of the hard-drive.
1. Low-level Formatting :-
Low level formatting is a type of physical formatting. In is the process of marking of
cylinders and tracks of the blank hard-disk. After this there is the division of tracks into
sectors with the sector markers. Now-a-days low-level formatting is performed by the hard-
disk manufactures themselves.
We have data in our hard-disks and when we perform low-level formatting in the presence
of data in the hard-disk all the data have been erased and it is impossible to recover that
data. Some users make such a format that they can avoid their privacy leakage. Otherwise
low-level will cause damage to hard-disk shortens the service-life.
Therefore, this formatting is not suggested to users.
2. Partitioning :-
Firstly High-level formatting clears the data on hard-disk, then it will generate boot
information, the it will initialize FAT after this it will go for label logical bad sectors when
partition has existed.
Formatting done by the user is the high-level formatting.
Generally, It does not harm the hard-disk.It can be done easily with the Administrator,
Windows snap-in Disk Management tool, diskpart, etc.
We can use such a format to use such a format to fix some problems like errors in the file
system, corrupted hard-drive and develop bad sectors.
How to making a file system
File System Types
Creating a file system writes information to the device and creates order of the empty space.
This file system–related data consumes a small percentage of the space. The remaining space
on the disk drive is split into small, consistently sized segments called blocks. Linux supports
a number of file system types, some of which are described as follows.
Filesystem Description
vfat MS-DOS file system useful when sharing files between Windows
and Linux
The command to build a Linux file system on a device, or hard disk partition, is mkfs. The
syntax for the command is:
Configuration File
A number of options are available to customize block size, fragment size, blocks per group,
journal options, number of inodes, and other parameters. Without including any options,
the defaults that are specified in the /etc/mke2fs.conf configuration file are used.
File systems on different partitions and removable devices, such as CDs, DVDs, or USB
flash drives, must be attached to the directory hierarchy to be accessed. To attach a partition
or device, a mount point must be created. A mount point is simply a directory created with
the mkdir command. After a directory, or mount point, is created, attach the partition by
using the mount command. Syntax for the mount command is:
# mkdir /test
# mount /dev/xvdf1 /test
Alternatively, mount the partition or device by referencing the UUID or label. The following
example displays the UUID and label, using the blkid command, and mounts the partition by
referencing each:
# blkid /dev/xvdf1
/dev/xvdf1: LABEL="Test Label" UUID="687eb83f-c16f-4fa9-bb49-1621eed3a35d"
TYPE="ext4"
# mount LABEL="Test Label" /test
# mount UUID="687eb83f-c16f-4fa9-bb49-1621eed3a35d" /test
The mount command without any options displays all currently attached file systems:
# df -hP /test
Filesystem Size Used Avail Use% Mounted on
/dev/xvdf1 923M 2.4M 857M 1% /test
The information in the proc file system displays mounted file systems. Example:
To specify mount options, use the –o flag followed by a comma-separated string of options.
The following are some of the available options for the mount command:
auto: Allows the file system to be mounted automatically by using the mount –a
command
loop: Mounts the image as a loop device
noauto: Disallows the automatic mount of the file system by using the mount –a
command
noexec: Disallows the execution of binary files on the file system
nouser: Disallows an ordinary user (other than root) to mount and unmount the
file system
remount: Remounts the file system in case it is already mounted
ro: Mounts the file system for reading only
rw: Mounts the file system for both reading and writing
user: Allows an ordinary user (other than root) to mount and unmount the file system
For example, to mount the /dev/xvdf1 partition on the /test mount point as read-only with
only the root user able to mount and unmount the file system, enter:
The ext3 and ext4 file systems have three journaling levels that can be set with the -o option
in the mount command or in the options section of /etc/fstab:
data=journal: The highest level. The one that does the most journaling. This writes
the journal entries for all the data and metadata changes. All data is committed into the
journal before being written into the main file system.
data=ordered: The default mode. All data is forced directly out to the main file
system before its metadata is committed to the journal.
data=writeback: The lowest level. Data ordering is not preserved. Data can be written
into the main file system after its metadata has been committed to the journal.
To unmount a file system, use the umount command. The partition name, the device name,
or the mount point is used as an argument. Example:
# umount /dev/xvdd1
# umount /test
/etc/fstab File
The /etc/fstab file is called the file system mount table and contains all the information that
the mount command needs to mount devices. When adding a new file system, create the
appropriate entry in /etc/fstab to ensure that the file system is mounted at boot time. The
following is an example of entries in the /etc/fstab file:
1. Introduction
In this tutorial, we’ll explore several Linux tools to monitor disk space.
First, it’s important to understand the difference between filesystems and mounts.
When we talk about filesystems, we mean a partition of a hard disk that can be used by the
operating system. Filesystems can also be network-based, in-memory, USB drives, and
special temporary spaces.
If we are able to access a filesystem on an operating system, we say the filesystem is
“mounted”, or available in a directory. The directory a filesystem is mounted onto is called
its “mount point”.
The “root” directory is the base directory of the operating system and is located at /.
Therefore, operating systems always have at least one filesystem mounted a
3. df – Filesystem Usage
We’ve seen how to use df and du to get summaries of how much space is used on our system.
However, when it’s time to free up some space on our machine we need to know specifically
what files are taking up all the space. This is where we can make use of
the ls and find commands.
First, let’s look at the directory tree that we are using in the following
examples: Size Path and File
6 ./test.txt
1000M ./three/output4.dat
10M ./two/output2.dat
100M ./two/two-a/more/output3.dat
10M ./two/output1.dat
10M ./one/output.dat
The find command is the way to see all files in a directory tree and their sizes.
To print all files and sizes under a directory, we can do something like this:
$ find . -type f -exec ls -lh {} \;
-rw-r--r-- 1 mike sudo 6 Nov 4 12:37 ./test.txt
-rw-r--r-- 1 mike sudo 1000M Nov 3 20:16 ./three/output4.dat
-rw-r--r-- 1 mike sudo 10M Nov 3 20:15 ./two/output2.dat
-rw-r--r-- 1 mike sudo 100M Nov 3 20:15 ./two/two-a/more/output3.dat
-rw-r--r-- 1 mike sudo 10M Nov 3 20:15 ./two/output1.dat
-rw-r--r-- 1 mike sudo 10M Nov 3 20:15 ./one/output.dat
This shows us each file in the directory tree and its size. Of course, there are many ways to
do this with find, but this simple one prints all files and their sizes.
6. Conclusion
In this tutorial, we have seen how to check disk space for both filesystems and files or
directories. We also looked at a couple of utilities to find some details about files that are
taking up space.
Additionally, if fragmentation does happen, most Linux filesystems would attempt to shuffle
files and chunks around to make them contiguous again.
Disk fragmentation seldom occurs in Linux unless you have a small hard drive, or it is
running out of space. Some possible fragmentation cases include:
if you edit large video files or raw image files, and disk space is limited
if you use older hardware like an old laptop, and you have a small hard drive
if your hard drives start filling up (above 85% used)
if you have many small partitions cluttering your home folder
All you need to do is to back up ALL your files and data to another drive (by
manually copying them over), format the partition, and copy your files back (don’t use a
backup program for this). The journalling file system will handle them as new files and place
them neatly to the disk without fragmentation.
Note: It is generally agreed that to copy large files or large amounts of data,
the dd command might be best. This is a very low level operation and does copy everything
“as is”, including the empty space, and even the junk left over. This is not what we want, so
it is probably better to use cp .
Microsoft’s old FAT file system — last seen by default on Windows 98 and ME, although
it’s still in use on USB flash drives today — doesn’t attempt to arrange files intelligently.
When you save a file to a FAT file system, it saves it as close to the start of the disk as
possible. When you save a second file, it saves it right after the first file — and so on. When
the original files grow in size, they will always become fragmented. There’s no nearby room
for them to grow into.
Microsoft’s newer NTFS file system, which made its way onto consumer PCs with Windows
XP and 2000, tries to be a bit smarter. It allocates more “buffer” free space around files on
the drive, although, as any Windows user can tell you, NTFS file systems still become
fragmented over time.
Because of the way these file systems work, they need to be defragmented to stay at peak
performance. Microsoft has alleviated this problem by running the defragmentation process
in the background on the latest versions of Windows.
Linux’s ext2, ext3, and ext4 file systems — ext4 being the file system used by Ubuntu and
most other current Linux distributions — allocates files in a more intelligent way. Instead of
placing multiple files near each other on the hard disk, Linux file systems scatter different
files all over the disk, leaving a large amount of free space between them. When a file is
edited and needs to grow, there’s usually plenty of free space for the file to grow into. If
fragmentation does occur, the file system will attempt to move the files around to reduce
fragmentation in normal use, without the need for a defragmentation utility.