Linux Commands for Beginners 16 - Output Redirection

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] so there's a concept that I should have gone over earlier in the series but there's no better time than now to take care of that and in this video number 16 in the series surprisingly already I'm going to show you how to redirect output and this is going to be the precursor to the next video where I'm going to be talking about different streams so this is an important video it's gonna be pretty easy we'll get more advanced in the next video but I definitely want to make sure that I show you how to redirect output from one command into another command so let's go ahead and dive right in alright so here on my laptop I'm actually using a different distribution I was doing a review of a distro called endeavour OS this is actually a beta but it's something that I was checking out so if you're wondering why the prompt here looks a bit different well now you know so redirecting output so what I'm going to do is actually show you some examples of that I'm going to redirect output from one command into another now an easy example of this I'll use the LS command with no arguments here you know it's just showing me a bunch of folders and of course we do LS dash L but this installation has no personal files so it's just a bunch of folders but that doesn't matter what we want to do is actually redirect output and LS is a Z command it's like the easiest command to do and it's also an easy way for me to show you redirecting output it but in this case what I'm going to do first is redirect the output into a file so I'll clear the screen and then I'm going to do LS dash L and then what I'm going to do is type the greater than symbol and then I'm going to type another space and I'm going to give it a file name so I'll call it file txt because I'm not very creative today so what is this actually going to do well it might be obvious if you look at it did mentioned redirect and the word file so you probably put it together what it's going to do but just in case let's go ahead and see what happens so nothing shows up on the screen it just returned the prompt to the next line but if I do LS again we can see that I have a file that was not here before file dot txt well what's inside that file cat file dot txt so cat I just want to show what's in that file I could I could also do you know less I could do more I'm gonna use cat and press ENTER and basically it's the output of the command as you probably guessed it's not showing the colorized output LS does support colorized output but it doesn't actually show that in the file which is probably to be expected what you can see is interesting is that file dot txt is here it's size is zero it's not actually zero though because if we look at it it actually has a size to it but basically what happened is when the file was first created it didn't have the contents yes it was zero but then this output was then put into the file a little bit of a chicken and egg problem there but you can see what happened when we ran that command we basically used the greater than symbol and we redirected the output of this command into a file and that allowed us to go ahead and you know basically save or capture the results of that command into a file now what happens if I run the command again and again and again and again and again so I ran it a bunch more times here let's go ahead and take a look so the size didn't change still the same size as it was before and if I cap the file the same contents are there so having a single greater than symbol this is a very important concept to know because it's also potentially dangerous because maybe I have an exist existing file and I would like the results of a command to be put at the end of the file but I want to retain everything in there and if you use a single greater than symbol it's going to overwrite that file it blows it away and replaces it with the output of the command so LS l the content of that command is going to be the content of the file or I should say the output of that command is going to be the content of the file and no matter how many times you run it is just going to replace the file but if you do to greater than symbols we can see that it's gonna be different I'm gonna do it a bunch of times here just to kind of over dramatize it but we'll just see what's inside the file quite a bit you see here that you know it basically duplicated that so basically what you could take from that is having two greater than symbols is effectively the same as saying I want to append to the file I don't want to overwrite it I want the output of this command to go to the end of the file and again that's an important distinction because I personally I think at least I think every Linux administrator has done this unfortunately at least once where they overwrite a file they really didn't want to and they blow it away oops darn it I really meant to append it but you know what shame on me I should have used two greater than symbols so I guess what I'm saying is be very careful with this because if you had an important file you could potentially wipe that out and in you know you don't want to do that if it's important or you want to retain that so it's an important distinction we'll get back to the video in just a moment but before we do I just wanted to mention my sponsor Linode Len oat is an awesome provider of cloud Linux servers setting up your Linux cloud servers or Lynne Oates is quick and easy with their intuitive cloud manager interface there are multiple instance types available to make any app or service flexible and scalable if it runs on Linux it runs on Linode use your Linode server to host a website set up a VPN create a next cloud instance host a game server and more you can set up your limits in a data center nearest you with their latest opening in Mumbai in july 2019 if you need assistance 24/7 365 friendly support is available by phone or support ticket visit the URL on the screen right now to get started with 20 dollars in credit you can use towards setting up your very own Linode there are Linux instance types available for as low as $5 a month so let's go ahead and get back to the video now another thing that we can do and I'll use LS as an example because you know it's a it's an easy example I'll just LS again and we have all of the contents here plus the file dot txt that we have already used and now what we could do is take the output of one command and have it serve as the input to another command we can chain commands together so that's what I'm going to give you an example of right now so I'm gonna run LS dash L and that's what the output is going to look like normally but what I'm gonna do is press space you know types of pipe symbol which on the US keyboard is the key above enter or near backspace and that's a pipe symbol so it's just a straight line and then I'm going to pipe that into a different command I'm gonna grep for the word file so when I press ENTER we can see that my LS command only contains one line normally it would contain several you know the difference is this versus just that one line so what grep allows me to do is limit the output to some kind of search criteria I'm searching for anything that includes file so of course that's only going to give me this right here but watch what happens if I do this I'm gonna grab for a capital D and then Oh press Enter I get two lines because both of these lines match that they actually include D oh and documents and downloads those two directories do match so it's going to show that but what I'm doing is I'm taking the output of this command and then I'm chaining it as the input to another command in this case grep and then I'm grepping for do so it allows me to take the output of one command and put it into the other so what I'm going to do right now is give you a more practical example of chaining output or output redirection so suppose you have a log file but you only want to get relevant individual lines and you wanted to omit any duplications so we're going to use the file dot txt file again but we're going to actually do things a little bit differently so just to show you what it includes right now we have this output right here which is again it's just LS a bunch of times so I'm going to do cat file txt and I'm going to redirect it into a command called sort what sort will do is practically alphabetize or sort the individual lines in the file and then I'm going to do space I'm going to redirect again and I'm going to redirect that output to a command called uniq so basically the first command cat file dot txt is going to show me the contents of that file and then it's going to redirect that output into the input of the sort command the sort command is going to sort the lines and then give output and its output is going to be redirected into the unique command so let's go ahead and see what happens what do you think is going to happen so there's actually going to be something unique that's going to happen about this that you might not expect but I'll go ahead and press Enter now we can see something interesting here so we only have each of the lines of the output four folders one time each even though they appeared a bunch of times since I ran the command over and over again it's only there one time but the file dot txt it's actually there quite a few times so why is that so the reason for that is because every time I was running the LS command and I was appending to the file the file dot txt changed its size each time you could see that it actually is a different size each occurrence here so that's why file dot txt is actually not it's actually inconsistent and shows a bunch of times where as the directories are not now to fix that I could simply do this cat file dot txt and then I could redirect it into grep and you don't need a space here but I always put one just looks and then what I'm gonna do here I'm going to use the dash V option the dash V option will do is show me lines that do not include a string because normally we're groping for something we want to show that something but in this case I want to not see something and what I don't want to see is file dot txt so let's just go ahead and see what happens now what we can see here is I see every line but I don't see file dot txt listed because I omitted that by using the dash V option so I'm going to overwrite or actually I'm going to redirect the output into a file and I'm going to create a new file called new file dot txt and if I check out the contents of that file we could see that sure enough we do not actually have the file dot txt listed at all so now what happens if we run the previous command so I'm going to go ahead and bring that up and here it is cat file dot txt but I actually called it a different name didn't I so I'm gonna have to give it that name new file I'm going to do the same thing redirect it into sort and then also unique and we can see that we don't have all those lines of file dot txt listed there because I resaved the file without that and now we see something different we do have total listed twice because that did change over the course of me running the command over and over again but you could basically see how this is useful and the reason why this is useful is if you have a misbehaving process and you're looking at its log file it might show a specific item like hundreds of times but maybe you only just need to see it the one time and that can basically cut the size of the file and then a really easy example of redirect and probably one I should have started with is word count so I could do LS and then grep for actually I need to do LS dash L redirect that to WC we get a number without going too much into the WC command that's a word count but that's not what we're using it for what we actually want to do is - L which is line number how many items do I actually have you my local directory 13 not exactly true because there's going to be other things there that are not files but the point is that you know you can basically use wcl to get a general idea how many items are in a current working directory or any other directory for that matter so I could basically just do LS dash L against the Etsy directory and get a completely different number which could be useful if you want to know a general idea of how many files are in a folder in an example of maybe a mail server that has stuck email you want to know about how many emails are there and we can certainly limit the output to the actual number of items but I'm not going to get into that in this video but I just wanted to show you how to redirect and also how to append output into a file when I went ahead and showed you all of that so I think we should be all set so in the next video we're going to take this concept and we're going to get a little bit more advanced with it so it's not too bad but I think you just need to make sure you understand redirecting output from one command into the input of another command also capturing the output of a command into a file just make sure you understand that right and then when the next video is uploaded you could take it just your understanding you just a little bit further so I'll see you there thank you so much for watching my video I really appreciate it if you want to help me out make sure you check out the description below this video where you'll find links to my latest book mastering a boon to server 2nd edition as well as my patreon page if you like this video be sure to click that like button and share it on Twitter or any other social media network and be sure to subscribe so you'll be the first to see my latest videos as they're uploaded thanks again
Info
Channel: LearnLinuxTV
Views: 12,084
Rating: 4.9629631 out of 5
Keywords: Linux, Tutorial, Learn Linux, cli, command-line, bash, linux commands for beginners, linux tutorial, linux command line, linux for beginners, ubuntu linux, command line, linux (operating system), linux tutorial for beginners, linux, linux terminal, linux command line tutorial, basic linux commands, linux basic commands, linux commands tutorial, linux command line basics, commands in linux, introduction to linux, su, ls, cd, mkdir, mv, cp, chmod, chown, linux commands, redirection in linux
Id: NUjpOLlYv7Q
Channel Id: undefined
Length: 15min 18sec (918 seconds)
Published: Tue Aug 20 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.