Linux Notes Mod 1&2 Edited
Linux Notes Mod 1&2 Edited
You can use linux as Server OS or as a Standalone OS on your PC. It is best suited for Server.
Linux is a multi user & multi tasking OS.
1
Advantages of Linux:
● Free – Most Linux distributions are free, users do not need to pay for a copy. Linux
distributions can be freely downloaded and legally installed on as many as you want and
freely (and legally) given to other people.
● Open Source – You have access to the source code and can customize Linux to be
whatever you want it to be. Linux is easy to install.
● Linux is very stable – Linux systems rarely crash, and when they do, the whole system
normally does not go down.
● Linux is not easily affected by computer malware – Infection with virus, spyware, Trojans,
and worms is considerably less for Linux. Also security updates normally come much quicker
to Linux than other operating systems because so many people are contributing to it.
● Linux does not have a registry like windows. So it is not troubled with registry errors which
can slow down a computer.
● Linux will run faster on an old computer. Linux runs great on newer computers as well.
● Linux offers a wide variety from which to choose the distribution.
● Linux is easier than other operating systems to use and install.
● Easier to upgrade than other operating systems.
Disadvantages of Linux:
● Many Windows programs will not run in Linux. Microsoft Office, Internet Explorer and many
other Windows programs will not run natively in Linux.
● There is a smaller selection of peripheral hardware drivers for Linux.
● Gaming software are less compared to Windows.
2
(1. What is the basic difference between Unix and Linux operating system?
Unix Linux
Unix is not open source Linux is open source
Distributions are AT&T, Ultrix, SunSolaris, Distributions are RedHat, Caldera, Debian,
Xenix, IRIX SUSE )
A file system is a group of files and relevant information regarding them. Hard disk may comprise a
single file system or it may be partitioned to have several file systems. The disk space allotted to a
Linux file system is made up of "blocks" each of which is typically of 512 bytes. All the blocks
belonging to the file system are logically divided into four parts. Explain boot block, Super block,
node table and data block.
The Linux file system is the structure in which all the information on the computer is stored. Linux
uses a hierarchical file system to organize its files. It has a root directory (/) that contains other files
and directories. Each file or directory is uniquely identified by its name, directory in which it resides
and a unique identifier called “inode”.
A full path or relative path can be used to refer a file or directory. In Linux there are several types of
files. They are:
1. Ordinary files
2. Directory files and
3. Device files (In Linux system, the devices are considered as special files.)
bin – This directory contains common linux commands such as ls, date etc.(binary files or
executable files)
dev – It contains special files corresponding to devices such as printers,disk storage devices,
terminals, modems, memory etc.
etc – It contains administrative configuration files.
3
home- It contains directories assigned to each regular user with a login account.
lib – It contains shared library files needed by applications in /bin to boot the system.
tmp – used to store all temporary files.
usr- used to store all files of user accounts.
mnt- used to store mount points.
boot – has bootable Linux kernel and boot – loader configuration files. [GRUB – Grand Unified
Bootloader]
Linux supported File Systems are Ext, Ext2, Ext3, Ext4, XFS, NFS, JFS etc.
Inode: An Inode is a data structure containing metadata about the files. The Inode of a file
contains Owner of the file, File size, Date of creation, Permission, Device ID, number of links, etc
An Inode number is a unique number for all the files in Linux and Unix type systems. When a file is
created on a system, a file name and Inode number is assigned to it. Generally, to access a file, a
user uses the file name but internally file name is first mapped with respective Inode number stored
in a table.
4
LINUX Commands
The Linux command is followed by options and a list of arguments. The options can modify the
behavior of a command. The arguments may be files or directories or some other data on which
the command acts. Every command might not need arguments. The options can be provided in
two ways:
(i) single letter option with a - (eg: -a, -l, -abc)
I. Simple Commands
5. more – the output of a command can be send to the more command, using ‘|’ symbol .
‘more’ is a filter for paging through text one screen full at a time.
eg: cal –y | more
1. cat – used to create and view files. Used to concatenate files also.
eg:1) cat > music - to create a file music from keyboard.
artist
album
playlist
Ctrl + Z
eg:2) cat music - to display the contents of the file music.
eg:3) cat music game > new : to concatenate multiple files (music, game)
and write into a single file.
eg:4) cat * - to display all files.
5
III. File commands
1. ls : list directory contents
eg:1) ls - displays all files and directories in the current directory. Ordinary files are denoted
by white color and Directories are denoted by blue color.
Output: pictures
desktop
documents
downloads
videos
music
eg:2) ls - l : to display a long list with file permission modes. Number of links, owner, group,
size in bytes, date and time of last modification and file name.
output: drwxr -xr –x 4 casp casp 4096 NOV 16 12 : 32 Desktop
–rw –rw –r - - 1 casp casp 69 Jul 17 21 : 01 dance.pl
eg:3) ls – R : to list all files and directories recursively from current directories.
eg: 4) ls - a : In linux low level configuration file names start with a period. By default ls does
not display those files. – a option is used to display those hidden files.
eg: 5) ls – s : to list by size, print the allocated size of each file in blocks.
6
File matching meta characters
Meta characters are used to easily refer a group of files.
* - Matches any number of characters
? - Matches any one character
[…] - Matches any one of the character can also include a range of letters or numbers
separated by dash
eg:1) ls f* - to list all files starting with the character ' f '
eg:2) ls a???? - to list all files starting with the character ' a ' and having 4 more characters
eg:3) ls [ab]* - to list all files starting with the letter ' a ' or ' b'
eg:4) ls [a-c]* - to list all files starting with the letter ' a ' or ' b ' or ' c '
There are many linux distributors for handheld devices or old PC’s (such as Damn Small Linux,
Slack ware) which require as little as 24 MB RAM and 486 process0or.
7
Module II : Essential Linux Commands
Processes in Linux : A process is a running instance of a command. A process is identified
in the system by a process id. Process id is unique for a system. Other attributes associated
with the process are: - user account and group account.
Piping in Linux:
A pipe is a form of redirection. A pipe sends the output of one command to another command for
further processing. The stdout of a command is redirected to stdin of another command. The pipe
character is ‘|’.
Pipe is used to combine two or more commands. The output of one command acts as input to
another command, and this command’s output may act as input to the next command and so on.
The commands operate simultaneously. Data are transferred between them continuously. Pipes are
unidirectional. Data flows from left to right through the pipeline. Pipes avoid the use of temporary text
files.
eg: ls -l | more - This command lists all files and directories and gives it as input to the more
command.
We can do piping through several commands as follows:
command1 | command2 | command3
One advantage of pipes in Linux is that, unlike some other popular OS, there is no intermediate file
involved.
8
2. kill - The KILL command is used to terminate a process based on its process id (PID). To kill
the process you must be the owner or have a super user status.
eg: kill 1056
kill command internally sends a signal:
SIGKILL - default signal to terminate a process
SIGSTOP - To pause the process
SIGOUT - To continue the stopped process
eg: kill SIGKILL 1056
killall - This command is used to kill process based on name.
eg: killall file1
6. touch - It is used to create a file without any content. The file created using touch command is
empty. This command can be used when the user doesn’t have data to store at the time of file
creation.
Eg1 : touch file1
Eg2: touch Doc1 Doc2 Doc3
Multiple files with name Doc1, Doc2, Doc3 are created at the same time using touch command here.
Touch command is used to change file time stamps. It updates the access and modification times
of each file to the current time.
– a option is used to change the access time
– m option is used to change the modification time
eg1: touch - a file1
eg2: touch - m file1
eg1: cut - c 5 good : to cut 5th character of each line in the file "good"
eg2: cut - c 2 - 5 good : to cut 2nd to 5th character of each line in the file "good"
eg3: cut - c - 5 good : to cut 1st to 5th character of each line in the file "good"
eg4: cut - d ‘ . ‘ - f1 s4c : to cut the first field from the file "s4c") with delimiter dot( . )
eg5: cut - d ‘ . ‘ - f2 sports : to cut the second field from the file "sports" with delimiter dot( . )
In Linux we can move active programs between the background and foreground. Thus we can
select one program we want to deal at the moment. To stop a running command and put it in the
background, press ‘Ctrl + Z’. The command ‘ fg’ is used to bring a process into foreground. The
command ‘jobs’ is used to check which commands are running in the background. To place a
program in the background, type an ‘&’ at the end of the command line at the time of running the
program.
eg1: ls - R &
find / usr > temp/file &
vi &
eg2: jobs
[ 1 ] Exit ls - R &
[ 2 ] Exit find / usr > /temp/file &
[ 3 ] - Stopped vi &
Exit means the command is completed, Stopped means the command is paused and running
means the command is executing.
The numbers [ 1 ], [ 2 ] and [ 3 ] give the job number. The – sign next to the number
shows that it was placed in the background just before the most recent background job. The + sign
next to the number shows that it was most recently placed in the background.
The find command finds all files in the usr directory and redirects to the file named ‘ file ‘
in the tmp directory.
1. expr: The expr command evaluates a given expression and displays output. It is used for:
● Basic operations like addition, subtraction, multiplication, division, and modulus on
integers.
● Evaluating regular expressions, string operations like substring, length of strings etc. The
operators used are +, –, *, /, %, | , &, <, <=, =, !=, >, >=.
eg: expr 5 + 6 o/p : 11
expr 5 \* 2 o/p : 10 (The multiplication operator * must be escaped when used
in an arithmetic expression with expr.)
3. bc : bc. It is similar to basic calculator by using which we can do basic mathematical operations.
This command is used for command line calculator.
Eg:
10
echo "12+5" | bc
Output : 17
11
IX. vi editor
vi is a text editor in Linux. There are other editors namely geany, emacs, nano, pico joe, gedit , vim
(vi-improved), etc. The vi editor is used to create and edit text or file. To open a file called demo,
type the command;
vi demo
The bottom line of vi editor keeps the information about editing.
vi works in 3 modes:
(i) input mode : It is used to enter text into the file.
(ii) command mode : The vi editor opens in this mode. It is used to pass commands that make
action on the text. Press Esc key to return from input mode to command mode.
(iii) Esc mode : It is used to save a file or switch to another file. Last Line mode is invoked by
typing colon (:) while vi is in command mode. This mode is also called last
line mode or Ex mode( Extended )
1. Commands for Adding Text in vi - To get input mode from command mode, type a input
command letter [a, A, i, I, o, O ]. After finishing the input of text, press the Esc key to return to
command mode.
i - To insert text at the left of the cursor
I - To insert text at the beginning of the line.
a - To add text at the right of the cursor.
A - To add text at the end of the line.
o - To open a line below the current line.
O - To open a line above the current line.
2. Commands for Moving around in the text - Arrow keys moves the cursor left, right, up and
down.
h : move left by one character h j k l
l : move right by one character
k : move up by one character
j : move down by one character
w : moves the cursor to the beginning of the next word
b : moves the cursor to the beginning of the previous word
12
5. Repeating Commands
After delete, change our page text you can repeat the action by typing period.
1. at - It schedules a command to run once at a later time. The command can be anything like a
message or complex script.
Steps: - Run the ‘at’ command in the command line with a scheduled time.
- At the special prompt type the command.
- Press Ctrl + D
Syntax: at <time> <date>
eg: at 11am May 14
at > echo “Welcome”
at > ^ D
The at command puts jobs on “queue a” by default.
2. batch - The batch command is a variation of at command. It executes commands when the
system load level drops below a specified value (1.5 or 0.8). If the system is not busy, the kernel
gives control to such a program. The batch command will delay the start of the job until there is idle
time. The batch command puts jobs on “queue b” by default.
3. crontab - The crontab is a table containing a list of commands that are scheduled to run at
regular time intervals on the computer. The crontab command opens the crontab for editing and lets
us add, remove or modify scheduled tasks. cron is a daemon which reads the crontab and executes
the commands at the right time. Daemon is a background process that performs a specific function
or task. corntab is a list of commands that you want to run on a regular schedule.
Format: < minute> <hour> <day> < month> < weekday> [ username ] command
Eg: 30 8 10 05 * /home/user1/file1
59 5 21 04 wed /home/user1/file2
13
crontab command examples
eg1: crontab -e : to edit crontab
eg2: crontab -l : to list contents
eg3: crontab -r : to remove contents
4. nohup (no hang up) - Prefixing a command with nohup prevents the command from being
aborted automatically when you logout or exit the shell.
*********************
14