Linux Terminal Commands: Pipes and Redirection

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello there my name's Gary Sims and this is Gary explains now as I follow up to my two previous videos on the Linux command line today I would like to look at pipes and redirection so if you'd like to know more about pipes and redirection on the Linux or UNIX command line please let me explain okay so the basic idea on Linux and on UNIX is that the output of one program can become the input to another program so here I've gone to user slash bin I've typed in LS minus 1 to give me a one-column command of all the files and there are lots of them in here and there are too many of I'm here at the end towards the out for s tuv but I don't know what was at the beginning so what you can do with pipes is you can say use a pipe and I'm using that vertical bar character which is often found by the Enter key over to the right hand side of your keyboard type that into the less program and so the output of LS becomes the input to less which means now I can see this file 1 or this output one screen at a time just by pressing the spacebar now I just like to apologize if you think my speech a little funny I'm in the middle of some dental work hopefully within a week it should all be sorted so let's carry on we can also now use a program called strings which actually pulls out all the strings from a file and if I now do strings Python 2.7 we can see here all the different strings that are inside of that file but again they all whiz past way too quickly so I can take the output of strings and make it the input of less and now I can see these strings one screen at a time by just hitting the space bar but of course some of these things here are a bit funny you know cocoa in it dot X dot fini so what you can actually do is you can use another command called grep now grep will search through the input stream and find strings that match the string that you give it so if I say grip though it will actually only find the strings that have got that in it and now we can see that we're seeing a whole bunch of short sentences with the word dirt in it so return a copy of the hash object that one here okay so that's now a nice sentence because it had the word dirt in it but of course it all went past again way too quickly so you can actually combine pipes so we've had one pipe now let's use a second pipe and a third command and we can do less so strings Python 2.7 will find all the strings inside of the Python program it will then pipe that into grep which will then search for all the strings that have the word vert in it it will then send that into less and so now we can see all of those strings one page at a time by using the space bar so there we go we use two pipes three programs and two pipes and we chained them all together to do one thing now we can also go back now to our home directory and we can do a similar thing called redirection now there's a little command called echo which will basically print out something on the screen so echo hello and there we have hello what I can do is I can say echo hello and now I use the greater than sign saying redirect this to hello dot text so nothing now comes out on the screen and what we'll actually see if we use cat which means concatenate concatenate the contents of this file to the standard output concatenate hello dot txt and what we find is it has the word hello in it so by using echo hello and then redirection we've actually managed to put something into a text file and if I now do echo howdy into hello dot txt what we actually find if we now concatenate hello dot txt is that it has been replaced with the word howdy so when you're using redirection if the file already exists it will be over written now another thing we can do with redirections we can copy files I can say now cat hello dot txt and I can redirect that to howdy dot txt we don't get any output but as you can see now there is actually a file called howdy so we can actually concatenate that now I look it's got the word how didn't course was going to at exodus as well concatenate is just a quick way of showing what they're howdy so what we've actually done is I've managed to copy the content of hello text into howdy dot text by using concatenation and redirection now if I was now to say echo welcome and I use two greater than signs that means don't overwrite the file append to the file so I can now say welcome and add that to hello dot text again we don't get any output but when I now look at hello dot text I've actually got the word howdy and then the word welcome because one was concatenated appended to the end of the other one and I can see do the same thing with files I can actually say concatenate now hello dot txt and add it to the end of howdy dot txt so by doing that I'm actually copying the contents of one file and adding it to the end of the other file and now if we concatenate howdy dot txt we can see it's got howdy because that was the original texels in there and then howdy and welcome which came across from the file hello dot txt now we can actually use this for other commands for example we could do strings again on user bin Python 2.7 okay and that just gives us this big long list but now in fact we could redirect that to temp text so we don't get any output but the output is now stored in this file temp dot txt and of course we could look at that by using less temp text which will give it to us one page at a time or can catenate temp dot text and pipe it into less achieves the same achieves the same thing now of course we can also now use our grep command so if we go back and find our redirection here we can now take the output of the strings come our we can send it through a pipe to grep for the word there and then we can redirect the output to tempt dr. text so again we're not gonna get anything on the screen but if we now look at Tim text we can see it's just got all those files strings with the word in it and of course a book I could now actually read it line two less and see one page at a time and in vertical so this is but I don't have to run this command every time now I've got the answer in temp dot text imagine if you were searching a database or you were extracting some data from somewhere you can redirect it into a file which covers your results file which you get an email to somebody you could store for later whatever you've now got the results permanently stored in that file now one more quick thing to show you on Linux if you want to see the list of current running processes you can do PS aux and there you get this big long list of all the processes that are running but they're getting always passed way too quickly so we can use a pipe to pipe that into less and now we can see all those processes that are running one at a time of course we could also use grep so if I wanted to see which ones have been started by the user PI because I'm on a Raspberry Pi here sorry grep PI then now it only shows me you can see here over on the line and I had PI the process that we've started by PI doesn't show me the system processes and again of course I could search for you know I could do that and pipe back into less so I can see that I paid the time and if I was looking for a particular program like oh I wonder what the secure shell process is doing but I can do that and here we can see there is the secure shell running their process ID four seven five started by root so of course there's a whole bunch of system management stuff I could do now around this process and I found it using PS aux and then using grep through a pipe to look for that and of course you can of course redirect these now to a file so I can redirect that now to temp dot text and so on and of course now temp dot Texas has got that information it so you can use pipes and redirections all together all the time but my name is Gary Simms this is Gary explains I really hope you enjoyed this video this video was about pipes and redirection please give it a thumbs up please subscribe and please share this video on okay that's it I'll see you in the next one
Info
Channel: Gary Explains
Views: 26,886
Rating: 4.9697542 out of 5
Keywords: Gary Explains, Tech, Explanation, Tutorial, Linux, Unix, macOS, Pipes, Redirection, Stdout, Stdin, Standard out, Standard in, grep, less, more, command line, terminal
Id: oyc_6UfoW3w
Channel Id: undefined
Length: 8min 56sec (536 seconds)
Published: Fri Jul 06 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.