 Today, we are going to create self-extracting bash scripts. I actually did a tutorial on this years ago, but basically we're going to create a shell script that has basically a compressed. It can be a tar file. We're going to do a tar.gz file today, but any type of compressed file in it, and basically it extracts that. So you only have one file, your script file, but it contains everything, it can contain images, it can contain anything. So let's go ahead and have a look at this. This is how I did it years ago, and this is a great little tutorial here from the Linux Journal, and it goes into detail into actually making one that echoes out stuff, creates temporary directories. Here they're using Ock, we're going to be using seds today, so we'll get everything below this line inside the file, print it out, and then decompress that into that temp directory, and then run a script that's inside that. We are going to simplify all this down to one line of code basically. But I wanted to show you this is a great resource if you want to get more in-depth on it. But let's go ahead and have a look here, and I'm in an empty directory right now, but I do have a directory right here, and my temp folder called stuff. And inside stuff, I have two images, and that's these two images. One is an image of Tux, and the other one is just a little thing that says filmsbychrist.com, which is my website, and you should visit it. So let's go ahead and go back to our shell here, right here. Okay, so I'm going to, again, it's going to be one line of code, but I am going to explain things as we go. So let's go ahead and just create our script. We'll call it selfextracting.sh, okay? We're going to do that. We're going to give it our shebang line. By the way, I'm using Vim as my text editor. You can use whatever text editor you would like. So the first line here is just saying this is a bash script. Now, if I was to echo out dollar sign zero, I've mentioned this in the past, basically that's going to be the first argument, which is going to be our script. So let's go ahead and make our script executable real quick. You only have to do that once on your system. But now if I run that, it's going to echo out the name of the script, which is great because what we want to do is we want to look at the script itself to find a tar file. So let's go ahead and go back into our script. And in here, what we're going to do is we're actually going to use sed. So what we're going to do is we're going to say sed. Oops, sed, if I could type today. Inside single quotations zero comma forward slash the carrot symbol, pound EOF, pound carrot symbol, dollar sign forward slash D. I am looking at my notes, but I'm going to explain all this in a moment. So what is this doing? This is saying, okay, you said look at the current file and look for everything after a line that has, and actually I put a little thing and this should not have the carrot symbol there. The carrot symbol means beginning of line, the dollar sign means end of the line. So it's going to find an exact match of this line and print everything after it. So let's go ahead and create that line. Boom, like that. Now, if I was to run our script again, we should see nothing, right? Cause there was nothing past that line, but I was to go back in there and I was to go, hello. It's going to print that, but then we're also going to get an error because it's trying to run hello as a command. So what we want to do is before we get to that EOF, we want to make sure we exit our script. So now if we run that, basically the script is going to look at itself and display everything after that line. It could be a multitude of things, but it's just going to print hello because that's the only thing after that EOF, which is end of file line. Let's go ahead and go back into VIM. We will delete EOF. And in here, now what we want to do is when we extract, when we display everything after that, it's going to be a tar file because we're going to put that tar file at the end of this file in a moment. But we're basically going to pipe all that into tar, I don't know if the order matters, ZX. And then of course we'll exit after that. Okay, so we have this script. It's going to look at itself, show everything past that point, which will be a tar file, and then it will extract it. So let's go ahead and create the tar file. We're going to say tar, and then we're going to do dash CZVF. And then the name of a file that we're going to create, let's call it data tar.gz. And we're going to put these directories in here. And you can do this different ways. It's going to actually do those full directories and everything within them, as you will see. So now we have our script file and our targz file. Now we want to put our targz file at the end of our file. Now one important thing to note here is that you don't have any empty lines past EOF. See how in Vim I'm hitting down and I can't go down any further. You want to make sure there is nothing after that EOF at this point. So now we're going to cat our data file and we're going to do greater than greater than which means append to the end of our script file. Now, if I was to go into our script file, you will see that everything after that is just the binary file of the targz file. I'm going to quit out of that and that's it. If I run that script now, it's going to extract that tar file which is in. So let's go ahead and remove our data file here. So all we have is our script file which contains the data compressed. Now, if I just run self-extracting.sh and I list out files now, you can see we have a temp directory in here. And if we list out what's inside the temp directory, there's a stuff and then we do that and you can see that it has our files. So that is it. Again, I broke it into two lines but you can actually put this exit up here and make it one line. So it could be a one liner and then basically just have your file after that. It doesn't get much simpler than that. Hopefully I explained it well. Again, I will link to that article on the Linux Journal which again goes into a more detailed example that allows you to create a temp directory that you put everything in and you can have an installer script. So once it extracts everything, you can do an installer but this is a one line version of that. Thank you for watching. I'll also put a link to the notes from this video in the description of the video. I hope that you have a great day. Be sure to visit filmsbychrist.com. That's Chris at the K. Link in the description. Again, have a great day.