 This is Arthur. This is Arthur. He does YouTube videos. Pretty good ones too. I enjoy them. Recently he did one on extracting zip files, or at least extracting single files from zip files. The way he did it was he was basically cutting out text files from within a zip file. So let's go ahead and look to see if we can improve on some of his techniques. Now, I'm sure that there are already shell applications, just as there's GUI applications where you can go into a zip file and go through all the files and pull out the ones you want. But today we're just going to write a simple script. Literally could be one line, but we're going to do three whole lines of code. And we're going to use one of my favorite programs, FZF. So you can search through that zip file, select multiple files, and extract just the files you want, whether they're text or binary. Okay, let's look at this. We have a zip file here. And of course, we can open it up with it with a GUI application like xarchiver, like so. And of course, we can go through the directories here. But let's say you don't want to use a GUI interface, or you want to do this on a server through SSH, and you don't have a GUI interface running. Let's talk about this, and let's see what we can do. Let's talk about what Arthur did. One of the things he showed us was with the unzip command, you can list out what files are inside a zip file by using the dash L option. And that will list all the files. And you can see down here, there are 598, so almost 600 files in this directory, or in this zip file, I should say. So let's go ahead and see what he did. So again, let's list those out. Let's choose a file. I'll choose this, log this power script shell script power, power shell script. There we go. That's I wrote. So let's go ahead. And what he did was he used the dash p command, I believe. So unzip, and the name of our file, but before that, we're going to give it the dash p option, which will print a file, and we can give it a file name, and it prints it to the screen, just like you catted it out, which is great for text files. But we want to extract the files, not just cap them out to the screen, and we also might have some binary files. So what can we do with that? Instead of the p option, we can give it the x option, which now extracts it, and it creates all the directories for you. So now you can see we have a folder, and I can cat out. I can just hit tab and go through all that and cat out that power shell script. OK, let's go ahead and let's say you want to extract multiple files. You don't want to extract them all, but just select files. Let's go ahead and list them out. And what we can do is same as before, we can use unzip dash x, the name of our zip file, and then we can give it each file we want individually here. So there I gave it three different files. One of them already exists, so it will give us a warning saying, do you want to override this? And we will say yes. And there we go. We've extracted all three of those files. That could be quite a lot to do, especially when you have almost 600 files inside the zip file. So again, we're going to use FZF, which is a program I've talked about a lot in the past. It is a great single binary cross platform, very small, very useful application that I use in almost all my scripts these days. So let's go ahead and start off by using the unzip dash L command, the name of our zip file, and just pipe it into FZF that will list all our the output of that. And now I can type in something like Arduino and it's a fuzzy finder. So it signs everything with Arduino or anything that's kind of similar to that. And now I can choose one of those files and it gives us that file name. But of course, we don't want all that information because we have the file size here, we have the date here and the time here. And I'm sure there's better ways to do this, but we're just going to actually, before we go into FZF, we're going to use AUK in this case to print out in this case, the fourth column because we have one column, two columns, three columns, and our file name is the fourth column. So we're just going to say print dollar sign for. And before we put that into FZF, this is what does it lists out the files without all that information. Unzip might have an option to do that. I didn't look into it. But let's fight that into FZF. We can do that. Now we type in again, something like Arduino and I can choose one and it gives us that file name. If I want to select more than one file, I can give it the dash M option. And now I can, again, search so I can do something like games. And then I can come through here and I can use the tab key to select different files and folders, hit Enter, and it lists those out. Now we're just listing the files right now. So what we need to do is now past all that information we just grabbed into our unzip command. So what we're going to do is we are going to put this in dollar sign and parentheses. We do not want quotations at this point because it will think all those file names are one file name. And that's not what we want. But now we can use our unzip command and we can do dash X, the name of our file, so dash X for extract. And now when we hit Enter, it's going to run these commands first and then take that output and put it right here as if you typed it. So I can hit Enter. Again, I can type in something like bash and I can now choose multiple files and when I hit Enter, it extracts them. Perfect. OK, so we've extracted those things. Let's go ahead and put this into a shell script. That's the only command we need, but we're going to add two other lines to make it one to make it a little more useful and one to avoid, you know, error type stuff. So I'm going to call this unzip files. I could give dot it SH because it's going to be a shell script. The extension does not matter, but the shebang line does. This line tells our operating system that this is a bash script and should use the bash interpreter. Now we can take our command from before. I will just copy and paste this for now to save us a little bit of time. We're going to make some changes. There we go. OK, we're not going to give it the name because we want to pass it. Right now, I'm just going to use a variable called zip, which we'll create in a moment. So dollar sign, zip, dollar sign, zip. And now we can also say that zip equals. Dollar sign one dollar sign one is the first argument you give the script. So now we can take this, we can make it execute all change mod plus X the name of our script. And now I can dot slash that file. I can pass it our zip file and it should give us our output here. You know, this could be cleaned up a little bit. We have things down here that aren't files such as name dash dash dash. That's just unzipped output. But I'm not going to worry about that in this tutorial, but I'll do something like doughnut and I will choose those two, that directory in the file and we'll do that and it will create and extract those files. Great. But we have to pass it a file, right? If we don't give it anything, it's going to get confused. It's like you didn't give me any information. I can type something that's going to try to run unzip. Let's go ahead and fix that. We'll go back into our script here. And what we're going to do here is I'm going to say inside brackets, dollar sign, the pound symbol, or the number symbol, the hashtag, whatever you want to call it, dash LT1. Let's not forget our spaces there and end. So what we're doing here is we're saying, look at the number of arguments. If it's less than one, do something. If it's greater than one, do something else. We're going to do pipe pipe here and we're going to say zip. Sorry, we want pipe pipe here, pipe pipe there. And we're going to use list and FZF. So let me take this out and then I'll explain it for you. We're going to say LZ zip, put that into FZF. So what is this going to do here? We are saying, and again, this entire script will be in the link in the description of this video, so you can get it there. We're saying, OK, this is a bash script. Count the number of arguments. Is it less than one? OK, if it's less than one, and that means that's true, the statement is true, so if it's less than one, we are going to list out all the zip files and put it into FZF. And then whatever file we select, we're going to put in the variable zip. Now, if it's greater than one, it's going to take the first one of those arguments and make it our zip file. So let's go ahead and see how that works. So now I can say dot slash our script and I can pass it our zip file name. And hit Enter. And now I can do something like choose these files here and extract them. But if I didn't give it a file name, it will list out all the zip files in the current directory, which in this case, there's only one. And I can choose it, you know, boom, boom. Now, let's go ahead and just copy our zip file a couple of times. We'll copy it to one dot zip. We'll copy it to test dot zip, file dot zip. And my dot zip, they're all the same file, but they're now in multiple locations. So now, again, I can run my script and I can give it something like my dot zip and it will automatically choose that as an option. And I come up here and I can choose a couple of files and extract them. But if I don't give it a file, it's now going to list them all and I can choose any one of these. So file and I can, you know, come up here and I'll choose two files and extract them. Let's add one more line to our script. So again, we only need the one line. We added another line to make it a little more user friendly. But now let's give it a little bit of error check. So real quick, let me show you what would happen if we were to run our script, not give it a file name, type something that does not exist and then hit enter. Well, now it's trying to list out files from a file that doesn't exist. And when I hit enter, it's going to try to run unzip against nothing and you get all this output. We're going to get rid of that just by saying, and I'm kind of combining different techniques here, but double brackets in this case, dollar sign, zip equals nothing. And that is definitely not the best way to do that, but it works best way. That's that's argumentative. And then we'll exit. So we're saying here is, OK, look at the number of inputs, arguments given by the user. If it's less than one, search through all the zip files. If not, if you have an argument, use that first argument as our zip file. Now, if we were to search through them and choose one that does not exist, you know, we choose something that's empty, then we're just going to exit. We're not going to continue with the script. And that's it. We have our three lines of code. I can now choose something. And if I was to choose a file that does not exist, it just exits. Doesn't give us an error. It doesn't try to run unzip, but I can choose one of these files and I can choose something like windows. Yeah, we'll choose those. There we go. I just extracted three files. That's it. Thank you again to Arthur Reader for his videos. Check out the links to his channel in the description of this video. Check out the link in the description of this video to get this little script and you can throw it on your system if you want. I thank you for watching. And as always, I hope that you have a great day.