File Handling and I/O Redirection In Ubunto

File Handling and I/O Redirection In Ubunto

This Lecture teaches you the following topics:

  • Handling Files in Linux using CLI
  • Using I/O redirection in

Working with Files

In this section, we will introduce the file related commands.

 Creating Empty Text File

  • In Linux, there are several ways to create an empty text file.
  • Most commonly the touch command is used to create a file.
  • We can create a file with name myfile.txt using touch command as given below:

  • Another, way to create a file in Linux is the cat command.

  • Similarly, a file can be created using some editors. For example, to create a file using gedit editor.

Reading the File Contents

  • cat command can also be used to read the contents of a file.

  • Another option to view the contents of a text file is the use of less command.

  • Similarly, an editor can also be used to view the contents of a file.

Appending text files

  • cat command is also used to append a text file.
  • Suppose we want to add some text at the end of myfile.txt. 

  • Now, type the text and enter ctrl+d to copy the text to myfile.txt.

Combining multiple text files

  • Using cat command, we can view the contents of multiple files.
  • Suppose, we want to view the contents of file1, file2 and file3, we can use the cat command as follows:

  • Similarly, we can redirect the output of multiple files to file instead of screen using cat command.
  • Suppose, in the above example we want to write the contents of file1, file2 and file3 into another file file4 we can do this as shown below:

Determining File Type

To determine the type of a file we can use the file command. The syntax is given below:

Redirecting I/O

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 Unix 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 write the output on another file instead of standard output i.e. screen. To do this, we use the redirection operator i.e. <. For example, we want to write the output of ls command in a text file myfile.txt instead of screen. This can be done as given below:

If we write the output of some other program to myfile.txt using > operator, its previous contents will be overwritten. Now, if want to append the file instead of over-writing we can use the << operator.

Redirecting Standard input

Redirecting input enables us to take input from another file instead of standard input i.e. keyboard. We have already discussed this in previous section while discussing cat command where we used the text file as input instead of keyboard and wrote it to another file.


The ability of commands to read data from standard input and send to standard output is utilized by a shell feature called pipelines. Using the pipe operator “|” (vertical bar), the standard output of one command can be piped into the standard input of another:

Home Activities:

  1. Considering the lab activities, perform the following tasks
    1. Go to Desktop directory
    2. write the contents of mynewfile to newfile
    3. view the output of both mynewfile and newfile on screen
    4. write the combined output of mynewfile and newfile to a third file out-put-file


  1. Long list all files and directories in your system and write out-put on a text-file.

Related Links  to Operating System topics

Operating system Course content

Lab Practice Task


#Operating System complete course #Operating System past paper #Operating System-project #Computer Science all courses  #operating system Problem with source code#University Past Paper #Programming language #Question paper #old paper #Operating System-Functions and History #Generations of Operating System #Functions of an Operating System #Components of Operating System #Types of Operating System #Services of Operating System #Properties of Operating System #Processes in Operating System #Process Scheduling in Operating System #Introduction to Linux Ubunto #Installation with virtual Box #Writing Linux Commands #Navigation in File System and Directory Management in Ubunto using CLI #File Handling and I/O Redirection In Ubunto #File Access Permission in Linux #Text Processing Tools and Basic System Configuration Tools in Linux #Package Management in Linux #How to manage processes in Linux #Compiling and Executing C++ programs in Linux #System Calls #Introduction To Shell Programming

Scroll to Top