Linux Lab CSC 371L 2 - Merged
Linux Lab CSC 371L 2 - Merged
Linux Lab CSC 371L 2 - Merged
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
1
Experiments:
$ cat months
$ cat months
$ head -1 months
$ cat months
$ sort months
$ tail -5 months
$ 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 the contents of file ‘ file1.txt‘ on the basis of 2nd column (which represents
number) as shown below
- 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
$ sort -–version
- Using the -o option is functionally the same as redirecting the output to a file.
$ sort file.txt > sortedfile.txt
OR
$ 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.
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
$ 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
- 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:
- 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 months
$ 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
5
c) | (Pipe): Communicates Between Processes.
Pipes help combine two or more commands and are used as input/output concepts
in a command
$ date
$ 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
1
Experiments:
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
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
$ fg
Normally user can run a job in background, by adding & at end of the
command (ex: sleep 10 &).
$ bg
Processes Commands--Summary
Stop (don't kill) the foreground job, and then return to
control-z
the shell
ps -u
Check the status of processes, including those from other
usernam
sessions. On BSD systems, use 'ps -gx'.
e
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
[1] A practical guide to Ubuntu Linux, Chapter 5. Mark Sobell, Third Edition.
1
Experiments:
$ 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
$ ls
memo
$ cp memo memo.copy
$ ls
memo memo.copy
$ ls
memo
$ mv memo memo.0130
$ ls
memo.0130
2
f. lpr: Prints a File
$ lpr 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
$ 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
1
Experiments:
/home/liveuser
$ ln file_a file_b
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
$ cat file_a
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
$ 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.
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
$ cat /tmp/s3
This is sum.
3
f. rm: Removes a Link
$ ls -l sum
$ ln -s sum total
$ rm sum
$ cat total
$ ls -l total
$ ls -l 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
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
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
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
1
Experiments:
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:
$ 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
$ 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.
g. Shell Arithmetic
$ expr 1 + 3
$ expr 2 - 1
$ expr 10 / 2
$ expr 20 % 3
$ expr 10 \* 3
$ echo `expr 6 + 3
$ 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
j. Decision making
$ bc
5+2
5-2
5/2
5>2
5<2
k. Exit Status
$ cat foo
$ echo $?