Unix Tutorial


What is Unix?

Getting started

Hardware

Software

Basic Commands

vi Commands

 

Files

File Commands

Different types of file

File Redirection

Permissions

Pipelines / Filters

 

Directory

Directory Structure

Directory Command

Shell

Features/Different types of shell

Changing your shell

Compiling Program

Networking

Commands

Reference

Reference Commands

online help : Manpages

Useful links/Books

 

Search Engine

File Redirection


File Redirection is the special commands in Unix that instruct the computer to read from a file, write to a file, or even append information to an existing file.When you issue a Unix command like date, the output of the command goes to what is called Standard output.

Type at the prompt

%date

output will look like

Thu Oct 11 16:03:06 EDT 2001

Each of these acts can be accomplished by placing a file-redirection command in a regular command line:

< redirects input : For example to run prog1 but read data from a file input_file instead of the keyboard, you would type

prog1<input_file

> redirects output : For example to run prog1 and write data to output_file instead of the screen, you would type

prog1>output_file

Above two commands can be combined as follows

prog1 < input_file > output_file

>> redirects output and appends the information to the existing file

To show application of above commands, here are the few examples :

  1. Log in to your account and create an empty file using the touch command:
  1. %touch testme

  2. First, use this empty file to learn how to redirect output.Use ls to list the files in your directory, saving them all to the newly created file.

    %ls -l testme

    -rw-rw-r-- 1 cs1234 0 Nov 15 09:11 testme

    %ls -l > testme

    %ls -l testme

    -rw-rw-r-- 1 cs1234 120 Nov 15 09:12 testme

    Here when we redirected the output, nothing was displayed on the screen; there was no visual confirmation that it worked. But it did, as you can see by the increased size of the new file.

  3. Instead of using cat or more to view this file, try using file redirection:

    %cat < testme

    total 126

    drwx------ 2 cs1234 512 Nov 6 14:20 Archives/

    drwx------ 2 cs1234 512 Nov 16 21:55 InfoWorld/

    likewise here will be all the list of files that are shown in testme.

    The results are the same as if you had used ls command, but the output file is saved too.You now can easily print the file or go back to it later to compare the way it looks with the way your files look in the future.

  4. Use the ls command to add some further information at the bottom of the testme file by using >>, the append double-arrow notation:

    %ls -FC >> testme


Removing the duplicate lines using function uniq

  • Create a file that has duplicate lines for example

%tail -1 testme > lastline

%cat lastline lastline lastline lastline > newtest2

%cat newtest2

News/ abc.txt testme

News/ abc.txt testme

News/ abc.txt testme

News/ abc.txt testme

Now you can see what uniq does

%uniq newtest2

News/ abc.txt testme

  • Obtain a count of the number of occurrences of each line in the file.The -c flag does that job.
  • %uniq -c newtest2

    This shows that this line occurs four times in the file. Lines that are unique have no number preface.