You are on page 1of 42

Chapter:-3

Shell Keywords in Linux Programming

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.

echo read set unset

readonly shift export if

fi else while do

done for until case

esac break continue exit

return trap wait eval

exec ulimit umask

What are the shell variables in Linux?

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

System Meaning To view variable value type


Variable
Your login
TERM echo $TERM export TERM=vt100
terminal type.

Set path to login


SHELL echo $SHELL
shell.

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

Bash supports the following arithmetic operators:

Operator Description Example


Addition Adds the operands on either side expr $a +
$b
Subtraction Subtracts the right hand operand from the left hand expr $a - $b
Multiplication Multiplies two operands expr $a * $b
Division Divides the right hand operand with the left hand operand expr $a / $b
and returns the quotient
Modulus Divides the right hand operand with the left hand operand expr $a %
and returns the remainder $b
Increment Unary operator to increment an operand by one expr
$((++a))
Decrement Unary operator to decrement an operand by one expr $((--a))
1

2
3
4
5
6
7

8
9
a=4
b=5
echo "a + b = $((a + b))"
echo "a - b = $((a - b))"

echo "a * b = $((a * b))"


echo "a / b = $((a / b))"
echo "a % b = $((a % b))"
echo "++a = $((++a))"
echo "--b = $((--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.

Relational operators either give true or false depending on the relation.

Operator Description Example


== Compares two operands and returns true if they are equal; otherwise, [$a ==
it returns false $b]
!= Compares two operands and returns true if they are not equal; otherwise, [$a != $b]
it returns false
< Less than operator; returns true if the left hand operand is less than the [$a < $b]
right hand operand
<= Less than or equal to operator; returns true if the left hand operand [$a <=
is less than or equal to the right hand operand $b]
> Greater than operator; returns true if the left hand operand is greater [$a > $b]
than the right hand operand
>= Greater than or equal to operator; returns true if the left hand operand [$a >=
is
greater than or equal to the right hand operand $b]
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

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

The following boolean operators are supported by bash:

Operator Description Example


! The logical negation operator [ ! false ]
-o ( || ) The logical OR operator [ $a < 20 || $b > 30 ]
-a ( && ) The logical AND operator [ $a < 20 & $b > 30 ]
1
2
3
echo "LOGICAL_AND = $((1 && 0))"
echo "LOGICAL_OR = $((0 || 1))"
echo "LOGICAL_Neg = $((!0))"

Run

4) Bitwise operators

Bitwise operators are used to perform bitwise operations on bit fields.

Operators Description Example


& Performs the binary AND operation bit by bit on echo "$((0x4 &
the arguments 0x1))"
| Performs the binary OR operation bit by bit on the arguments echo "$((0x4 |
0x1))"
^ Performs the binary XOR operation bit by bit on echo "$((0x4 ^
the arguments 0x1))"
~ Performs the binary NOT operation bit by bit on the arguments echo "$((~0x4))"
<< Shifts the bit field to the left by the number of times specified echo "$((0x4 <<
by the right hand operand 1))"
>> Shifts the bit field to the right by the number of times specified echo "$((0x4 >>
by the right hand operand 1))"
1
2
3
4
5
echo "0x4 & 0x0 = $((0x4 &
0x0))" echo "0x4 | 0x1 = $((0x4 |
0x1))" echo "0x1 ^ 0x1 = $((0x1 ^
0x1))" echo "0x1 << 4 = $((0x1 <<
4))" echo "0x4 >> 2 = $((0x4 >>
2))"

Run

5) File test operators

Bash provides operators to test for various properties of files; these operators are known
as file test operators.

Operator Description Example


-b Checks whether a file is a block special file or not [ -b $FileName]
-c Checks whether a file is character special or not [ -c $FileName]
-d Checks if a given directory exists or not [ -d $FileName]
-e Checks whether a given file exists or not [ -e $FileName]
-r Checks if the given file has read access or not [ -r $FileName]
-w Checks if the given file has write access or not [ -w $FileName]
-x Checks if the given file has execute access or not [ -x $FileName]
-s Checks the size of the given file [ -s $FileName]
main.sh

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 with Examples

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 :

echo -e "Geeks \bfor \bGeeks"

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:

 read command without any option:


The read command asks for the user’s input and exit once the user provides some input.

 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.

Linux tput command

On Unix-like operating systems, the tput command initializes a terminal or queries


the terminfo database.

This page covers the Linux version of tput.


Description
The tput utility uses the terminfo database to make the values of terminal-dependent
capabilities and information available to the shell, to initialize or reset the terminal, or return
the long name

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.

