Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
0 views

Lab 1_Linux_Shell_Commands (3)

The document provides an overview of Linux shell programming, covering various commands for file creation, manipulation, and permissions. It explains how to use commands like 'ls', 'cp', 'mv', and 'chmod', as well as file viewing and searching techniques using 'grep' and 'find'. Additionally, it touches on mathematical operations, redirection, and piping in the Linux shell environment.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Lab 1_Linux_Shell_Commands (3)

The document provides an overview of Linux shell programming, covering various commands for file creation, manipulation, and permissions. It explains how to use commands like 'ls', 'cp', 'mv', and 'chmod', as well as file viewing and searching techniques using 'grep' and 'find'. Additionally, it touches on mathematical operations, redirection, and piping in the Linux shell environment.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

1

LINUX SHELL
PROGRAMMING
Operating System Lab
Introduction
2

 Many people says that Linux is a command based


operating system.
 So many of us thinks that Linux is not so user friendly
OS.
 But it is not true. Linux is a GUI based OS with a Shell
which is more powerful than its counter part in
Windows OS.
 We will be familiar with some shell commands.
Identity
3

 Type uname and Linux will tell his name to you

 If you want to know your name type whoami


Manual
4

 For each command Linux contains manual. To view


the manual : man name
 man uname
Creating Files
5

 There are two commands to create file.


 touch
 cat

 The size of the file would be zero bytes since touch


doesn’t allow to store anything in a file.

 Useful to create several empty files quickly.


Creating Files (Cont.)
6

 Store lines in a file

 After completion of writing press the keys ctrl+d.

 In Unix the keys ctrl+d indicate the EOF or end of file


character.
Creating Files (Cont.)
7

 To see the contents of the file test

 It can concatenate the contents of two files and store them


in the third file.

 This would create newsample which contains contents of


sample1 followed by that of sample2.
File Operations
8

 To copy a file : cp
 Copy source to destination or multiple sources to
directory
 -i [prompt before overwrite]

 ‐r [copy directories recursively]

 ‐u [copy only when the src file is newer than the dest
file or when the dest file is missing]
File Operations (Cont.)
9

 To remove a file or directory : rm


 ‐f [ignore nonexistent files, never prompt]
 ‐i [prompt before any removal]

 ‐r [remove the contents of directories recursively]

 ‐v [explain what is being done]


File Operations (Cont.)
10

 To move or rename a file : mv


 rename src to dest or move src(s) to directory
 ‐i [prompt before overwrite]

 ‐u [move only when the src file is newer than the dest
file or when the dest file is missing]
 ‐v [explain what is being done]
Directory and File Listings
11

 To list information about directory or files : ls


 This command contains some options.
 ‐a [do not hide entries starting with .]
 ‐A [do not list implied . and ..]

 ‐h [print sizes in human readable format]

 ‐l [use a long listing format]

 ‐S [sort by file size]


Directory and File Listings (Cont.)
12

 Any file name which begins with a ‘.’ is treated as a


hidden file.

 . stands for the current directory.


 .. stands for the parent previous directory.
Directory and File Listings (Cont.)
13
Directory and File Listings (Cont.)
14
Directory and File Listings (Cont.)
15
Directory and File Listings (Cont.)
16

 To create Link

 Avoids unnecessary duplication of the same file


contents in different directories.

 By default any new file that we create has one link


whereas any new directory we create has two links.
Directory and File Permissions
17

 Each file or directory has 3 security groups.


 Owner
 Group
 All Others
 Each security group has 3 flags that control the access
status : read, write, execute
 They are listed as 'rwx' or a "‐" if the access is turned
off.
 rwxrwxrwx[read, write and executable for owner, group
and all others]
 rw‐r‐‐r‐‐[read and write by owner, read only for group and
all others]
Directory and File Permissions (Cont.)
18

 To change the permissions type chmod


 u, g, o or all [whose permission you are changing]
 + or ‐ [type of change: add or subtract permission]
 combination of r, w or x [which permission you are
changing: read, write or execute]
 To change Permission
 chmod [who] [+/-/=] [permissions] file
Directory and File Listings (Cont.)
19

 file or directory [name of file or directory to change]


 chmod go+rw file1 file2 add read and write access for
group and others for files 'file1' and 'file2’
 chmod a+rwx file1 add read, write and execute for
everyone for 'file1‘.
 chmod 555 file1
