LAB 5
Course Code: CSC 2209 Course Title: Operating Systems
Dept. of Computer Science
Faculty of Science and Technology
Lecturer No: 05 Week No: 05 Semester: Fall 22-23
Lecturer: Syma Kamal Chaity, chaity@aiub.edu
Lecture Outline
1. grep Command
2. Case in-sensitive search using grep
3. File Permissions in Linux/Unix with Example
4. Ownership of Linux files
5. Change permissions of a file
grep Command
grep: The -i option enables to search for a string case
insensitively in the give file. It matches the words like “UNIX”,
“Unix”, “unix”.
$ grep -i “UNix" sample.txt
Search for a given string in a file (case
in-sensitive search)
Case insensitive search : The grep filter searches a file for a
particular pattern of characters, and displays all lines that
contain that pattern.
Displaying the count of number of matches : We can find the
number of lines that matches the given string/pattern
$grep -c "unix" sample.txt
Cont’d
Displaying only the matched pattern : By default, grep displays
the entire line which has the matched string. We can make the
grep to display only the matched string by using the -o option.
$ grep -o "unix" sample.txt
Inverting the pattern match : You can display the lines that are
not matched with the specified search sting pattern using the -v
option.
$ grep -v "unix" sample.txt
File Permissions in Linux/Unix with
Example
Linux is a clone of UNIX, the multi-user operating system which
can be accessed by many users simultaneously. Linux can also
be used in mainframes and servers without any modifications.
But this raises security concerns as an unsolicited or malign
user can corrupt, change or remove crucial data. For effective
security, Linux divides authorization into 2 levels.
- Ownership (chown)
- Permission (chmod)
Ownership of Linux files
Every file and directory on your Unix/Linux system is assigned 3 types of owner,
given below.
User: A user is the owner of the file. By default, the person who created a file
becomes its owner. Hence, a user is also sometimes called an owner.
Group: A user- group can contain multiple users. All users belonging to a
group will have the same access permissions to the file. Suppose you have a
project where a number of people require access to a file. Instead of manually
assigning permissions to each user, you could add all users to a group, and
assign group permission to file such that only this group members and no one
else can read or modify the files.
Other: Any other user who has access to a file. This person has neither
created the file, nor he belongs to a usergroup who could own the file.
Practically, it means everybody else. Hence, when you set the permission for
others, it is also referred as set permissions for the world.
Change permissions of a file
user group others
rwx rwx rwx
421 421 421
7 7 7
User: rwx = 4 + 2 + 1 = 7
group: rwx = 4 + 2 + 1 = 7
Other: rw- = 4 + 2 + 1 = 7
Current permissions of guest directory is rwx rwx rw_
Change the permissions of guest directory to -rw-r--r--
User: rw- = 0+4+ 2 = 6
group: -r- = 0 + 4 + 0 = 4 chmod 644
everyone: -r- = 0+4+0 = 4
guest
Change permissions of a file
(cont’d)
user group others
rwx rwx rw- User: rwx = 4 + 2 + 1 = 7
group: rwx = 4 + 2 + 1 = 7
421 421 420 Other: rw- = 4 + 2 + 0 = 6
7 7 6
1. create AIUB and assign rw- rw- --- permission to AIUB
directory
2. Create a file os1.txt in dir2sub. Give the read, write and
executable permission for the user, read and executable
permission for the group and give others read permission.
3. Random text file ..write 2 lines containing “aiub” string.. Now
search “aiub” and show all aiub (both case)
Books
Unix Shell Programming
Written by Yashavant P. Kanetkar