Redirection in Linux - Linux Tutorial 8

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to the video tutorial on redirection in Linux most of the commands we have learned so far take an input and give an output the standard input device is the keyboard and the standard output device is the screen Linux is a very flexible operating system and you can change the standard input and output devices let us learn how this redirection works starting with output redirection the more than symbol is used for output or STD out redirection let us try this on terminal using the LS L command we can see the detail information of the files and directories in our home directory now we can redirect the output of the command LS l to a new file listings instead of the screen this is how we would do it on viewing the contents of the file listings we would see that the output of the command Alice al has been stored in this file this is how we can redirect the output of a command it is important to note here that you should use the right file name while three direction if there is an existing file with the same name it will be overwritten so if we redirect the statement to the same file listings they would find that the contents of this file have been overwritten so if you do not want a file to be overwritten but want to add more content to an existing file you should use the more than more than operator this is how we would try it [Music] now if we run the cat command for the same file we would see that it has not been overwritten instead the new content has been added to it moving on to input redirection the less than symbol is used for input or stdin redirection an example of this is the mail program in Linux which 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 redirection operator let us move on to some advanced redirection techniques which make use of file descriptors but before then we must know that in Linux or UNIX everything is a file be it a regular file directories or even devices all of them are files moving on to descriptors every file has an Associated number called file descriptor or 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 so whenever you execute a program for a command at the terminal three files are always open they are the standard input the standard output and the standard error by default error stream is displayed on the screen and error redirection is routing the errors to a file other than the screen these files are always present whenever a program is run as explained before a file descriptor is associated with each of these files so why every direction every direction is one of the very popular features of UNIX or 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 while executing shell scripts you often do not want error messages cluttering up the normal program output the solution is to redirect the error messages to a file let's learn error redirection with some examples the first one is for redirecting an error log to a file here we are executing a program called telnet and running it gives us the following error the file descriptor for the standard error is two so by using two with the more than sign really direct the error output to a file named error file thus the program output is not cluttered with errors and if we want to see the error file we can do it this way let us go through another example which uses the find statement using the find statement we are searching the current directory for a file with name starting with mine on running the command it finds a file with the name my texts but it also shows an error for the directory files which cannot be accessed now using the output redirection we will now redirect the error message to a file named error lock now we can see the error and the file contents let's see a more complex example server administrators frequently list directories and store both error and the standard output into a file which can be processed later they use this command here the more than ampersand writes the output from one file to the input of another file error output is then redirected to standard output which in turn is redirected to the file dir list has both the output is written to file with the ir list let's try it on terminal let us first list the contents of the directories documents and ABC the command says that the directory ABC was not found and it lists the contents of the documents directory now we add the output redirection to a file named dir list but we would see that the error still shows to correct this we now redirect the error output to standard output by using - more than sign ampersand & 1 hence both standard and error output are written to the file dir list this is how we can use output redirection let us now go through the key points of this tutorial each file in Linux has a corresponding file descriptor associated with that the keyboard is the standard input device while your screen is the standard output device the more then is the output redirection operator more than more than appends output to an existing file less than is the input redirection operator more than ampersand redirects output of one file to another you can redirect error using its corresponding file descriptor to thank you for watching I'll see you in the next tutorial
Info
Channel: Guru99
Views: 152,888
Rating: 4.8767505 out of 5
Keywords: file permissions, Ubuntu (operating System), Linux Kernel (Software), Software (album), redirection, linux tutorial, unix, standard, linuxcommandoutputtotext, redirection in linux, redirection in unix, file descriptor, command line tutorial, command line, guru99 linux youtube, guru99 Linux, guru99 linux tutorials, linux tutorial for beginners guru99
Id: Bzd7XfApxLI
Channel Id: undefined
Length: 7min 35sec (455 seconds)
Published: Sun Feb 03 2013
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.