 All right more Chad pipes today. So I want to address a user's question that brings out some really important issues when you're first starting to learn to use pipes and Use the Linux or Unix command line Because this is one of the things that definitely confused me when I was a new user and am I confused you actually this particular question brings up a Lot of little problems that it'll get in your way. So it's worth talking about okay So in the last video, which you don't have to have seen but in the last video, right? We had We had a list of all the manuals We wanted D menu to give us a prompt pick one of those and display it in Zathura Just show that P it's show a PDF of it automatically. So someone was inspired by that to do something a little similar So what he's trying to do is he has a folder of videos and he wants to get a D menu menu of that He wants to select one of them and play it in MPV Okay, so it looks like some people have already given him hints as to what the answer is But I want to explain what why what he was doing wasn't working and you know what exactly is going on So I actually have a folder here. And so let's let's actually reproduce what he's doing first off Now I have a folder here. That's actually full of a bunch of videos. So I can do the same thing here So let's say I take all the MP in KV files and I pipe those into D menu So D menu, you know L 30 is gonna give us a list showing 30 of them we can start typing one in and We can select one and it'll print it to standard output. Now at this, you know, sort of in the same way You might know that in PV the the video player that he's trying to pipe them into does actually Accept standard input right so you can take hyphen as standard input and you might think okay So great we have this so I should be able to do something like this I can say MPV and then hyphen and then I should be able to run that so I'm gonna try I'm gonna pick one of those and It's gonna fail to recognize the file format. What exactly is going on here? So it's important to remember what exactly you're piping from place to place Okay, when we do LS and we pipe that into D menu. We are not piping files We are typing we are piping texts. We are just typing the text output of the LS command They happen to be file names, but we're not piping files. We're just Piping the text into D menu and when we select one of those with D menu It is going to be outputting text as well. So if we input if we take that and give it to MPV there are actually a couple problems that we might have here, but One one important one is that MPV is not actually receiving a video. It's receiving text So if you select one of these if you select, you know Comparing Arch Linux Rises if you select that That text it is going to try and read that if as if it is a video file with that text as the content That's the problem. Okay, so people give them some solutions here But I'll show you how to how to I guess come across or overcome this problem There are multiple ways of course One of the ways is actually instead of using pipes if we do something like MPV Well, actually this might show another error that's gonna vex you but Let's do this instead where we have this all of the output this file name that we pick is going to be the Argument of MPV now we can run that we'll actually see we'll get a lot of errors here And why that's a problem is because this is like the second thing that confuses people when you're dealing with files With a bunch of spaces a lot of times on the command line Well, really all the times on the command line they're gonna be treated as different arguments So you want to make sure to put those in quotation marks? Okay But if we run that that should all right, so that works so we can get an output for you know Now what's happening? We're not using pure pipe. So we're not piping something to directly to MPV Instead we are getting text output that we choose and putting that in quotation marks So we don't you know mess up the spaces and then running MPV on that so that would be an appropriate one-liner for that Let's say hypothetically we wanted to use nothing but pipes just for fun Okay, you could do something like this you would have to use x-args and there are different ways of doing this So for example, I could say There are silly ways of doing this too. So let's say x-args cat now if we just run this it's going to take Actually, I might have to Put quotation marks in this as well, but if I run x-args cat and select one of these Yeah, so there would have to be quotation marks here. I don't know if you can actually do something goofy like that That's not gonna work. Is it okay? Whatever Anyway, the way I would do this in the way that I did this in a script recently is actually using x-args But doing making it a little different. Okay, so x-args Allows you now if you just run x-args MPV you might think that you know basically we talked about it in the last video But x-args basically takes standard input and creates a command out of it So in this situation Whatever text would be the output of this x-args basically is gonna put it right besides MPV Okay, so if I run that and Select one it's gonna try and run on that, but we actually have the same Issue that we had a second ago, right because really it's treating this text It's treating all the different words as different files and that isn't working so what you have to do with x-args is you give it the capital I option and This allows you to give it some kind of string The one I always use and a lot of other people use is just open and close brackets Okay, and it says okay whenever x-args sees that open and close brackets. It's gonna actually put the input there Okay, so I can do something like this MPV Quotation marks and then open and close brackets now this Should work. Okay, and it does work, right? So now Keep in mind. What's happening here is or it what's happening in the other Situation where we ran MPV on the whole thing The thing that is getting to MPV is not the file name or the thing that MPV is like actually Processing is not the file name. It is processing the actual file That is if MPV is just run on a file name. That is what it does But when it's receiving stuff on standard input it reads that as if it you are like just directly Streaming a video into that now that's very useful in some circumstances. So for example, you know, you could I mean you could pipe like video output from like a security camera into MPV and it would receive that, right? But you can't just pipe, you know text to it. Okay, that that's the thing to keep away So it's important to keep in mind. There's a difference between Text that that is a file name and the actual content of a file. That's the problem here So I'll show you an implementation actually pretty similar to this last command that I had I guess a sort of real-life Implementation although it is a useless real-life implementation now This is sort of a 30 year old boomer thing, but there was this old show back in the day It was a pretty good show. No, it actually was a terrible show, but I for whatever reason I wanted to download it It's called mystery science theater or 3000 it played at the night it played during the night It was like just guys watching it be guys watching a movie movie and not commenting on it It doesn't matter But I've I was downloading this DVD collection of them that of course has all the different Separate movies separate and because it's so big I'm downloading I'm basically picking which ones to download one by one. Okay, but while it's downloading. I wanted to be able to watch You know watch whichever ones of these are finished So I made a little script basically pretty close to what we just talked about to do exactly this Let me show you what it looks like it is MST so I'm gonna open that up So here's what we do here Now I I'll actually reproduce you can probably actually make out what's gonna happen looks pretty familiar So I'm using du here people always get mad when I use du in videos by the way people always are like Why don't you find why don't use LS or something like that did it find triggers my autism because its syntax is different So I just don't like it and LS has its own problems But anyway, it doesn't matter. This is just the script on my own computer, but anyway in this context so a d uh Dua is doing is so we're going into my incomplete torrents directory specifically The the mystery science theater collection Running that and that outputs all the file names, but it also outputs all the file sizes So I get rid of that with cut um So again, you could do it with find I I'm sick I have to say it because I know people are gonna nag me about it. I don't use fine Just don't I do occasionally but you know, I don't like it So d menu So I'm gonna pipe that into d menu in the way that we expect actually looks like I made it case insensitive So we get three You know 30 of those now notice by default it is printing out all the files and directories but also These uh part files and those are just incomplete files and I don't really want those Since again, you know, I'm downloading these gradually So what I do is I grep out only those maybe I should move my face. I grep out only those That end in avi And not part avi. So if I we look back at these, right? So the ones incomplete have part at the end But if I tell it, oh no grep out the ones with avi and then dollar sign for an end of a line It's only gonna show me Actually, oops. I need to put that before d menu. I don't know what I'm doing I'm sure some of you guys were yelling at your computer screen because I was doing it wrong But uh, oops, so grep out only those complete send them to d menu And um, so now we have only the complete ones and then like I talked about before we can send them to xargs Now, of course, again, we have two options We can say mpv And then put this as an argument to mpv. Okay, so that is one possibility. So we can play this movie That works fine. Okay But at the same time we could also as I do in this script and again, I didn't do it for any particular reason It might actually be better not to use xargs here But that's happened to be how I wrote it when I just sort of you know, mashed it out one day So we can do this select one of these movies and there it is. Okay So that's basically it. So that's a real life implementation But to review what you should take away from the video There's a difference when you're piping magical things to different places Remember what you're piping now in most cases you're piping text and while some programs can read things as standard input Piping to them text is not always what Literal text is not what you want. Sometimes you want file content And even in the example of the last video when I did uh, what was the file I named it? mansplain Even in this situation Zathara is not reading text directly. It's reading actually pdf I mean the pdf format is actually text, but it's formatted in a you know a format that Zathara can read Zathara here does not read a file name. It reads the content of a file Okay, and in the same way if you want to read something on mpv you have to read it has to be file content It can't be text. All right. So anyway, hopefully that clarifies you Clarifies that for you. So remember that and remember also to just Take care to avoid spaced file names or know how to work around them. So see you guys next time