Linux Lab CSC 371L 2 - Merged

Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

FAHAD BIN SULTAN UNIVERSITY

College of Computing
Linux Lab CSC 371L

Experiment 2
Course Outline

Instructors:
1. Mohammad A. Mezher, PhD. Email: [email protected]

Experiment Objectives:
The objectives of these experiments are
 To use and apply knowledge learned in the class.
 To give you experience in using files and text files
 To search for a text inside a text file
 To display the head and tail of number of lines in the text files
 To sort the text in the text files
 To compare between two files
 To remove duplicate of the lines in the text files
 To communicate between process using pipes
 To display time
 To convert between Unix and MS-Dos and vice versa

Suggested Reference Readings:

[1] A practical guide to Ubuntu Linux, Mark Sobell, Third Edition.

1
Experiments:

1. Using GUI do the following


a. Create a folder named by you “your_name”.
b. Using the editor of your choice, create small files named “months” and
“months2”
c. Enter the names of the months that lists the 12 months of the year in order, one
each line
d. Enter some duplicated months
e. Save your “months” and “months2” files into “your_name” folder
f. Exit
2. Using LUI do the following
a. grep: Finds a String

$ cat months

$ grep 'apr' months

b. head: Displays the Beginning of a File

$ cat months
$ head -1 months

c. tail: Displays the End of a File


$ cat months
$ tail -5 months

d. sort: Displays a File in Order

$ cat months
$ sort months
$ tail -5 months

Save the sorted contents in another File

$ cat months
$ sort months > months_sorted

2
- Now sort the contents of text file ‘file1.txt‘ in reverse order by
using ‘-r‘ OR ‘--reverse‘ switch and redirect output to a file ‘
reversesorted.txt‘. Also check the content listing of the newly
created file.

$ sort -r file1.txt > reversesorted.txt


$ cat reversesorted.txt

- Sort the contents of file ‘ file1.txt‘ on the basis of 2nd column (which represents
number) as shown below

$ sort –nk2 file1.txt

- To check more information and options with descriptions about the sort command,
we use the --help option with the sort command as shown below.

$ sort -–help

- To check in which version the sort command is working, we use the --


version option with the sort command in the Linux system as shown
below.

$ sort -–version

- Using the -o option is functionally the same as redirecting the output to a file.
$ sort file.txt > sortedfile.txt

OR

$ sort –o sortedfile.txt file.txt

- -n Option: To sort a file numerically used –n option. -n option is also predefined in


Unix as the above options are. This option is used to sort the file with numeric data
present inside.

$ sort –n file.txt

- -nr option: To sort a file with numeric data in reverse order we can use the
combination of two options as stated below.

$ sort –nr file.txt

3
- -c option: This option is used to check if the file given is
already sorted or not & checks if a file is already sorted pass
the -c option to sort. This will write to standard output if there
are lines that are out of order. The sort tool can be used to
understand if this file is sorted and which lines are out of
order

$ sort -c file1.txt

- -u option: To sort and remove duplicates pass the -u option to


sort. This will write a sorted list to standard output and remove
duplicates.

$ sort -u file1.txt

- -M : To sort by month pass the -M option to sort. This will write a sorted list to
standard output ordered by month name.

$ sort -M months.txt
$ cat months.txt
Output :
January
February
March
August
September

- -R: If you want to arrange the data in any sequence and if there are no criteria for
sorting, then random sorting is preferred. Consider a file named 'sortedfile.txt'

$ sort -R sortedfile.txt

- Comparing two sorted files using the following Linux command:

$ comm <(sort file1.txt) <(sort file2.txt)

- One of the most useful commands of sorting is to sort the data of different files at
a time. This can be done by using the find command. The output of the find
command will act as an input for the command after the pipe that is a sort
command. Find keyword is used to give only one file on each line, or we can say
that it uses a break after each word. For instance, let’s consider three files named
sample1.txt, sample2.txt, and sample3.txt. Here the “?” represents any number
that is followed by the word “sample”. Find will fetch all three files and their data
will be sorted with the help of a sort command with the pipe initiative:

$ find –name "file*.txt" –print0 | sort –files0-from=-

- Sort for Column Number: Let us have an example of a text file in which
there are names and marks of the students. We want to organize them in
ascending order on the basis of second column. So we will use the keyword –k in
the command. Whereas –n is used for numerical sorting.

$ sort –k 2n file.txt 4
a) uniq: Removes Duplicate Lines from a File

$ cat months
$ uniq months

Uniq -c or uniq – -count : It tells how many times a line was repeated by displaying a
number as a prefix with the line
$ uniq -c months

Uniq -d or uniq – -repeated : It only prints the repeated lines and not the lines
which
aren’t repeated.

$ uniq -d months