A Bit of Mathematics
20

 Calculator is invoked at shell prompt by typing bc.


 The input to the calculator is taken line by line.
 By typing bc at prompt the calculator mode starts and the $
the prompt disappears.
 Typing quit ends tryst with bc.
A Bit of Mathematics (Cont.)
21

 Working with floats

 After Setting the scale variable if the answer of an expression turns


out more than what scale can provide then the value in scale is
ignored and the correct answer is displayed.
A Bit of Mathematics (Cont.)
22

 Working with different base

 By setting the variable ibase to 2 and obase to 16 all input that is supplied
is taken as binary whereas all output is displayed in hexadecimal.
A Bit of Mathematics (Cont.)
23

 Working with functions

 Trigonometric functions expect their arguments in radians and not in


degrees.
A Bit of Mathematics (Cont.)
24

 Working with variables


A Bit of Mathematics (Cont.)
25

 Another math related command in Unix: factor


 If a positive number less than 2^46 is types in then it factorise
the number and print its prime factors. Each one is printed the
proper number of times.
A Bit of Mathematics (Cont.)
26

 Another math related command in Unix: factor


 If a positive number less than 2^46 is types in then it factorize
the number and print its prime factors. Each one is printed the
proper number of times.
Redirection
27

 Input redirection:
< ‐get input from file instead of the keyboard

 Output redirection:
> ‐send output to file instead of the terminal window

 Append output:
 >> ‐command is used to append to a file if it already
exists
Redirection (Cont.)
28

 Example:
Calender
29

 To view calendar in the shell: cal


File Viewing

ƒ To output the first lines of files : head file1 file2 file3 …


ƒ Print the first 10 lines of each file to standard output
ƒ With more than one file , precede each with a header
giving the file name
ƒ ‐n [ output the last n lines, instead of the last 10 ]
File Viewing

ƒ To output the last lines of files : tail file1 file2 file3 …


ƒ Print the last 10 lines of each file to standard output
ƒ With more than one file, precede each with a header
giving the file name
ƒ ‐n [ output the last n lines, instead of the last 10 ]
File Viewing

ƒ To sort lines of a text files : sort file1 file2 file3…


ƒ Write sorted concatenation of all file(s) to standard
output.
File Viewing

ƒ To print the number of lines, words and bytes in files :


wc file1 file2 file3 …
ƒ print byte, word, and newline counts for each file and a
total line if more than one file is specified.
ƒ ‐l [ print the newline counts ]
ƒ ‐w [ print the word counts ]
Piping

ƒ The input of a command may come from the output of


another command.
ƒ This is accomplished with the ‘ | ‘ pipe operator.

ƒ How to view the lines 15‐20 of a file named ‘a.txt’ ?


Piping

ƒ The input of a command may come from the output of


another command.
ƒ This is accomplished with the ‘ | ‘ pipe operator.

ƒ How to view the lines 15‐20 of a file named ‘a.txt’ ?


ƒ head ‐20 a.txt | tail ‐5
Grep

ƒ grep matches a pattern in a given a list of files or


standard input and outputs only the matching lines.
ƒ grep pattern filename
ƒ grep abc file.txt
ƒ grep patterns are case sensitive by default.
ƒ Some options
ƒ ‐i [ case insensitive search ]
ƒ ‐c [count of total matches]
ƒ ‐E [regular expressions can be provided as patterns]
ƒ ‐n [display the line numbers of the matched lines]
Find
ƒ search for files in a directory hierarchy.
ƒ By default, find returns all files below the current
working directory.
ƒ find
ƒ To search a pattern : find ‐name '*txt*'
ƒ To search for a file type :
ƒ find ‐type d [find all directories]
ƒ fine ‐type f [find all regular files]
ƒ Find executes the '‐print' action by default. To change it
to style such as ‘ls’ : find ‐type f –ls
Find
ƒ To search all the directories
ƒ not recommended
ƒ find / ‐name "myfile" ‐type f
ƒ To search a specific directory
ƒ find /home/dir1 ‐name "myfile" ‐type f
ƒ To search multiple directories
ƒ find dir1 dir2 ‐name "myfile" ‐type f
ƒ To Search for all files owned by a user
ƒ find ‐user userid
ƒ To take an action
ƒ find ‐type f ‐name '*ch*' ‐exec chmod a+rwx {} \;
ƒ {} is replaced with the name of the file
ƒ The ; indiates the end of the command.
Thanks

You might also like