OS Exp.4 Working With File Content
OS Exp.4 Working With File Content
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.
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.
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
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
-A Equivalent to -vET.
-t Equivalent to -vT.
-v Show nonprinting i.e. use ^V and ^M notation, except for LFD and TAB.
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
• 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