1. Set the Cursor Position using tput cup


You can move the cursor to a specific row and column using tput cup. Following example
positions the cursor at row 2 and column 3.

$ tput cup 2 3

2. Clear the Screen Using tput clear


If you are in middle of the terminal screen, tput clear will clear the screen and put you at the
top of the terminal screen.

$ tput clear

3. Get the Number of Columns and Lines of a Terminal


To display the number of columns of your terminal screen, do the following.

$ tput cols

Following displays number of lines of your terminal screen.

$ 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

5. Change the Terminal Background Color using tput setb


Using tput, the background color of the screen can be changed as shown below.

$ tput setb 4

6. Change the Foreground Color using tput setf


You can also change the foreground color of the terminal as shown below.

$ 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”.

7. Turn On and Turn Off Highlighting


tput allows you to turn on and turn off the text high lighting. When you turn it ton, new text
in the terminal gets bold.

$ 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.

$ echo `tput bold`guide`tput sgr0`

guide

8. Underline Text using smul and


rmul Start the underline mode:

$ tput smul

Stop the underline mode:

$ 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

Their description with syntax is as follows:

1) if statement

This block will process if specified condition is true.


Syntax:

if [ expression ] then
statement
fi

An example of the code:

#Initializing two variables a=20


b=20

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;;

4) if..elif..else..fi statement (Else If ladder)

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

3 .if..elif..else..fi statement (Else If ladder)

4 .if..then..else..if..then..fi..fi..(Nested if)

5 switch statement

Their description with syntax is as follows:

1) if statement

This block will process if specified condition is true.

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

3) if..elif..else..fi statement (Else If ladder)


To use multiple conditions in one if-else block, then 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 condition is true then it processes else part.

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

#Check whether they are equal

if [ $a == $b ]

then

echo "a is equal to b"

fi

#Check whether they are not equal

if [ $a != $b ]

then

echo "a is not equal to b"

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

#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

Output
$bash -f main.sh
a is equal to b

Example 3:
Implementing switch statement

CARS="bmw"

#Pass the variable in string


case "$CARS" in

#case 1

"mercedes") echo "Headquarters -


Affalterbach, Germany" ;;

#case 2

"audi") echo "Headquarters - Ingolstadt, Germany" ;;

#case 3

"bmw") echo "Headquarters - Chennai, Tamil Nadu,


India" ;;

esac

Output
$bash -f main.sh
Headquarters - Chennai, Tamil Nadu, India.

Looping Statements in Shell Scripting:


There are total 3 looping statements which can be used in bash programming

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

#Start of for loop

for a in 1 2 3 4 5 6 7 8 9 10

do

# if a is equal to 5 break the loop

if [ $a == 5 ]

then

break

fi

# Print the value


echo "Iteration no $a"

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 the loop

and # don't move to line 8

if [ $a == 5 ]

then

continue

fi

echo "Iteration no $a"

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

# -lt is less than operator

#Iterate the loop until a less than

10 while [ $a -lt 10 ]

do

# Print the values

echo $a

# increment the value

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

# -gt is greater than operator

#Iterate the loop until a is greater than

10 until [ $a -gt 10 ]

do

# Print the values

echo $a

# increment the value

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:

COLORS="red green blue"

# the for loop continues until it reads all the values from
the COLORS

for COLOR in $COLORS

do

echo "COLOR: $COLOR"

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 $@

echo "Executing script"

# the script is executed using the below command

# main.sh sample1 sample2 sample

# where sample1, sample2 and sample3 are the positional


arguments

# here $@ contains all the positional arguments.

for SAMPLE in $@

do

echo "The given sample is: $SAMPLE"

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 indicates it sleeps for 1 sec

echo "Hi, I am infinity loop"

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

while [ "$CORRECT" == "n" ]

do

# loop discontinues when you enter y i.e.e, when your


name is correct
# -p stands for prompt asking for the

input read -p "Enter your name:" NAME

read -p "Is ${NAME} correct? "

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.

Figure – Formatting process of disk

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 :-

As suggesting from the name, partitioning means divisions. Partitioning is the


process of dividing the hard-disk into one or more regions. The regions are called as
partitions.
It can be performed by the users and it will affect the disk performance.
3. High-level Formatting :
High-level formatting is the process of writing. Writing on a file system, cluster size,
partition label, and so on for a newly created partition or volume. It is done to erase the
hard-disk and again installing the operating system on the disk-drive.

