Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

I

Jubail University College


Lab – 4

CS314 Operating System

Semester 391
Introduction
In this lab, you will use the Linux command line to manage files and folders and perform some
basic administrative tasks.

Recommended Equipment
A computer with a Linux OS, either installed physically or in a virtual machine

Step 1: Access the command line.


a. Log on to a computer as a user with administrative privileges. The account smahmud74 is used
as the example user account throughout this lab.

b. To access the command line, click Dash, and type terminal in the search field and press Enter. The
default terminal emulator opens. OR press Ctrl – Alt + T to open the terminal.

c. In the current directory, use the mkdir command to create three new folders: ITEfolder1,
ITEfolder2, and ITEfolder3. Type mkdir ITEfolder1 and press Enter. Create ITEfolder2 and
ITEfolder3.

d. Type ls to verify the folders have been created.

Step 2: Create text files & Use redirection operator.


a. Navigate to the /home/smahmud74/ITEfolder1 (~/ITEfolder1) directory. Type cd ITEfolder1 at the
prompt.
b. Type cat > test2.txt at command prompt. The command awaits for input from user, type 10 fruit names
in each line and press CTRL+D (hold down Ctrl Key and type ‘d‘) to exit. You can see content of the file
using the cat command: cat test2.txt.
c. Type cat > test1.txt at the command prompt. Now, type 15 country names in each line. The text will be
written in test1.txt file.

d. Copy both the files test1 and test2 into ITEfolder2 from the current folder. Use ls command to check the
content of ITEfolder2 without moving to ITEfolder2.

e. Appending Standard Output with Redirection Operator >>: Appends in existing file with ‘>>‘ (double
greater than) symbol. Here, contents of test1 file will be appended at the end of test2 file.

a. Type cat test2.txt to see the content of the test2.txt. You won’t be able to see the full content of
the file as the terminal scrolls down and shows only the last part of the file.
b. To see the content of a file page by page, use command more. Type cat test2.txt | more.

You can also use these keys to move through the output:
[Space] - scrolls the display, one screenful of data at a time
[Enter] - scrolls the display one line
c. You can also append at the end of a single text file using the >> operator as follows:

Use the cat command to check the content of test1.txt. Using >> operator, merge the files
test1.txt and test2.txt again.

Step 3:
i. less command:
The command less writes the contents of a file onto the screen a page at a time. Type

Press the [space-bar] if you want to see another page, and type [q] if you want to quit reading.
As you can see, less is used in preference to cat for long files.

ii. head
The head command writes the first ten lines of a file to the screen. First clear the screen then type

Now, type
head -3 test2.txt
What difference did the -3 do to the head command? (Displays only the first three lines of the file).
The command does take any positive number as argument/option.
iii. tail
The tail command writes the last ten lines of a file to the screen. Clear the screen and type

 How can you view the last 15 lines of the file?


tail -15 test2.txt

iv. wc (word count)


A handy little utility is the wc command, short for word count. The ‘wc‘ command without passing any
parameter will display a basic result of ”test2.txt‘ file.

The three numbers shown above are 45 (number of lines), 49 (number of words) and 322 (number of bytes)
of the file. To do a word count on test2.txt, type

All possible options for wc command:

Step 4:
v. Redirecting the Input:
i. We use the < symbol to redirect the input of a command. The command sort
alphabetically or numerically sorts a list. Type

Press Ctlr + D to stop taking input. The output will be

ii. Using < you can redirect the input to come from a file rather than the keyboard. For
example, to sort the list of country names, type

iii. To output the sorted list to a file, type,

Check the content of test1_sort.txt using cat command.

Step 5:
Pipes
i. To see who is on the system with you, type
who
ii. One method to get a sorted list of names is to type,

This is a bit slow and you have to remember to remove the temporary file called names when
you have finished. What you really want to do is connect the output of the who command
directly to the input of the sort command. This is exactly what pipes do. The symbol for a pipe
is the vertical bar |.

iii. The following command will give you same result as above, but quicker and cleaner.

iv. One more example, if you want to list all the files and folders in your current directory and get them
sorted, Type:

v. Display the number of lines in your test1_sort.txt file using the pipe and cat command as follows:

vi. Sort the content of test2.txt and display the first 10 lines of the file

Step 6:
The * wildcard
i. The character * is called a wildcard, and will match against none or more character(s) in a file
(or directory) name. For example, in your ITEfolder1 directory, type

The above command is displaying all the files in the current directory that starts with “test…….”.
ii. Try typing
This will list all files in the current directory ending with .txt
iii. The ? wildcard
The character ? will match exactly one character. So ?ouse will match files like house and mouse, but
not grouse. Try typing

You might also like