Lab Manual 3
Lab Manual 3
Lab Manual 3
Experiment #03
Linux Shell commands for file operations and their rights
Objective
The main objectives of this lab are to understand:
Software Tools
Ubuntu 16.04
GCC Complier
Theory
Viewing and editing a Text File
Using touch to create a blank text file: $ touch NewFile.txt.
Using cat to create a new file: $ cat NewFile.txt. ... (write anything in your file CNTR+D to
save and CTRL+Z to exit
Simply using > to create a blank text file: $ > NewFile.txt.
$ cat [option] [filename]
i. $ cat filename
Purpose: It will display the contents of the file filename.
ii. $ cat > file1
Purpose: “>” is called output redirection operator. It permits user to write in file1.If
file1 already exists then it over writes the contents of the file1.
E.g. write any word/phrase after giving command $ cat > file1 like “Success is not a
destination.”
Press [Ctrl+d]
Note: The above command creates the file called file1 if file1 does not exist and
allow user to write text there. Press Ctrl+d (To save and exit).
iii. $cat >> file1
Purpose: “>>” is use to append data in a file. It does not overwrite text of file1
rather appends text at the end of file1.
E.g. write any word/phrase after using command $ cat >> file1 like “It’s a
progressive journey. ”
Press [Ctrl+d]
Note: The above command appends data in file1. Here you can find that data is
appended in file1 or not by using command $ cat filename, which will display all the
content of file1.
$ cat file1
iv. $cat file1 file2 >file3
The above command is used to write contents of the file1 and file 2 into file3
v. $ head [option] Filename
Purpose: “head” displays the top part of a file. By default it shows the first 10 lines.
head -n allows you to change the number of lines to be shown.
$ head File1: It displays first 10 lines of File1.
$ head –n50 file.txt: Displays the first 50 lines of the file.txt
$head -50 file.txt: Produces the same result as above command and Displays
the first 50 lines of the file.txt
vi. $ tail [option] Filename
Purpose:“tails” displays the bottom portion of a file. By default it shows the last 10
lines.
Same as head command.
tail -n allows you to change the number of lines to be shown.
$ tail File1: It displays last 10 lines of File1.
$ tail –n15 file.txt: Displays the last 15 lines of the file.txt
Mostly all command gives output on screen or take input from keyboard, but in Linux (and in
other OSs also) it's possible to send output to file or to read input from file.
Linux redirection feature is used to detach the default files from stdin, stdout, and stderr and
attach other files with them.
Input Redirection:
command < input-file OR command 0< input-file
Purpose: Detach keyboard from stdin and attach ‘input-file’ to it, i.e., ‘command’ reads
input from ‘input-file’ and not keyboard. . E.g. To take input for cat command $ cat
<myfiles
Output Redirection:
command> output-file OR command 1> output-file
Purpose: Detach the display screen from stdout and attach ‘output-file’ to it, i.e., ‘command’
sends output to ‘output-file’ and not the display screen. E.g. $ ls command gives output to
screen; to send output to file of ls command.
Give command : $ ls> filename
It means put output of ls command to filename. Now if filename file exist in your current
directory it will be overwritten without any type of warning.
E.g. To send output of date command to already exist file give command
$ date >>myfiles
Error Redirection:
command 2> error-file
Purpose: Detach the display screen from stderr and attach ‘error-file’ to it, i.e., error
messages are sent to ‘error-file’ and not the display screen
Description “cat" is short for concatenate. This command is used to view and concatenate files.
Examples cat /etc/passwd
This command displays the "/etc/passwd" file on your
screen.
cat /etc/profile
This command displays the "/etc/profile" file on your screen. Notice that some of the contents of
this file may scroll off of your screen.
cat file1 file2 file3 > file4
This command combines the contents of the first three files into the fourth file.
Create text file sname as follows:
$ cat>sname
virk
ash
zebra
babu
Press CTRL + D to save.
Note: In above example sort ($ sort <sname>sorted_names) command takes input from sname
file and output of sort command (i.e. sorted names) is redirected to sorted_names file.
File Permissions:
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.
1. Ownership
2. Permission
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.
Now, the big question arises how does Linux distinguish between these three user types so that
a user 'A' cannot affect a file which contains some other user 'B's' vital information/data. It is like
you do not want your colleague, who works on your Linux computer, to view your images. This
is where Permissions set in, and they define user behavior.
Let us understand the Permission system on Linux.
grou
user p othe
rs
chmod777 filename
Permissions
Every file and directory in your UNIX/Linux system has following 3 permissions defined for
all the 3 owners discussed above.
Read: This permission give you the authority to open and read a file. Read permission on
a directory gives you the ability to lists its content.
Write: The write permission gives you the authority to modify the contents of a file. The
write permission on a directory gives you the authority to add, remove and rename files
stored in the directory. Consider a scenario where you have to write permission on file
but do not have write permission on the directory where the file is stored. You will be
able to modify the file contents. But you will not be able to rename, move or remove the
file from the directory.
Execute: In Windows, an executable program usually has an extension ".exe" and which
you can easily run. In Unix/Linux, you cannot run a program unless the execute
permission is set. If the execute permission is not set, you might still be able to
see/modify the program code(provided read & write permissions are set), but not run it.
ls - l on terminal gives
ls - l
Here, we have highlighted '-rw-rw-r--'and this weird looking code is the one that tells us about
the permissions given to the owner, user group and the world.
Here, the first '-' implies that we have selected a file.p>
We can use the 'chmod' command which stands for 'change mode'. Using the command, we can
set permissions (read, write, execute) on a file/directory for the owner, group and the
world. Syntax:
chmod permissions filename
There are 2 ways to use the command -
1. Absolute mode
2. Symbolic mode
Absolute(Numeric) Mode
In this mode, file permissions are not represented as characters but a three-digit octal
number.
The table below gives numbers for all for permissions types.
Number Permission Type Symbol
0 No Permission ---
1 Execute --x
2 Write -w-
4 Read r--
In the above-given terminal window, we have changed the permissions of the file 'sample to
'764'.
U user/owner
G Group
O Other
A All
We will not be using permissions in numbers like 755 but characters like rwx. Let's look into an
example
Example
p. Assign permissions of read and write only to user (do it twice by using both symbolic and
numeric methods).
q. Verify that above task has done successfully.
r. Assign all permissions to user (do it twice by using both symbolic and numeric methods).
s. Verify that above task has done successfully.
t. Assign permission of read and execute only to group (do it twice by using both symbolic
and numeric methods).
u. Verify that above task has done successfully.
v. Assign all permissions to all users (do it twice by using both symbolic and numeric
methods).
w. Verify that above task has done successfully.
2. Show Simulation results after performing the following tasks by using suitable commands in
Linux. Also show command/commands used for specific task
a. Create 3 empty text files and name them as students.txt, pstudent.txt and fstudents.txt.
b. Assign permissions to read only permission to the user (do it twice by using both symbolic
and numeric methods).
c. Enter at least 10 10 different students’ names in pstudent.txt and fstudent.txt. If any error
appears, debug that error for user only.
d. Now create directory named HITEC and assign permissions of read only to user and
group (do it twice by using both symbolic and numeric methods).
e. Copy all files to directory HITEC. If any error appears, debug that error for user and
group only.
f. After that append the file students.txt with first five sorted names from pstudent.txt and
last five sorted names from fstudent.txt.
g. Show the contents of sorted names from file students.txt.
h. Change the permissions of file students.txt read only and both other files read and write
only (do it twice by using both symbolic and numeric methods).
i. Now append another student name in student.txt file.
j. Display contents of all the files.
k. Show permissions and all of the file contents of HITEC.
Conclusion:
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
_________________________________
_________________
Lab Instructor
Iram Abdullah