0% found this document useful (0 votes)
10 views

OS Exp.4 Working With File Content

Uploaded by

Alison Roman gg
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

OS Exp.4 Working With File Content

Uploaded by

Alison Roman gg
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Experiment No.

4
Redirection: "Working with File Contents"

Objectives
The redirection or what is called “I/O” redirection stands for input/output and with the
facility that you can redirect the input and output of commands to and from files, as
well as connect multiple commands together into powerful command pipelines. To
show off this facility, we will use the following commands:
• cat - command
• tac- command
• head - command
• tail - command

Introduction
Many of the programs that we have used so far produce output of some kind. This output
often consists of two types. First, we have the program's results; that is, the data the program
is designed to produce, and second, we have status and error messages that tell us how the
program is getting along. If we look at a command like ls, we can see that it displays its
results and its error messages on the screen. Keeping with the Linux theme of “everything
is a file,” programs such as ls actually send their results to a special file called standard
output (often expressed as stdout) and their status messages to another file called standard
error (stderr). By default, both standard output and standard error are linked to the screen
and not saved into a disk file. In addition, many programs take input from a facility called
standard input (stdin) which is, by default, attached to the keyboard.

I/O redirection allows us to change where output goes and where input comes from.
Normally, output goes to the screen and input comes from the keyboard, but with I/O
redirection, we can change that. Redirecting Standard Output I/O redirection allows us to
redefine where standard output goes. To redirect standard output to another file besides the
screen, we use the " > " redirection operator followed by the name of the file. Why would
we want to do this? It's often useful to store the output of a command in a file.

Redirecting Standard I/O & Working with File Contents:


• cat Command: The cat (Short of "concatenate") command is one of the most
frequently used command in Linux. cat command allows us to redirect output in terminal
or files, concatenate files, create single or multiple files, and view contain of file, as
shown:

1 Prepared by Dr. Fatemah Al-Assfor & Dr. Atheel Kadhum OS Lab. 3rd Class - Exp.4 2023
Example: Display on the terminal contents of file adduser.confg which is stored in
directory etc in file system. Write the result in your report.

cat /etc/adduser.conf

1-Concatenate
cat is short for "concatenate" . One of the basic uses of cat is to concatenate files into
a bigger (or complete) file. This is can be achieved using for example echo command
to store data in files then concatenate files using the cat command, as shown in the
following example.

Example: Create four files: (L1, L2, L3, and L4) using touch command to store the
following phrases. Display the Contents of the files together and write the result:
L1: Memory system consists of: Register file
L2: Cache memory
L3: Main Memory
L4: Secondary memory
echo Memory system consists of: Register file > L1
echo Cache memory > L2
echo Main memory > L3
echo Secondary memory > L4
cat L1 L2 L3 L4

2- Create Files
The cat command can also be used to create text files, as shown:
cat > filename.txt

Then type one or more lines inside your text file, finishing each line with the "Enter
Key". After typing your last line, press (ctrl + d) together to end your file. By pressing
(ctrl + d), the system will send an EOF (End Of File) to the running process to end the
cat command.
Example: Create a summer.txt file, type the following phrase inside the file, display
the content of files, and write your result.

cat > summer.txt


It is a very nice day!
cat summer.txt

2 Prepared by Dr. Fatemah Al-Assfor & Dr. Atheel Kadhum OS Lab. 3rd Class - Exp.4 2023
3- Custom & Marker
One of the features of cat command, it can be used to choose an end marker for the
command itself with " << " . This construction is called "here directive" and will end
the cat command.
Example: Create a gpr.txt file, put an end marker named 'stop' to your file and write
the following phrase inside the file. Display the content of gpr.txt file on the screen and
write the result.
There are two types of registers
Dedicated registers that used to store data
General purpose registers that used to store, shift, rotate, increment
Decrement, clear, and set

cat > gpr.txt <<stop


> There are two types of registers
> Dedicated registers that used to store data
> General purpose registers that used to store, shift, rotate, increment
> Decrement, clear, and set
> stop
> cat register.txt

4- Use Standard Output with Redirection the operator (Copy Files)


