1 00:00:00,120 --> 00:00:01,620 Hello the beautiful people. 2 00:00:01,620 --> 00:00:06,689 And welcome back to the second part of our discussion about making your very own batch script. 3 00:00:06,689 --> 00:00:11,250 And in this video, we're going to make another batch script that's going to allow us to back up all 4 00:00:11,250 --> 00:00:13,410 the folders in our home directory. 5 00:00:13,410 --> 00:00:18,330 And we're also going to show you how you can set up your batch script so they can be run in a more convenient 6 00:00:18,330 --> 00:00:18,870 way. 7 00:00:19,050 --> 00:00:21,090 So let's make another batch script. 8 00:00:21,090 --> 00:00:24,600 We're going to call it up on our desktops. 9 00:00:24,600 --> 00:00:30,600 So we're going to type Nano, then back up a dot H and in there we're going to add as the very first 10 00:00:30,600 --> 00:00:36,420 line, as shebang line and our shebang line tells the shell to not read this code, not read this file 11 00:00:36,420 --> 00:00:40,440 as a text file, but instead to interpret it as a bash script. 12 00:00:40,440 --> 00:00:45,150 So the way we do that is we type a hash, then a bang, so a hash and then an exclamation mark. 13 00:00:45,150 --> 00:00:49,740 And then the path to the interpreter that we want this file to be interpreted with. 14 00:00:49,740 --> 00:00:55,410 And that is going to be our bash shell, which is located at slash bin slash bash. 15 00:00:55,410 --> 00:00:57,480 And we can check that if we're close out of that. 16 00:00:58,470 --> 00:01:04,200 And you go back to our command line by typing which and then bash and we can see slash bin. 17 00:01:04,200 --> 00:01:07,770 Slash bash is where the bash interpreter is located. 18 00:01:07,770 --> 00:01:14,910 So with that, if we open back up our backup script, we can now underneath here start writing commands 19 00:01:14,910 --> 00:01:16,680 that will make up our script. 20 00:01:16,710 --> 00:01:25,950 Now we want this script to to compress and archive all of the folders inside our home directory, or 21 00:01:25,950 --> 00:01:31,080 at least the downloads, documents, desktop pictures and videos folders because they're the best. 22 00:01:31,080 --> 00:01:33,570 So let's just let's see how we might do that. 23 00:01:33,570 --> 00:01:33,770 Okay. 24 00:01:33,810 --> 00:01:37,800 Now, if you remember from what we learnt at the end of the last section to make this happen, we'll 25 00:01:37,800 --> 00:01:41,700 need to use the tar command and we need to give it a few options. 26 00:01:41,700 --> 00:01:47,550 So for example, we'll give it the C option to create a new archive, the the V option to make it speak 27 00:01:47,550 --> 00:01:54,690 to us and the F option to tell it that the next argument is going to be our is going to be our archive 28 00:01:54,690 --> 00:01:58,590 file and we're going to call it back up to GZ. 29 00:02:00,030 --> 00:02:05,460 But to make it compressed using GZIP, we actually need to give it the Z option as well. 30 00:02:05,460 --> 00:02:10,560 So we're going to compress it through GZIP into a file called backup to GZ. 31 00:02:10,650 --> 00:02:14,280 But now we need to tell it what we want to actually store. 32 00:02:14,280 --> 00:02:15,880 And this is what we're going to tell it. 33 00:02:15,900 --> 00:02:19,350 We're going to say, Hey, I want you to put in there. 34 00:02:19,350 --> 00:02:22,680 We want we want to put in there everything in our home directory. 35 00:02:22,680 --> 00:02:29,730 We're going to put in our documents folder, our downloads folder, our desktop folder, our pictures 36 00:02:29,730 --> 00:02:32,730 folder, and our videos folder. 37 00:02:33,150 --> 00:02:38,010 So what we're saying here is, hey, use the time command, compress it, be verbose, tell us what's 38 00:02:38,010 --> 00:02:44,940 going on, zip it through GZIP and make the file called backup to GZ and inside there. 39 00:02:45,120 --> 00:02:50,610 Put the documents, downloads, desktop pictures and videos folders from our home directory using a 40 00:02:50,610 --> 00:02:53,620 bit of brace expansion to save us some typing. 41 00:02:54,030 --> 00:02:55,310 So that's it. 42 00:02:55,710 --> 00:03:01,770 Now when we run our batch script, if we do batch backup DSH, you'll see that it's given us some output 43 00:03:01,770 --> 00:03:06,420 and now we've got a backup to GZ file on our desktop. 44 00:03:06,420 --> 00:03:07,320 So isn't that very cool. 45 00:03:07,320 --> 00:03:07,530 Right. 46 00:03:07,530 --> 00:03:14,070 And you can see here all the files that have been added into the, into the, into the, into the backup 47 00:03:14,070 --> 00:03:14,540 folder. 48 00:03:14,550 --> 00:03:21,900 Now in there we gave it the V option here for verbose and that's why we got to see all that output on 49 00:03:21,900 --> 00:03:22,260 the screen. 50 00:03:22,260 --> 00:03:23,310 Maybe we don't want that. 51 00:03:23,310 --> 00:03:32,640 So if we just get rid of the V and let's say we remove backup dot GZ and now we run it again, you can 52 00:03:32,640 --> 00:03:37,980 see that now we don't really have as much output from it, but we've still got the backup file. 53 00:03:37,980 --> 00:03:43,470 So you can see here one benefit of writing these things in scripts, you can save and edit the behavior 54 00:03:43,470 --> 00:03:48,240 of those scripts over time as your needs evolve, and they're always going to be there for you to run. 55 00:03:48,240 --> 00:03:53,100 For example, we might not like the fact that this message comes out on standard error, so we can just 56 00:03:53,100 --> 00:04:02,970 go back into our backup DSH and redirect this this command standard error to a place called slash dev 57 00:04:02,970 --> 00:04:08,430 slash null, which basically gets rid of it so that we are we've redirected standard error to slash 58 00:04:08,430 --> 00:04:13,590 dev slash null, which is a place on the file system called the Bitbucket that basically just deletes 59 00:04:13,590 --> 00:04:14,730 whatever is sent to it. 60 00:04:14,730 --> 00:04:19,860 And now if we try to run bash backup dsh, cli the screen, we run it. 61 00:04:19,860 --> 00:04:26,550 Now we know that we now get no output, but if I delete this backup archive and run it, you can see 62 00:04:26,550 --> 00:04:27,540 that the backup is still created. 63 00:04:27,540 --> 00:04:28,650 But now we don't get any errors. 64 00:04:28,650 --> 00:04:33,990 So you can see that you can customize these these scripts as time goes on. 65 00:04:33,990 --> 00:04:38,490 So what I want to draw our attention to a bit right now is how we can make running the bash scripts 66 00:04:38,490 --> 00:04:40,590 a bit more efficient and user friendly. 67 00:04:40,590 --> 00:04:47,460 So at the minute, what we're doing is we are on our desktop where the bash script is located and we're 68 00:04:47,460 --> 00:04:51,540 typing the Bash Command and then giving it the path to the bash script, which at the minute, because 69 00:04:51,540 --> 00:04:53,810 we're already on the desktop is just backup DSH. 70 00:04:53,820 --> 00:05:00,600 When I run that, we get the the required output, but that's kind of a bit of a hassle because if we 71 00:05:00,600 --> 00:05:05,280 weren't on the desktop then we would have to give it a different path and we have to run the bash command. 72 00:05:05,280 --> 00:05:10,050 There's a, there's a better way to do it in just one step and this is how you set that up. 73 00:05:10,050 --> 00:05:14,250 So what you do is you go back to your home directory and you can see in here that we've just got our 74 00:05:14,250 --> 00:05:15,330 normal folders. 75 00:05:15,380 --> 00:05:22,260 What you do is you make a directory called bin, all in lowercase bin, and you can see that that's 76 00:05:22,260 --> 00:05:22,860 been created. 77 00:05:22,860 --> 00:05:27,390 And you what you do is you move all of your shell scripts into that folder. 78 00:05:27,390 --> 00:05:31,500 Let's move from our desktop, the, the backup DSH file. 79 00:05:31,500 --> 00:05:35,490 So backup DSH, we're going to move it into our bin folder. 80 00:05:35,760 --> 00:05:41,310 So we're moving from our desktop, the backup folder file into our bin folder. 81 00:05:41,310 --> 00:05:41,470 Okay. 82 00:05:41,490 --> 00:05:43,200 So you see it's been moved from there. 83 00:05:43,200 --> 00:05:47,340 And when we now go into our bin, we see that backup is in there. 84 00:05:47,340 --> 00:05:52,650 And actually I'm just going to rename it from backup DSH into just backup. 85 00:05:52,680 --> 00:05:53,100 Okay. 86 00:05:53,130 --> 00:05:57,720 So now we've just got the same file called backup and if I run it, it'll still work. 87 00:05:57,720 --> 00:05:58,920 So there's nothing wrong. 88 00:05:58,950 --> 00:06:01,980 It's I've just renamed it a little bit now. 89 00:06:01,980 --> 00:06:06,900 I need to make this so that it can be run from the command line. 90 00:06:07,140 --> 00:06:09,610 So what we're going to do, there's two ways to do this. 91 00:06:09,630 --> 00:06:16,650 First of all, we can open up in our file browser, we can go into bin and then we can right click click 92 00:06:16,650 --> 00:06:23,700 properties, go on permissions and then click allowing executing file as program. 93 00:06:23,700 --> 00:06:24,220 Okay. 94 00:06:24,270 --> 00:06:28,530 That's one way to do it, but I'm not going to do it like that for now because I want you to learn the 95 00:06:28,530 --> 00:06:29,490 terminal way. 96 00:06:29,490 --> 00:06:32,340 So what you do is you type the C mod command. 97 00:06:32,340 --> 00:06:35,670 Now the C mod command is stands for change mode. 98 00:06:35,670 --> 00:06:40,080 And what it does is it allows you to change the permissions that a certain file has. 99 00:06:40,080 --> 00:06:44,790 And then as a second argument, you type plus X, which means give it executable permission. 100 00:06:44,790 --> 00:06:48,270 So add executable permissions and then you tell it the file. 101 00:06:48,270 --> 00:06:50,130 So just back up when I run that. 102 00:06:50,130 --> 00:06:52,920 So what we've done is we've added executable permissions to the backup. 103 00:06:52,920 --> 00:06:56,070 Now when I look at it with LS, you can see that it's gone green. 104 00:06:56,340 --> 00:06:58,860 Okay, so that means that it can be run as. 105 00:06:59,120 --> 00:06:59,990 A script. 106 00:07:00,140 --> 00:07:04,240 Now, the cool thing about this is, is if we close out, actually, what I'm going to do is as well 107 00:07:04,250 --> 00:07:05,990 just to make it a bit clearer. 108 00:07:06,740 --> 00:07:11,060 If I go back in to back up, what we're going to do is I'm going to save make sure that the backup is 109 00:07:11,060 --> 00:07:13,550 always saved on the desktop. 110 00:07:13,760 --> 00:07:16,190 That's just going to make it a bit clearer to demonstrate what's happening. 111 00:07:16,310 --> 00:07:19,220 So what we're going to do is I'm going to close the terminal. 112 00:07:19,280 --> 00:07:20,930 I'm going to delete our backup here. 113 00:07:21,890 --> 00:07:29,810 And now I notice I'm in our home directory, but if I type back up, we can see here that we're getting 114 00:07:29,810 --> 00:07:33,530 an error saying that the backup program is not currently installed. 115 00:07:33,560 --> 00:07:37,700 Now, if you remember what I told you a long time ago, right at the start of this course, when the 116 00:07:37,700 --> 00:07:42,410 shell says it can't find something, that means it's not on the shell's search path. 117 00:07:42,410 --> 00:07:47,930 So we need to edit the shell search path to include our recently created bin folder. 118 00:07:47,960 --> 00:07:51,140 Now, the way that we do this is we're in our home directory. 119 00:07:52,220 --> 00:07:59,300 If we go ahead and use the nano command, I want you to edit the dot bash RC file. 120 00:07:59,300 --> 00:08:05,390 So the dot bash RC no spaces, all lowercase will open up this very important file. 121 00:08:05,420 --> 00:08:10,850 Now, if we scroll all the way down to the bottom and add a line down there, this is what I want you 122 00:08:10,850 --> 00:08:11,440 to write, okay? 123 00:08:11,480 --> 00:08:15,590 I want you to write path in uppercase, then an equals sign. 124 00:08:15,920 --> 00:08:18,200 Then some double quotes are two double quotes. 125 00:08:18,200 --> 00:08:28,550 And inside those double quotes, dollar sign path a colon, then dollar sign home in uppercase slash 126 00:08:28,550 --> 00:08:29,180 bin. 127 00:08:29,930 --> 00:08:31,390 So let me explain this a little bit. 128 00:08:31,400 --> 00:08:34,730 So what we're saying here is a variable called path. 129 00:08:34,730 --> 00:08:38,659 We're going to have we're going to set our path equal to what the path currently is. 130 00:08:38,840 --> 00:08:42,530 Then a colon, then our home folder slash bin. 131 00:08:42,530 --> 00:08:46,070 So the bin folder in our home folder is going to be added to the path. 132 00:08:46,070 --> 00:08:48,290 And when I close that, we right to the bash. 133 00:08:48,290 --> 00:08:55,010 RC close it and if we close our terminal and open another one, what that has done, if we echo the 134 00:08:55,010 --> 00:09:03,450 path, what that has done is it has added slashes, home slashes, the add slash bin to our path. 135 00:09:03,470 --> 00:09:10,160 Now that means it'll be able to search for our our backup script in the path just like it would any 136 00:09:10,160 --> 00:09:10,690 other command. 137 00:09:10,700 --> 00:09:17,840 So now when I run back up, you can see that our script has just been run and it has now created the 138 00:09:17,840 --> 00:09:18,200 backup. 139 00:09:18,200 --> 00:09:25,970 So if I, if I now go into our bin folder and I create another file called Hello for example, and in 140 00:09:25,970 --> 00:09:33,620 there I tell it that it's a batch script with the shebang and I say Echo hello world and save it. 141 00:09:33,710 --> 00:09:36,230 And then I give the file executable permission. 142 00:09:36,230 --> 00:09:38,240 So C-H mod plus x. 143 00:09:38,240 --> 00:09:39,050 Hello. 144 00:09:39,500 --> 00:09:41,540 See that it's gone green now. 145 00:09:41,540 --> 00:09:43,580 I can be anywhere in my file system. 146 00:09:43,580 --> 00:09:47,000 I could be in my documents folder but if I run. 147 00:09:47,000 --> 00:09:52,550 Hello, you can see that hello world now works and I can pipe these to wherever I want to redirect them. 148 00:09:52,550 --> 00:09:57,170 So if I redirects it to a file called Hello to my desktop, there we are. 149 00:09:57,170 --> 00:10:04,610 We've now got a file called TXT on the desktop and we're now basically creating our own commands. 150 00:10:04,610 --> 00:10:05,860 So isn't that awesome, right? 151 00:10:05,870 --> 00:10:11,660 Like we've got our own our own commands and anything that we put in this bin folder, if we give it 152 00:10:11,660 --> 00:10:15,410 executable permissions, we can run it just like any other command. 153 00:10:15,410 --> 00:10:17,000 So that is incredible. 154 00:10:17,000 --> 00:10:22,250 But now you might be thinking, okay, Ziad, this is cool and everything, but what's really the difference 155 00:10:22,250 --> 00:10:24,080 between an alias and a script? 156 00:10:24,080 --> 00:10:29,450 So remember, aliases are things that you put in your in your in your bash. 157 00:10:29,450 --> 00:10:32,810 Aliases file this one this dot bash aliases file. 158 00:10:32,810 --> 00:10:39,470 And if we if we take a look in there, what we're doing is we are setting command names and we're giving 159 00:10:39,470 --> 00:10:41,090 them what those commands mean. 160 00:10:41,090 --> 00:10:52,100 Why didn't we just make a make an alias and we say backup equals task dash CV, Z, F of to our to our 161 00:10:52,100 --> 00:11:04,100 desktop and then put back up to GZ and say we want in there, we want our desktop and our documents 162 00:11:04,460 --> 00:11:12,500 and our and our downloads and our pictures and our videos and then close it. 163 00:11:12,500 --> 00:11:13,750 Why don't we just do that as hard? 164 00:11:13,760 --> 00:11:15,980 Like we, you know, we know how to make aliases. 165 00:11:15,980 --> 00:11:17,420 Wouldn't that work the same? 166 00:11:17,420 --> 00:11:22,160 Well, yes, it would work the same in this instance, because we have a one line script. 167 00:11:22,310 --> 00:11:22,910 I'm going to close that. 168 00:11:22,910 --> 00:11:23,750 We're not going to do that. 169 00:11:24,050 --> 00:11:25,790 That's because we have a one line script. 170 00:11:25,790 --> 00:11:29,720 So aliases can become the same as one line script, really. 171 00:11:30,110 --> 00:11:34,910 But the good thing about script is that scripts are designed to contain more than one command line, 172 00:11:34,910 --> 00:11:39,290 whereas aliases are usually just to give more convenient names to just one command line. 173 00:11:39,320 --> 00:11:45,170 Now, secondly, scripts can have more complicated programming constructs such as loops if statements 174 00:11:45,170 --> 00:11:46,010 and functions. 175 00:11:46,010 --> 00:11:50,540 Now, if you've never done any programming before, suffice it to say that scripts are more heavy duty 176 00:11:50,540 --> 00:11:52,070 than aliases can be. 177 00:11:52,490 --> 00:11:58,760 And thirdly, scripts can be scheduled to run at any time using Cron and. 178 00:11:58,960 --> 00:12:02,800 Scheduling part is what we're going to focus on in the next video. 179 00:12:03,770 --> 00:12:07,460 So over the last couple of videos, you've seen how to create your very own scripts. 180 00:12:07,460 --> 00:12:12,590 And the key about batch scripts is that they allow you to store useful command sequences in files that 181 00:12:12,590 --> 00:12:13,910 you can use later on. 182 00:12:14,000 --> 00:12:18,080 Now, because anything on your computer is changeable and editable through the command line, and because 183 00:12:18,080 --> 00:12:23,540 you can save these series of commands that can do specific functions, you can create incredibly powerful 184 00:12:23,540 --> 00:12:28,970 batch scripts that can do whatever you want and save those batch scripts for later by using the fundamental 185 00:12:28,970 --> 00:12:31,130 skills that you've learned in the last two videos. 186 00:12:31,190 --> 00:12:36,110 Now, a key piece of information is that bash scripts must have the shebang line, telling them that 187 00:12:36,110 --> 00:12:41,630 the file needs to be interpreted as bash commands, using the bash shell interpreter, which is located 188 00:12:41,630 --> 00:12:43,580 at slash bin slash bash. 189 00:12:43,610 --> 00:12:48,800 Now this shebang line is the first line of every script, and there mustn't be anything above it, and 190 00:12:48,800 --> 00:12:51,350 there must not be any spaces in the shebang line. 191 00:12:51,680 --> 00:12:53,570 The text that's highlighted here in Orange. 192 00:12:53,570 --> 00:12:55,730 So it must come just as you see it there. 193 00:12:55,730 --> 00:13:00,410 And it's the shebang line that turns your regular text file into a bash script and tells the shell to 194 00:13:00,410 --> 00:13:03,230 interpret everything in that file as bash commands. 195 00:13:03,470 --> 00:13:07,910 Now, if you wanted to make a Python script, you could put as the shebang line something like hash 196 00:13:07,910 --> 00:13:13,550 bang, slash slash user slash bin slash Python three, and that would allow the script to be interpreted 197 00:13:13,700 --> 00:13:14,870 as a Python program. 198 00:13:14,870 --> 00:13:17,810 But this is how you do it in orange here for bash scripts. 199 00:13:17,960 --> 00:13:22,250 Now, you've also seen how you would make scripts rumble just like regular commands, and you saw how 200 00:13:22,250 --> 00:13:27,080 you would make a bin folder in your home directory and add each script to that bin folder. 201 00:13:27,080 --> 00:13:31,910 You would then give each of the scripts executable permissions using the C mod command and add that 202 00:13:31,910 --> 00:13:35,480 bin folder to your shell's path by editing your bash RC file. 203 00:13:35,510 --> 00:13:38,120 Now this allows you to run scripts just like regular commands. 204 00:13:38,120 --> 00:13:40,100 So we type backup and our backup script ran. 205 00:13:40,100 --> 00:13:41,870 We typed hello in our Hello script run. 206 00:13:41,870 --> 00:13:45,080 And this will also work for Python programs as well. 207 00:13:45,110 --> 00:13:49,280 Now as an aside, if you're interested in learning more about the Python programming language, then 208 00:13:49,280 --> 00:13:53,510 go to the last video of this course where you have some discount coupons for all my courses, including 209 00:13:53,510 --> 00:13:58,820 my best selling Python course, the Python Bible, which will take you in a project based way from absolutely 210 00:13:58,820 --> 00:14:02,870 no previous Python experience all the way to writing 11 Python programs. 211 00:14:02,870 --> 00:14:04,660 So if you're interested, go ahead and check that out. 212 00:14:04,670 --> 00:14:10,760 But the bottom line is that scripts are more powerful than aliases, and scripts can be run at any date 213 00:14:10,760 --> 00:14:14,600 or time that you like, which allows you to automate your workflows. 214 00:14:14,600 --> 00:14:18,410 And it's precisely that scheduling that will be coming onto in the next video. 215 00:14:18,410 --> 00:14:23,210 So in the next video, you're going to learn the true power of shell scripts by learning how to automate 216 00:14:23,210 --> 00:14:26,570 them using an incredibly useful utility known as Cron. 217 00:14:26,660 --> 00:14:31,580 So to learn how to schedule automated tasks, I'll see you in the next video.