1 00:00:00,000 --> 00:00:04,200 [Intro Music] 2 00:00:04,200 --> 00:00:06,300 I have just demonstrated to you how 3 00:00:06,300 --> 00:00:08,189 you could change your permissions 4 00:00:08,189 --> 00:00:10,710 for a specific file. You are able 5 00:00:10,710 --> 00:00:12,870 to assign specific permissions, to 6 00:00:12,870 --> 00:00:14,430 add permissions, or to remove 7 00:00:14,430 --> 00:00:16,590 permissions. And for that, you need 8 00:00:16,590 --> 00:00:19,230 to use command 'chmod'. Please 9 00:00:19,230 --> 00:00:20,700 know that if you want to modify 10 00:00:20,700 --> 00:00:22,770 permissions for entire directory, 11 00:00:22,920 --> 00:00:24,660 you need to use additional option 12 00:00:24,690 --> 00:00:28,050 '-R'. Great. Now, let's 13 00:00:28,110 --> 00:00:30,510 create a sample script, and make 14 00:00:30,540 --> 00:00:32,759 the script executable script. And 15 00:00:32,759 --> 00:00:34,350 actually, we will create simply 16 00:00:34,350 --> 00:00:36,210 file, and you are not able to 17 00:00:36,210 --> 00:00:38,190 execute any file in the new system. 18 00:00:38,370 --> 00:00:39,780 You need to make this file 19 00:00:39,810 --> 00:00:41,940 executable first. And let's try to 20 00:00:41,940 --> 00:00:43,830 do this here in this lecture. Let's 21 00:00:43,830 --> 00:00:45,780 clear terminal, and here let me type 22 00:00:45,780 --> 00:00:47,400 the following command, 'touch 23 00:00:47,670 --> 00:00:51,690 script.sh'. Notice that I have just 24 00:00:51,720 --> 00:00:55,110 added the extension 'sh', that stands 25 00:00:55,110 --> 00:00:57,420 for Shell. And the reason for that 26 00:00:57,450 --> 00:00:59,460 is that I am going now to create 27 00:00:59,490 --> 00:01:01,470 executable script, and this script 28 00:01:01,500 --> 00:01:03,900 will be executed by a specific 29 00:01:03,900 --> 00:01:06,330 process. And that process will be 30 00:01:06,360 --> 00:01:08,760 bash shell. And that's why if 31 00:01:08,760 --> 00:01:10,290 you're going to create executable 32 00:01:10,290 --> 00:01:11,910 scripts that will be executed in 33 00:01:11,910 --> 00:01:15,611 Shell, you should add extension 'sh'. 34 00:01:15,611 --> 00:01:17,730 Of course, you could go on 35 00:01:17,730 --> 00:01:20,100 without any extension; and in such 36 00:01:20,100 --> 00:01:21,600 way you will be able to execute the 37 00:01:21,600 --> 00:01:23,280 file as well. But it's a good 38 00:01:23,280 --> 00:01:25,170 practice to add extension. All 39 00:01:25,170 --> 00:01:27,150 right, let's create a new file 40 00:01:27,150 --> 00:01:30,450 'script.sh'. Let's list files and 41 00:01:30,450 --> 00:01:32,430 folders here. Let me actually 42 00:01:32,430 --> 00:01:34,230 remove 'new-file.txt'. I don't 43 00:01:34,230 --> 00:01:37,560 need it anymore. And now I see 44 00:01:37,560 --> 00:01:40,470 script.sh file here. And notice 45 00:01:40,470 --> 00:01:42,960 permissions for this file. It is 46 00:01:42,990 --> 00:01:45,000 readable, writeable, but not 47 00:01:45,030 --> 00:01:47,490 executable by owner; readable, 48 00:01:47,520 --> 00:01:50,130 writeable, and not executable by any 49 00:01:50,190 --> 00:01:52,260 other users in the same group 50 00:01:52,260 --> 00:01:54,600 'bogdan'; and only readable by 51 00:01:54,630 --> 00:01:56,970 all remaining users in all other 52 00:01:57,000 --> 00:01:59,400 groups. Could you quickly tell me 53 00:01:59,600 --> 00:02:02,300 decimal representation of those permissions? 54 00:02:02,300 --> 00:02:05,300 [Clock Ticking] 55 00:02:05,300 --> 00:02:06,500 This section stands 56 00:02:06,570 --> 00:02:09,750 for 4+2, 6; this 4 57 00:02:09,750 --> 00:02:12,090 +2, also 6; and here it will be 58 00:02:12,120 --> 00:02:14,640 4. So, resulting permission is 59 00:02:14,640 --> 00:02:19,290 664. Great. Now let's make this 60 00:02:19,290 --> 00:02:21,840 file executable. And I want to make 61 00:02:21,840 --> 00:02:24,480 it executable only by owner, this 62 00:02:24,480 --> 00:02:26,850 user, 'bogdan'. And I will modify 63 00:02:26,880 --> 00:02:29,040 only this section. And for that I 64 00:02:29,040 --> 00:02:30,700 could use either decimal 65 00:02:30,700 --> 00:02:32,600 representation of the resulting 66 00:02:32,670 --> 00:02:35,430 permission, or I could add only 67 00:02:35,460 --> 00:02:38,130 execution permission for user. And 68 00:02:38,130 --> 00:02:40,440 for that I'll use 'chmod' command. 69 00:02:40,560 --> 00:02:43,200 And here I'll type 'u', stands for 70 00:02:43,200 --> 00:02:45,900 user; next '+', I'm adding 71 00:02:45,900 --> 00:02:47,340 permission; and here will be 72 00:02:47,400 --> 00:02:50,310 execution permission 'x'; and here 73 00:02:50,340 --> 00:02:53,370 will be name of the file, 'script.sh'. 74 00:02:53,880 --> 00:02:55,710 Let's run it, and let's verify 75 00:02:55,710 --> 00:02:57,810 permissions now. And you'll see 76 00:02:57,840 --> 00:03:01,200 that now this file script.sh is 77 00:03:01,200 --> 00:03:03,480 marked in green color. And now it is 78 00:03:03,540 --> 00:03:06,960 executable by user - read, write and 79 00:03:06,990 --> 00:03:10,350 execute, 'rwx'. And now let's open this 80 00:03:10,380 --> 00:03:13,200 file, 'sh' file in Nano Editor; and 81 00:03:13,200 --> 00:03:15,600 let's add some shell commands there. 82 00:03:16,230 --> 00:03:19,680 'nano script.sh'. And first what you 83 00:03:19,680 --> 00:03:21,750 need to do here in this file is to 84 00:03:21,780 --> 00:03:24,030 add reference to a specific 85 00:03:24,060 --> 00:03:26,970 executable file that will run this 86 00:03:26,970 --> 00:03:29,910 file, this script. And I'm going to 87 00:03:29,910 --> 00:03:32,400 run this script using bash shell 88 00:03:32,400 --> 00:03:34,530 process. And for that you need to 89 00:03:34,530 --> 00:03:37,530 use following line. '#', next 90 00:03:37,560 --> 00:03:39,570 '!', and here will be 91 00:03:39,570 --> 00:03:41,790 path to corresponding executable 92 00:03:41,940 --> 00:03:44,790 that is supposed to run this shell 93 00:03:44,820 --> 00:03:47,640 script, and path is following '/bin 94 00:03:47,760 --> 00:03:50,790 /bash'. Actually, you could 95 00:03:50,790 --> 00:03:53,400 find the full path to bash shell if 96 00:03:53,400 --> 00:03:56,550 you type 'which bash' in terminal. 97 00:03:56,610 --> 00:03:57,930 Actually, let me quickly do that. 98 00:03:57,930 --> 00:04:01,380 Let's exit without saving. And here 99 00:04:01,380 --> 00:04:04,050 type 'which bash', and here in the 100 00:04:04,050 --> 00:04:06,120 system I see that bash executable 101 00:04:06,120 --> 00:04:08,820 file is located in '/usr/bin/bash'. 102 00:04:09,150 --> 00:04:10,680 But let me actually have a look at 103 00:04:10,680 --> 00:04:13,320 contents of the 'bin' folder, 'ls 104 00:04:13,350 --> 00:04:16,320 -l'; and you'll see that this 105 00:04:16,350 --> 00:04:19,230 folder is actually linked to '/usr/ 106 00:04:19,260 --> 00:04:21,000 bin'. We have talked about links 107 00:04:21,000 --> 00:04:23,070 before. And that means that I'm 108 00:04:23,100 --> 00:04:25,860 able to use either this path as 109 00:04:25,860 --> 00:04:27,540 reference to the location of the 110 00:04:27,570 --> 00:04:30,750 bash script, or simply '/bin/bash' 111 00:04:31,110 --> 00:04:33,060 because if I enter '/bin/bash', it 112 00:04:33,060 --> 00:04:35,640 will point to 'usr/bin/bash'. All 113 00:04:35,640 --> 00:04:37,500 right, it sounds a bit complicated, 114 00:04:37,500 --> 00:04:38,820 but in fact it is very simple. 115 00:04:39,060 --> 00:04:40,710 Let's go back to editing of the 116 00:04:40,710 --> 00:04:43,560 file, 'nano script.sh'. And here I'll 117 00:04:43,560 --> 00:04:46,500 type same line, '! 118 00:04:46,620 --> 00:04:50,130 /bin/bash' like so. And 119 00:04:50,160 --> 00:04:52,710 it tells system that this 120 00:04:52,740 --> 00:04:55,380 executable file 'bash' will be used 121 00:04:55,400 --> 00:04:58,400 for execution of the script. Great. And here 122 00:04:58,409 --> 00:05:01,349 let's simply type 'echo', and here in 123 00:05:01,349 --> 00:05:03,869 double quotes, "Hello from the 124 00:05:03,869 --> 00:05:07,409 custom script", like so. Let's now 125 00:05:07,409 --> 00:05:09,599 save contents of this file, like 126 00:05:09,599 --> 00:05:13,079 so. Yes, Enter. And now let's try 127 00:05:13,079 --> 00:05:15,419 to execute this file. And if you 128 00:05:15,419 --> 00:05:17,669 want to execute a specific script, 129 00:05:17,909 --> 00:05:20,399 you need to enter simply absolute 130 00:05:20,459 --> 00:05:24,119 or relative path to this script. In 131 00:05:24,119 --> 00:05:26,429 this case, this script is located 132 00:05:26,429 --> 00:05:28,589 here in home directory for this 133 00:05:28,619 --> 00:05:31,469 user. It is actually current folder 134 00:05:31,499 --> 00:05:35,939 represented by '.' or by '~' sign. It 135 00:05:35,939 --> 00:05:38,489 is home directory for this user, or 136 00:05:38,489 --> 00:05:41,489 absolute path is '/home/ 137 00:05:41,519 --> 00:05:44,969 bogdan/script.sh'. And 138 00:05:44,969 --> 00:05:46,889 actually, you are able to use any 139 00:05:46,919 --> 00:05:49,679 of those variants. Any will work. 140 00:05:50,099 --> 00:05:51,689 Let's go try to use this 141 00:05:51,689 --> 00:05:53,939 absolute path, and simply press 142 00:05:53,999 --> 00:05:56,399 Enter. And I see, 'Hello from the 143 00:05:56,399 --> 00:05:59,099 custom script'. And that means that 144 00:05:59,129 --> 00:06:02,039 bash shell has executed this 145 00:06:02,069 --> 00:06:06,209 script.sh file. You don't need to 146 00:06:06,239 --> 00:06:08,369 enter any arguments or options or 147 00:06:08,399 --> 00:06:10,979 any other commands, simply path to 148 00:06:10,979 --> 00:06:12,809 corresponding executable script. 149 00:06:12,839 --> 00:06:15,959 That's it. Now let's try to use 150 00:06:15,989 --> 00:06:18,310 relative path and simply type 151 00:06:18,310 --> 00:06:23,399 './script.sh', and I see same 152 00:06:23,399 --> 00:06:25,169 result, 'Hello from the custom 153 00:06:25,169 --> 00:06:27,779 script'. Let's try to use reference 154 00:06:27,779 --> 00:06:30,329 to home directory, '~/ 155 00:06:30,359 --> 00:06:34,409 script.sh'. And I see same result 156 00:06:34,409 --> 00:06:37,049 again. But if I enter simply 157 00:06:37,079 --> 00:06:40,739 script.sh, I'll get 'command not 158 00:06:40,739 --> 00:06:43,589 found'. And now question to you, why 159 00:06:43,589 --> 00:06:45,689 I get this error when I entered 160 00:06:45,689 --> 00:06:47,489 this command without full path to 161 00:06:47,489 --> 00:06:50,489 it? Answer is very simple. At the 162 00:06:50,489 --> 00:06:52,199 beginning of this course, I have 163 00:06:52,199 --> 00:06:53,939 explained to you what is the PATH 164 00:06:53,969 --> 00:06:56,759 variable, and how actually Linux 165 00:06:56,759 --> 00:06:58,919 shell understands that you want to 166 00:06:58,919 --> 00:07:01,739 enter a specific command. And if I 167 00:07:01,739 --> 00:07:05,339 enter 'echo $PATH', I will 168 00:07:05,339 --> 00:07:07,499 see a set of different paths that 169 00:07:07,529 --> 00:07:09,299 are concatenated using a colon. 170 00:07:09,779 --> 00:07:11,849 For example, here is location of 171 00:07:11,879 --> 00:07:15,551 some executable files, '/usr/local/sbin', 172 00:07:15,551 --> 00:07:18,569 next one, next one, and so on. 173 00:07:19,289 --> 00:07:22,499 And now this script.sh is located 174 00:07:22,499 --> 00:07:25,679 simply in '/home/bodgan'. 175 00:07:26,279 --> 00:07:29,789 And this path is not in this list, 176 00:07:30,749 --> 00:07:33,209 that's why shell is not able to 177 00:07:33,209 --> 00:07:36,029 recognize that I want to run some 178 00:07:36,059 --> 00:07:40,469 script named script.sh. If you want 179 00:07:40,469 --> 00:07:43,079 to be able to run some scripts in 180 00:07:43,079 --> 00:07:45,359 such way without full path, you 181 00:07:45,359 --> 00:07:47,369 need to add the folder name where 182 00:07:47,369 --> 00:07:50,129 the script is located to your PATH. 183 00:07:50,639 --> 00:07:52,919 Simply concatenate to existing 184 00:07:52,919 --> 00:07:56,009 PATH. But it's not a good practice 185 00:07:56,129 --> 00:07:59,069 to add home directory to PATH. It's 186 00:07:59,069 --> 00:08:00,899 a good practice to create 'bin' 187 00:08:00,899 --> 00:08:02,639 folder in home directory for your 188 00:08:02,639 --> 00:08:05,309 user and place all executable 189 00:08:05,309 --> 00:08:07,919 scripts there. But let's talk about 190 00:08:07,919 --> 00:08:09,329 it in the next lecture. I will show 191 00:08:09,329 --> 00:08:10,799 you how to do that, and how to 192 00:08:10,799 --> 00:08:12,749 modify path, and next after this 193 00:08:12,749 --> 00:08:14,939 action you will be able to execute 194 00:08:14,969 --> 00:08:17,729 script like this anywhere in any 195 00:08:17,729 --> 00:08:19,229 folder. I will see you in the next 196 00:08:19,229 --> 00:08:20,351 lecture guys. Bye-Bye. 197 00:08:20,351 --> 00:08:22,592 [no audio]