Uniq -u : It allows you to print only unique lines

$ uniq -u months

Uniq -w N : It only compares N characters in a line

$ uniq -w 4 months

Uniq -i or uniq – -ignore case : By default, comparisons done are case sensitive but
with this option case insensitive comparisons can be
made.

$ uniq -i months

b) diff: Compares Two Files

$ diff -u months months2

5
c) | (Pipe): Communicates Between Processes.
Pipes help combine two or more commands and are used as input/output concepts
in a command

$ cat months | head


$ cat months | head -1
$ cat months | sort
$ cat months | sort | uniq > months2

d) date: Displays the Time and Date

$ date

e) unix2dos: Converts Linux Files to Windows Format

$ unix2dos months

Or in vice versa

$ dos2unix months

6
FAHAD BIN SULTAN UNIVERSITY
College of Computing
Linux Lab CSC 371L

Experiment 3
Course Outline

Instructors:
1. Mohammad A. Mezher, PhD. Email: [email protected]
2. Hiba Al-Senawi Email: [email protected]

Experiment Objectives:
The objectives of this experiment are
 To use process and job control and apply knowledge learned in the class.
 To locate and search utilities by displaying the full pathname of the file for the utility.
 To teach the student about Unix processes, and how to control their own processes.
 To list basic information about processes
 To Prints the current priority value.
 To let you to kill processes
 To alters the scheduling priority of one or more running processes
 To continues a stopped job by running it in the foreground
 To place a job in background

Suggested Reference Readings:

[1] A practical guide to Ubuntu Linux, Mark Sobell, Third Edition.

1
Experiments:

Using LUI do the following

a. Which: the which utility locates utilities by displaying the full pathname of the
file for the utility.

$ which

b. Whereis: The whereis utility searches for files related to a utility by looking in
standard locations instead of using your search path.

$ whereis

c. PS Process: list basic information about your processes


$ ps
$ ps –f

d. nice: Prints the current priority value. Note: Try to open pico editor and to save
the file by the name myfile.txt

$ nice
$ nice -13 pico myfile.txt

e. renice: alters the scheduling priority of one or more running processes, priority
range goes from -20 (highest priority)
$torenice
19 (lowest priority).

f. kill: lets you kill your processes (you cannot kill the processes of other users). [
kill PID]

2
$ Kill 83

g. fg: continues a stopped job by running it in the foreground

$ fg

h. bg: continues a stopped job by place a job in background

Normally user can run a job in background, by adding & at end of the
command (ex: sleep 10 &).

$ bg

Daemons: System Processes that run in background without user request


such as init, lpsched, sac, cron….