Figure – Steps of High-level Formatting

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

ext2 High performance for fixed disk and removable media

ext3 Journaling version of ext2

ext4 Supports larger files and file system sizes

vfat MS-DOS file system useful when sharing files between Windows
and Linux

XFS High-performance journaling file system

Btrfs Addresses scalability requirements of large storage systems


Creating Filesystems

The command to build a Linux file system on a device, or hard disk partition, is mkfs. The
syntax for the command is:

# mkfs [options] device


The mkfs command is actually a front end for the different file system builder utilities such as
mkfs.ext2 and mkfs.ext4. These utilities are executable directly from the command line.
When using the mkfs wrapper, include the -t fstype option to specify the type of file system
to be built. If not specified, the default file system type, ext2, is created.

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.

Sample /etc/mke2fs.conf file

Mounting a file system

Mounting File Systems

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:

# mount [options] device_file mount_point


The following example creates a mount point (/test) and attaches the partition:

# 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:

# mount | grep test


/dev/xvdf1 on /test type ext4 (rw,relatime,seclabel,data=ordered)
In this example, the /dev/xvdf1 partition is mounted on /test. The file system type is ext4 and
is mounted for both reading and writing. The df command also displays mounted file
systems. Example:

# 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:

# cat /proc/mounts | grep test


/dev/xvdf1 /test ext4 rw,seclabel,relatime,data=ordered 0 0
Mount Options

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:

# mount –o nouser,ro /dev/xvdf1 /test


To mount an ISO image by using the loop device (assuming that the ISO image is present in
the current directory and the mount point exist), enter:

# mount -o ro,loop rhel7-x86_64-dvd.iso /media/cdrom


Journaling Mount Options

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.

Unmounting a file system

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:

# cat /etc/fstab LABEL=centos_root


/ ext4defaults 00
devpts tmpfs proc sysfs
/dev/pts devpts gid=5,mode=620 0 0
/dev/shm tmpfs defaults 00
/procprocdefaults 00
/sys sysfs defaults 00
UUID=687eb83f-c16f-4fa9-bb49-1621eed3a35d/test ext4defaults0 0
The first column is the device to mount. The UUID or the label name should be used in place
of the device name, because device names could change. The second column is the mount
point, except the swap partition entry. The third column is the file system type. The fourth
column specifies mount options. The fifth column is used by the dump command. The
number 1 means to dump the file system and 0 means the file system does not need to be
dumped. The last column is used by the fsck program to determine the order in which file
system checks are done at reboot time. The root file system should be specified with a
value of 1 and the other file systems should have a value of 2. A value of 0 does not check
the file system.

Monitoring disk usage

1. Introduction

In this tutorial, we’ll explore several Linux tools to monitor disk space.

2. Filesystems and Mounts

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

df is the command we’ll use to look at filesystem usage.


Let’s try running this in a shell:
user@host:~$ df
Filesystem 1K-blocks Used Available Use% Mounted on
udev 1005792 0 1005792 0% /dev
tmpfs 204824 22140 182684 11% /run
/dev/sda 24543644 15646380 7635696 68% /
tmpfs 1024108 0 1024108 0% /dev/shm
tmpfs 5120 0 5120 0% /run/lock
tmpfs 1024108 0 1024108 0% /sys/fs/cgroup
tmpfs 204824 0 204824 0% /run/user/1000
We can see from the column headers what each value means. Importantly, the columns that
have to do with sizes will change in scale depending on the arguments that we pass to df.
For example, let’s try df -h. The -h will display the output in human-readable format in
powers of 1024:
4. du – Directory Usage

du is the command we’ll use to look at directory sizes.


Let’s imagine our terminal is located where there are three directories and a file:
$ ls -l
total 20
drwxr-xr-x 2 mike sudo 4096 Nov 3 20:15 one
-rw-r--r-- 1 mike sudo 6 Nov 4 12:37 test.txt
drwxr-xr-x 2 mike sudo 4096 Nov 3 20:16 three
drwxr-xr-x 2 mike sudo 4096 Nov 3 20:15 two

5. Finding Large Files

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.

5.1. Example Files

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

5.3. Using find

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 on Linux

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

How to easily defragment Linux filesystems

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.

To back up your files, run

cp -afv [/path/to/source/partition]/* [/path/to/destination/folder]

Mind the asterix (*); it is important.

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 .

Now you only need to remove all the original files.

sudo rm -rf [/path/to/source/partition]/*

How Windows File Systems Work

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.

How Linux File Systems Work

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.

You might also like