Linux Notes Unit 2
Linux Notes Unit 2
(What is Linux Redirection? Explain the different types of redirection with suitable
examples. 5Marks)
Redirection is a feature in Linux such that when executing a command, you can change
the standard input/output devices
Types of Redirection
1. Overwrite Redirection:
Overwrite redirection is useful when you want to store/save the output of a command to a file
and replace all the existing content of that file. for example, if you run a command that gives a
report, and you want to save the report to the existing file of the previous report you can use
overwrite redirection to do this.
∙ “>” standard output
∙ “<” standard input
Example:
So whatever you write after running this command, will be redirected and copied to the “file.txt”.
This is standard output redirection.
cat > file.txt
input redirection, cat command will take the input from “file.txt” and print it to the terminal
screen. This line of code also shows the real working and meaning of the cat command that is
copy and paste. Many people have a misconception that the cat is used to create a file, but it is
not true, the main work of the cat is to copy the input and give the output to the screen.
cat < file.txt
2. Append Redirection:
With the help of this Redirection, you can append the output to the file without compromising
the existing data of the file.
∙ “>>” standard output
∙ “<<” standard input
3. Merge Redirection:
This allows you to redirect the output of a command or a program to a specific file descriptor
instead of standard output. The syntax for using this is “>&” operator followed by the file
descriptor number.
∙ “p >& q” Merges output from stream p with stream q
∙ “p <& q” Merges input from stream p with stream q
1
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.
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.
Syntax :kill [signal] PID
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.
Syntax :kill [option] [filename]
eg: killall file1
2
eg1: find
eg2: find /etc : To search the files in the /etc directory.
- name option is used to find files by name
eg 3: find /home/Steve -name "jan" : Search for “jan” file in /home/Steve directory
- size option is udes to find files by size
eg4: find / usr - size + 10 M : to find files > 10 MB in the usr directory ]
eg5: find / usr - usr s4c : used to find files by user.
eg6: find / usr - perm w : used to find files by permission.
eg7: find / usr - exec -name file1 : used to find files and execute command.
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.
Syntax :touch [options] <file_name1> <file_name2> . . .
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( . )
3
( Which are the commands used to create files in Linux? 2 Marks)
The touch command creates empty files. The cat command is most commonly used to view the
contents of a file. But you can also use it to create files. The echo command is used to add and
append text to files. It also creates the file if it doesn't already exist.
Vi editor is also used to create files.
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 +, –, *, /, %, | , &, <, <=, =, !=, >, >=.
Syntax :$expr <expression>
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.)
4
○ Increment or Decrement operators
○ Assignment operators
○ Comparison or Relational operators
○ Logical or Boolean operators
○ Math functions
○ Conditional statements
○ Iterative statements
Egs:
$ echo "var=10;++var" | bc
echo "10>5" | bc Output: 1
IX. vi editor
( What are editors? Explain vi editors. 5 marks)
Linux text editors can be used for editing text files, writing codes, updating user instruction files,
and more. A Linux system supports multiple text editors. There are two types of text editors in
Linux, which are given below:
o Command-line text editors such as Vi, nano, pico, and more.
o GUI text editors such as gedit (for Gnome), Kwrite, and more.
VI in the vi editor stands for Visual Editor. It is a user-friendly and very powerful application that is
available in all Linux distros. An improved version of vi editors is vim.
These are the following reasons why VI editor is popular:
∙ It is available in almost all Linux Distributions.
∙ It is user-friendly.
∙ It works the same across different platforms and distributions.
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) command mode : In command mode, actions are taken on the file. The vi editor opens in this
mode. It is used to pass commands that make action on the text.
(ii) input mode / insert mode: It is used to insert text into the file. For that, just type 'i' and you'll
be in insert mode. 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 )
5
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
5. Repeating Commands
After delete, change our page text you can repeat the action by typing period.
6
ctrl + g - goes to the last line of the page.
1g - goes to the first line of the page.
8. Do and Undo commands
u - undo the previous change
ctrl + R - to redo a command
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
4. nohup (no hang up) - Prefixing a command with nohup prevents the command from being
aborted automatically when you logout or exit the shell.
*********************