Processes Commands--Summary
Stop (don't kill) the foreground job, and then return to
control-z
the shell

jobs Check the status of jobs in the current session

ps -u
Check the status of processes, including those from other
usernam
sessions. On BSD systems, use 'ps -gx'.
e

Kill a job, by specifying its job number after the percent


kill -9 %1
sign

kill -9 123 Kill a process, by specifying its process id (PID) number

bg Run the most recently stopped job in the background

fg Bring most recently backgrounded job to the foreground

Bring a job to foreground by specifying its job number


fg %1
after the percent sign

3
FAHAD BIN SULTAN UNIVERSITY
College of Computing
Linux Lab CSC 371L

Experiment 1
Course Outline

Instructors:
1. Tareq Alawneh, Email: [email protected]

Course Description:
1. Using GUI and LUI
2. Create new folders in GUI
3. Create Text File using GUI
4. Working with Files (copy, move, remove, print, and display)

Course Objectives:
The objectives of these exercises are
 To use and apply knowledge learned in the class.
 To give you experience in using terminal to login using the user id and password
provided, and then to logout
 Get help on the ls command
 What is your current directory (present working directory)
 Use the cd command to go to the “root” of the file system
 Use the cd command to go to your home directory
 Use the pwd command to display the name of the present working director
 Use the proper file instructions to copy, remove, move, display and print txt files

Suggested Reference Readings:

[1] A practical guide to Ubuntu Linux, Chapter 5. Mark Sobell, Third Edition.

1
Experiments:

1. Using GUI do the following


a. Create a folder named by you “your_name”.
b. Using the editor of your choice, create a small file named “test”
c. Save your “test” file into “your_name” folder
2. Using LUI do the following
a. ls: Lists the Names of Files
b. cat: Displays a Text File
c. rm: Deletes a File

$ ls
practice
$ cat practice
This is a small file that I created
with a text editor.
$ rm practice
$ ls
$ cat practice
cat: practice: No such file or directory

d. cp: Copies a File (cp source-file destination-file)

$ ls
memo
$ cp memo memo.copy
$ ls
memo memo.copy

e. mv: Changes the Name of a File (mv existing-filename new-filename)

$ ls
memo
$ mv memo memo.0130
$ ls
memo.0130

2
f. lpr: Prints a File

Prints the file named report

$ lpr report

Prints the same file on the printer named mailroom:

$ lpr -P mailroom report

You can see which jobs are in the print queue by giving an
lpstat –o command or by using the lpq utility:

$ lpq

You can use the job number (86 in this case) with the lprm
utility to remove the job

$ lprm 86

g. hostname: Displays the System Name

$ hostname
bravo.example.com

3
1- List the name of File
$ls
2- Display the contact of text file named test
$cat test
3- Copies a file TTa (name the copied file to TTb)
$cp TTa TTb
4- Printing the file named exam on the printer named School
lpr -P School exam
5- Displays the System Name
$hostname
6- It tells how many times a line was repeated by displaying a number
tail
7- What command is used to add printing jobs to the queue
lpr
8- Displays the Time and Date
$date
9- Write a command that clears the contents of your terminal display
$clear
10- Converting text file from Linux to Windows
unix2dos
11- Displaying the first 10 lines of the file named final_exam
$head -10 final_exam
12- Compares between two files named by " test1" and "test2"
$diff -u test1 test2
13- Find a string "aqw" in file named Car_three
$grep 'aqw' Car_three
14- Creating a folder named by " Lap15"
$mkdir Lap15
15- Displaying a file named test in Order
$sort test
16- Displaying only the unique lines of a text file named " weeks"
$uniq -u weeks
17- Remove file named by exam3
$rm exam3
18- What is the Linux command used to search for files related to a utility by looking in
standard locations
$whereis
19- Continues a stopped job by place a job in background
$bg
20- What command do you type in to change the current directory
cd
21- Process command that kill a process (11)
$kill -9 11
22- Displaying the full format list in process
$ps -es
23- Linux command will terminate a job by specifying its job
$kill -9%
24- What is Daemons
System Processes that run in background without user request
25- The Linux command which is used to change the scheduling priority of one or more
running processes
$renice
26- Linux command used to locates utilities by displaying the full pathname of the file
for the utility.
$which
27- Killing the job with the PID of 332
$kill -9%332
28- Create link between files " test_a" and "test_b"
$ln test_a test_b
29- Linux command that displays the path name of the working directory
$pwd
30- What is the purpose of this command: $ ls -l file_a file_b file_c file_d
Compare files
31- What is the purpose of this command: $ chmod a+rw
add permission write to file
32- Created and maintained by Linux itself. This type of variable defined in CAPITAL
LETTERS
system variables
33- Linux command that to get information about all running process
$ps -ag
34- Linux command that to stop all process except your shell
$kill 0
35- Define variable called 'num' having value 20
$num=20
36- Add 3+5 using shell script
$expr 3+5
37- Linux command that everyone has access to read/write/execute
chmod777
38- What is the command is used to remove the directory
rmdir
39- Write command that read from user
read
40- The shell script command that can be used to print the current user information
$echo "Hello $USER"
FAHAD BIN SULTAN UNIVERSITY
College of Computing
Linux Lab CSC 371L

Experiment 4
Course Outline

Instructors:
1. Mohammad A. Mezher, PhD. Email: [email protected]
2. Hiba Al-Senawi Email: [email protected]

Experiment Objectives:
The objectives of this experiment are
 To use process and files structure knowledge learned in the class.
 To create hard links between folders.
 To create symbolic links.
 To use various types of parameters for hard and symbolic links
 To remove a link.
 To change access permissions granted to users
 Table of commands summary

Suggested Reference Readings:

[1] A practical guide to Ubuntu Linux, Mark Sobell, Third Edition.

1
Experiments:

Using GUI do the following


1. Create a folder with your name
2. Inside your folder create two text files (file_a, file_c)

Using LUI do the following

a. ln: CREATES A HARD LINK


$ pwd

/home/liveuser

$ ln file_a file_b

The output file looks like:

b. ln VERSUS cp

$ cat file_a

This is file A.

$ ln file_a file_b

$ cat file_b

This is file A.

$ pico file_b

...

$ cat file_b

This is file B after the change.

$ cat file_a

This is file B after the change.

2
$ cat file_c

This is file C.

$ cp file_c file_d

$ cat file_d

This is file C.

$ pico file_d

...

$ cat file_d

This is file D after the change.

$ cat file_c

This is file C.

c. ls –l : use ls with the –l option, followed by the names of the files you
want to compare
$ ls -l file_a file_b file_c file_d

d. ls –i: The –i option lists the inode number for each file.

$ ls -i file_a file_b file_c file_d

e. ln -s: Creates a Symbolic Link which displays the name of the link and the
name of the file it points to.
$ ln -s /home/liveuser/sum /tmp/s3

$ ls -l /home/liveuser/sum /tmp/s3

-rw-rw-r-- 1 liveuser liveuser 38 Jun 12 09:51


/home/liveuser/sum

lrwxrwxrwx 1 liveuser liveuser 14 Jun 12 09:52


/tmp/s3 -> /home/liveuser/sum

$ cat /tmp/s3

This is sum.

3
f. rm: Removes a Link

$ ls -l sum

-rw-r--r-- 1 alex pubs 981 April 22 02:05 sum

$ ln -s sum total

$ rm sum

$ cat total

cat: total: No such file or directory

$ ls -l total

lrwxrwxrwx 1 alex pubs 6 May 24 11:09 total -> sum

g. chmod: Changes Access Permissions (all with adding new permission)

$ chmod a+rw letter.0610

$ ls -l letter.0610

-rw-rw-rw- 1 alex pubs 3355 May 2 10:52 letter.0610

h. $ chmod:
chmod o-rx check_spell
Changes Access Permissions (others with removing old
$ permission)
ls -l check_spell

$ chmod o-rx
-rwxr-x--- 2 check_spell
alex pubs 852 May 5 14:03 check_spell

$ ls -l check_spell

-rwxr-x--- 2 alex pubs 852 May 5 14:03 check_spell

Links Commands--Summary
ln To create a hard link

4
use ls with the –l option, followed by the names of
ls –l
the files you want to compare

ls –i The –i option lists the inode number for each file.

ln - -
symboli Creates a Symbolic Link which displays the name of
c (or - - the link and the name of the file it points to.
s)

rm Removes a Link

chmod Changes Access Permissions (all with adding new


+ permission)

chmod - Changes Access Permissions (others with removing


old permission)

5
FAHAD BIN SULTAN UNIVERSITY
College of Computing
Linux Lab CSC 371L

Experiment 5
Course Outline

Instructors:
1. Mohammad A. Mezher, PhD. Email: [email protected]
2. Hiba Al-Senawi Email: [email protected]

Experiment Objectives:
The objectives of this experiment are
 How to write shell script
 Variables in shell
 How to define User defined variables (UDV)
 How to print or access value of UDV (User defined variables)
 echo Command
 Exit Status
 Shell Arithmetic
 The read Statement
 Shells (bash) structured Language Constructs
 Decision making in shell script ( i.e. if command)
 if...else Statements

Suggested Reference Readings:

[1] A practical guide to Ubuntu Linux, Mark Sobell, Third Edition.

1
Experiments:

Using LUI do the following

a. To write first shell script that will print "Knowledge is Power" on screen.

$ pico first
#
# My first shell script
#
clear
echo "Knowledge is Power"

b. After saving the above script, you can run the script as follows:

$ chmod 755 first


$ .first

c. Write following shell script, save it and execute it

$ pico ginfo
#
#
# Script to print user information who currently login
, current date & time
#
clear
echo "Hello $USER"
echo "Today is ";date
echo "Number of user login : " ; who | wc -l
echo "Calendar"
cal

d. In Linux (Shell), there are two types of variable:


(1) System variables - Created and maintained by Linux itself. This type
of variable defined in CAPITAL LETTERS.
(2) User defined variables (UDV) - Created and maintained by user. This
type of variable defined in lower letters.

$ set
$ echo $USERNAME
$ echo $HOME
2
e. To define UDV use following syntax
$ no=10# this is ok
$ 10=no# Error, NOT Ok, Value must be on right side of = sign.

To define variable called 'vech' having value Bus


$ vech=Bus

To define variable called n having value 10


$ n=10
$ echo $n

f. How to print or access value of UDV


$ pico usrvar
#
#
# Script to test MY knowledge about variables!
#
myname=Mohammad
myos = Ubuntu
myno=5
echo "My name is $myname"
echo "My os is $myos"
echo "My number is myno, can you see this number??"

g. Shell Arithmetic

$ expr 1 + 3
$ expr 2 - 1
$ expr 10 / 2
$ expr 20 % 3
$ expr 10 \* 3
$ echo `expr 6 + 3

h. The read Statement

$ pico sayH
#
#Script to read your name from key-board
#
echo "Your first name please:"
read fname
echo "Hello $fname, Lets be friend!"

3
i. Run it as follows

$ chmod 755 sayH


$ . sayH

j. Decision making

$ bc
5+2
5-2
5/2
5>2
5<2

k. Exit Status

$ cat foo
$ echo $?

l. Write shell script as

$ cat > showfile


#!/bin/sh
#
#Script to print file
#
if cat $1
then
echo -e "\n\nFile $1, found and successfully echoed"
fi

m. Run above script as:

$ chmod 755 showfile


$. showfile foo

You might also like