Javascript required
Skip to content Skip to sidebar Skip to footer

Ls Reading Directory . Bad File Descriptor

What is Redirection?

Redirection is a characteristic in Linux such that when executing a control, you can change the standard input/output devices. The basic workflow of whatever Linux control is that it takes an input and requite an output.

  • The standard input (stdin) device is the keyboard.
  • The standard output (stdout) device is the screen.

With redirection, the above standard input/output can be inverse.

In this tutorial, we volition learn-

  • Output Redirection
  • Input redirection
  • File Descriptors (FD)
  • Fault Redirection
  • Why Error Redirection?
  • Examples

Click here if the video is not accessible

Output Redirection

The '>' symbol is used for output (STDOUT) redirection.

Redirection in Linux/Unix - Demystified!

Example:

ls -al > listings

Here the output of control ls -al is re-directed to file "listings" instead of your screen.

Redirection in Linux/Unix - Demystified!

Note: Use the correct file proper noun 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 information technology may be overwritten."

If yous practise not desire a file to be overwritten but want to add together more content to an existing file, then you should use '>>' operator.

Redirection in Linux/Unix - Demystified!

You tin can redirect standard output, to not simply files, but as well devices!

$ true cat music.mp3 > /dev/audio

The cat command reads the file music.mp3 and sends the output to /dev/audio which is the sound device. If the audio configurations in your PC are correct, this command will play the file music.mp3

Input redirection

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

Redirection in Linux/Unix - Demystified!

Example: The mail program in Linux can help yous send emails from the Last.

You tin type the contents of the email using the standard device keyboard. Simply if you desire to adhere a File to email y'all tin use the input re-direction operator in the following format.

Postal service -s "Bailiwick" to-address < Filename

Redirection in Linux/Unix - Demystified!

This would adhere the file with the email, and it would be sent to the recipient.

The above examples were unproblematic. Permit's await at some advance re-direction techniques which make apply of File Descriptors.

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 likewise has a File Descriptor. When a program is executed the output is sent to File Descriptor of the screen, and you run into program output on your monitor. If the output is sent to File Descriptor of the printer, the program output would have been printed.

Fault Redirection

Whenever you execute a program/command at the terminal, 3 files are ever open, viz., standard input, standard output, standard error.

Redirection in Linux/Unix - Demystified!

These files are e'er nowadays 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 Mistake STDERR

two

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 y'all massive amounts of errors.

  • For example, while searching for files, i typically gets permission denied errors. These errors normally do not help the person searching for a detail file.
  • While executing vanquish scripts, you often practise NOT desire error messages cluttering up the normal program output.

The solution is to re-straight the mistake messages to a file.

Instance 1

$ myprogram 2>errorsfile

Redirection in Linux/Unix - Demystified!

Above we are executing a program names myprogram.

The file descriptor for standard error is 2.

Using "2>" we re-direct the mistake output to a file named "errorfile"

Thus, programme output is not cluttered with errors.

Example 2

Here is another case which uses observe statement –

find . -name 'my*' 2>error.log

Using the "find" command, we are searching the "." current directory for a file with "proper name" starting with "my"

Redirection in Linux/Unix - Demystified!

Example iii: Let'due south meet a more complex example,

Server Administrators often, list directories and store both error and standard output into a file, which can be candy later. Here is the command.

ls Documents ABC> dirlist 2>&1

Here,

  • which writes the output from one file to the input of some other file. ii>&1 means that STDERR redirects to the target of STDOUT (which is the file dirlist)
  • We are redirecting error output to standard output which in turn is beingness re-directed to file dirlist. Hence, both the output is written to file dirlist

Redirection in Linux/Unix - Demystified!

Summary

  • Each file in Linux has a respective File Descriptor associated with it
  • The keyboard is the standard input device while your screen is the standard output device
  • ">" is the output redirection operator. ">>" appends output to an existing file
  • "<" is the input redirection operator
  • ">&"re-directs output of one file to another.
  • Y'all tin re-direct error using its corresponding File Descriptor 2.

nankervisidel1939.blogspot.com

Source: https://www.guru99.com/linux-redirection.html