0% found this document useful (0 votes)
33 views26 pages

ITEC3116 SNAL Lecture 14 File Redirection

This document discusses input/output redirection in Linux. It begins with a recap of changing file permissions and an agenda that includes input/output redirection and file descriptors. It then explains what redirection is and how the > symbol redirects standard output and < symbol redirects standard input. Examples are given of redirecting command output to files and devices. File descriptors are described as numbers associated with files and streams in Linux. Error redirection routes errors to files instead of the screen. The document concludes with an assignment to demonstrate redirection commands and troubleshoot errors.

Uploaded by

zeeshan arif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views26 pages

ITEC3116 SNAL Lecture 14 File Redirection

This document discusses input/output redirection in Linux. It begins with a recap of changing file permissions and an agenda that includes input/output redirection and file descriptors. It then explains what redirection is and how the > symbol redirects standard output and < symbol redirects standard input. Examples are given of redirecting command output to files and devices. File descriptors are described as numbers associated with files and streams in Linux. Error redirection routes errors to files instead of the screen. The document concludes with an assignment to demonstrate redirection commands and troubleshoot errors.

Uploaded by

zeeshan arif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

System and Network

Administration Linux based

ITEC 3116
File Permissions in Linux
Lecture 14-Week 10,
April 2023.
Recap of the previous Lecture

How can we Change file/directory


permissions?
 Absolute mode
 Symbolic mode
Agenda for Today’s Class
 Input Output Redirection in
Linux/Unix
 File Descriptors (FD)
Input Output Redirection in
Linux/Unix Examples

What is Redirection?
 Redirection is a feature in Linux such
that when executing a command, you
can change the standard input/output
devices.
 The basic workflow of any Linux
command is that it takes an input and
give an output.
Redirection…

 The standard input (stdin) device is the


keyboard.
 The standard output (stdout) device is the
screen.
Redirection…

With redirection, the standard


input/output can be changed.
Output Redirection
 The '>' symbol is used for output
(STDOUT) redirection.
For Example:
ls –al > listings
Redirection…
For Example:
ls –al > Redirect_File_Info
 Here the output of command ls -al is
re-directed to file "Redirect_File_Info"
instead of your screen.
Redirection…
Redirection…

Note: Use the correct file name while


redirecting command output to a file.
If there is an existing file with the same
name, the redirected command will
delete the contents of that file and then
it may be overwritten.
Redirection…

If you do not want a file to be overwritten


but want to add more content to an
existing file then should you do?

By using '>>' operator with echo, can add


more content to an existing file.
Redirection…
Device Redirection

You can redirect standard output, to not just


files, but also devices!
Syntex: cat music.mp3 > /dev/audio
 The cat command reads the file music.mp3 and
sends the output to /dev/audio which is the
audio device.
 If the sound configurations in your PC are
correct, this command will play the file
music.mp3
 Note: sudo apt-get install …..
Input redirection

The '<' symbol is used for input(STDIN)


redirection.
 Example: The mail program in Linux can help
you send emails from the Terminal.
 You can type the contents of the email using
the standard device keyboard. But if you want
to attach a File to email you can use the input
re-direction operator in the following format.

 mail –s “subject” to address < Filename


 Note: sudo apt-get install …..
File Descriptors (FD)

 In Linux/Unix, everything is a file. Regular file,


Directories, and even Devices are files. Every
File has an associated number called File
Descriptor (FD).
 Your screen also has a File Descriptor. When a
program is executed the output is sent to File
Descriptor of the screen, and you see program
output on your monitor. If the output is sent to
File Descriptor of the printer, the program
output would have been printed.
Error Redirection
 Whenever you execute a
program/command at the terminal, 3
files are always open, viz., standard
input, standard output, standard
error.
Error Redirection

These files are always present whenever


a program is run. As explained before a
file descriptor, is associated with each of
these files.

File File Descriptor


Standard Input STDIN 0
Standard Output STDOUT 1
Standard Error STDERR 2
Error Redirection

 By default, error stream is displayed


on the screen. Error redirection is
routing the errors to a file other than
the screen.
Why Error Redirection?

 Error re-direction is one of the very popular


features of Unix/Linux.
 Frequent UNIX users will reckon that many
commands give you massive amounts of
errors.
 For instance, while searching for files, one
typically gets permission denied errors. These
errors usually do not help the person searching
for a particular file.
Why Error Redirection?

While executing shell scripts, you often do NOT


want error messages cluttering up the normal
program output.

 The solution is to re-direct the error messages


to a file.
Stream Redirection
 Linux includes redirection commands for each
stream. These commands write standard
output to a file.
 If a non-existent file is targeted (either by a
single-bracket or double-bracket command), a
new file with that name will be created prior to
writing.
 Commands with a single bracket overwrite the
destination's existing contents.
 Overwrite
 > - standard output
 < - standard input
 2> - standard error
 Commands with a double bracket do
not overwrite the destination's
existing contents.
 Append
 >> - standard output
 << - standard input
 2>> - standard error
Example
 cat > write_to_me.txt
 A , B, C
 View the contents of writetome.txt
using cat:
 cat write_to_me.txt
Redirect File Content

 Redirect cat to writetome.txt again, and enter


three numbers.
 cat > write_to_me.txt
 1 , 2, 3
 View the contents of writetome.txt using cat:
 cat write_to_me.txt
 The prior contents are no longer there, as the
file was overwritten by the single-bracket
command.
 Example
 Myprogram 2>errorfile
 Above we are executing a program names
myprogram.
 The file descriptor for standard error is 2.
 Using "2>" we re-direct the error output to a
file named "errorfile"
 Thus, program output is not cluttered with
errors.
Assignment # 2
Due Date: 10-04-2023
 Install and configure all packages related to Input &
Output Redirection and show the final result by taking
the screenshot of your (Input/output) Redirection
command queries.
 such as: 1
 cat music.mp3 > /dev/audio
 we found an error
 bash: /dev/audio: Permission denied,
 mail -s "Linux-Mail" emailAddress < filename
 Command 'mail' not found, but can be installed with:
 sudo apt install mailutils
 What are the reasons and how error can be
resolved?
 End of the Lecture

You might also like