Cat- command can also be used to copy files like cp- command, as shown in the following
example:
Example: using cat command to create two files t1.txt and t2 file. Write the following
phrase inside the file: The world is much brighter with my family
Display the content of t1.txt , then copy the file to a new file t2.txt.

cat > t1.txt


The world is much brighter with my family
cat t1.txt > t2.txt
cat t2.txt

3 Prepared by Dr. Fatemah Al-Assfor & Dr. Atheel Kadhum OS Lab. 3rd Class - Exp.4 2023
5- Appending Standard Output with Redirection Operator
Cat- command can also be used to append in existing file with (; ) symbol, as shown in the
following example:
cat gpr.txt; summer.txt; t1.txt

6- Redirecting Multiple files Contain in a Single File


Example: Create a file test4.txt. Then, redirect the outputs of the files gpr.txt and t2.txt in
the newly file test4.txt.

cat gpr.txt t2.txt >> test4.txt


cat test4.txt

7- Options of cat Command


cat - command have several useful options as shown in Table I
Table I: cat Command Options
CLI option Meaning

-A Equivalent to -vET.

-b Number the nonempty output lines.

-e Show the End of Line Equivalent to -vE.

-E Display $ at end of each line.

-n Number all output lines.

-s Remove the blank lines from the output

-t Equivalent to -vT.

-T Display TAB characters as ^I

-v Show nonprinting i.e. use ^V and ^M notation, except for LFD and TAB.

Example: Display the line numbers of file gpr.txt:

4 Prepared by Dr. Fatemah Al-Assfor & Dr. Atheel Kadhum OS Lab. 3rd Class - Exp.4 2023
cat -n gpr.txt
1 There are two types of registers
2 Dedicated registers that used to store data
3 General purpose registers that used to store, shift, rotate, increment
4 Decrement, clear, and set

- Display the total number of lines in the text file


To print out or displays the total number of lines in the text file, use the option:

cat filename |wc –l

• tac- Command:
tac command is just the opposite of cat command, (i.e. it will display the contents
of a file in an opposite order).
Example: Create a count .txt file, put an end marker named 'stop' to your file. Display
the content of count.txt file on the screen, use tac command to display the contents of
the file in an opposite order, and write the result
cat > count.txt <<stop
> one
> two
> three
> four
> five
> stop

cat count.txt
one
two
three
four
five
tac count.txt

• head- Command:
head- command is used to display an n- lines of a file. Where n: is the number of files
required to display.

5 Prepared by Dr. Fatemah Al-Assfor & Dr. Atheel Kadhum OS Lab. 3rd Class - Exp.4 2023
Example: Display the first 6- lines of the file passwd in directory etc. Write your
results.
head -6 /etc/passwd

• tail- Command:
This command is similar to the tail command, but it will display the last n- lines of a file.
Example: Display the last 5- lines of the file passwd in directory etc. Write your results.

tail -5 /etc/passwd

Exercises: Make the Desktop your working directory to perform all the following
exercises:
o Using cat command to create a win.txt file, write the following phrases in the file:
Happiness depends upon ourselves

Time moves in one direction, memory in another


Every moment is a fresh beginning

Never regret anything that made you smile


Be yourself
Life is trying things to see if they work
Then, do the following:
- Copy the content of the file to a new file called w.txt
- Display the total number lines of w.txt
- Display the content of win without empty lines
o Use cat command to create a file named team.txt. Write the following phrases in the file.
Nothing is impossible. The word itself says “I’m possible!
Everything has beauty, but not everyone sees it

Simplicity is the ultimate sophistication


We need much less than we think we need
Be so good they can’t ignore you
You become what you believe
- Display the contents of team.txt with line numbers, followed by the contents of the
file: /etc/passwd.
- Display first two lines only followed by the content of file counter.txt.
- Display the file content in reverse order.

o Display first 12 lines of file /etc/services


o Display the last line of file /etc/passwd
6 Prepared by Dr. Fatemah Al-Assfor & Dr. Atheel Kadhum OS Lab. 3rd Class - Exp.4 2023

You might also like