1 00:00:06,720 --> 00:00:08,550 - In this video, we are going to explore 2 00:00:08,550 --> 00:00:11,040 the Linux filesystem hierarchy. 3 00:00:11,040 --> 00:00:15,510 So before we do so, why are files that important? 4 00:00:15,510 --> 00:00:17,100 Well, it's easy to understand. 5 00:00:17,100 --> 00:00:19,740 On Linux, everything is a file. 6 00:00:19,740 --> 00:00:22,200 You want to access your hard disk? 7 00:00:22,200 --> 00:00:23,280 It's a file. 8 00:00:23,280 --> 00:00:24,420 Everything is a file. 9 00:00:24,420 --> 00:00:28,620 So the ability to work with files is really key 10 00:00:28,620 --> 00:00:29,973 to mastering Linux. 11 00:00:30,840 --> 00:00:32,520 Also, configuration is stored 12 00:00:32,520 --> 00:00:35,700 in mostly ASCII text configuration files. 13 00:00:35,700 --> 00:00:37,560 So if you know how to deal with files, 14 00:00:37,560 --> 00:00:41,790 then you can create configurations, and that's convenient. 15 00:00:41,790 --> 00:00:45,660 Devices are addressed by using device files as well. 16 00:00:45,660 --> 00:00:49,020 And in order to define where your files should end up, 17 00:00:49,020 --> 00:00:51,150 there is the Linux filesystem 18 00:00:51,150 --> 00:00:55,083 as defined in the Linux Filesystem Hierarchy Standard. 19 00:00:56,190 --> 00:00:58,080 So the good thing is that directories 20 00:00:58,080 --> 00:01:00,480 are highly standardized on Linux. 21 00:01:00,480 --> 00:01:03,600 Doesn't really matter which distribution you are on. 22 00:01:03,600 --> 00:01:06,900 You will always find the /usr directory. 23 00:01:06,900 --> 00:01:09,418 Now that we talk about it, let me explain what it is. 24 00:01:09,418 --> 00:01:12,060 /usr is for your program files. 25 00:01:12,060 --> 00:01:14,460 So that is where you will find all the commands 26 00:01:14,460 --> 00:01:17,310 that you are using in a Linux environment. 27 00:01:17,310 --> 00:01:19,743 There's a /var directory, that's the directory 28 00:01:19,743 --> 00:01:21,750 that different services are using 29 00:01:21,750 --> 00:01:23,925 to dynamically create files. 30 00:01:23,925 --> 00:01:27,090 /var/log, for instance, contains your log files, 31 00:01:27,090 --> 00:01:29,040 and if anything needs to be cached, 32 00:01:29,040 --> 00:01:31,950 big chance that it is written to /var/cache. 33 00:01:31,950 --> 00:01:33,197 There's the /etc directory. 34 00:01:33,197 --> 00:01:36,780 The /etc directory is what contains 35 00:01:36,780 --> 00:01:39,270 your configuration files. 36 00:01:39,270 --> 00:01:41,790 So if you're looking for configuration, big chance 37 00:01:41,790 --> 00:01:44,793 that it is in the /etc directory. 38 00:01:46,200 --> 00:01:49,208 Now the nice thing about this Linux filesystem is 39 00:01:49,208 --> 00:01:51,060 that the file system is defined 40 00:01:51,060 --> 00:01:53,370 in the Filesystem Hierarchy Standard. 41 00:01:53,370 --> 00:01:57,150 I included the link to linuxfoundation.org. 42 00:01:57,150 --> 00:01:58,530 Now, what is Linux Foundation? 43 00:01:58,530 --> 00:02:02,940 Well, Linux Foundation is the organization behind Linux. 44 00:02:02,940 --> 00:02:06,570 All the Linux distributions are a member of Linux Foundation 45 00:02:06,570 --> 00:02:09,840 and Linux Foundation defines how stuff should happen. 46 00:02:09,840 --> 00:02:11,550 And one of the important things is 47 00:02:11,550 --> 00:02:14,580 the FHS or Filesystem Hierarchy Standard. 48 00:02:14,580 --> 00:02:16,170 Actually, it also exists 49 00:02:16,170 --> 00:02:18,840 as a man page on many Linux distributions, 50 00:02:18,840 --> 00:02:23,040 and that is what I'm going to show you in just a minute. 51 00:02:23,040 --> 00:02:25,950 Now, talking about these directories, you should notice 52 00:02:25,950 --> 00:02:28,260 that the regular users have write access 53 00:02:28,260 --> 00:02:33,260 to just two directories, which are /home and /tmp. 54 00:02:33,390 --> 00:02:34,910 Just to make sure we understand one another, 55 00:02:34,910 --> 00:02:39,000 /home, which contains their own home directory, 56 00:02:39,000 --> 00:02:41,340 a user can write to their own home directory, 57 00:02:41,340 --> 00:02:43,221 not to anybody else's, 58 00:02:43,221 --> 00:02:45,990 and a /tmp directory is a traditional location 59 00:02:45,990 --> 00:02:48,600 where everybody can create files. 60 00:02:48,600 --> 00:02:53,600 Now, write ability on Linux is sometimes pretty frustrating 61 00:02:53,760 --> 00:02:55,320 if you're new to Linux. 62 00:02:55,320 --> 00:02:57,810 So if you're looking for an easy way to verify 63 00:02:57,810 --> 00:03:00,240 if you can write, use a touch command. 64 00:03:00,240 --> 00:03:04,530 touch filename will allow you to verify write access. 65 00:03:04,530 --> 00:03:08,190 It creates a file in a specific location. 66 00:03:08,190 --> 00:03:09,633 Now let's go check this out. 67 00:03:11,940 --> 00:03:12,960 All right, to start with, 68 00:03:12,960 --> 00:03:15,000 I'm showing you man hier. 69 00:03:15,000 --> 00:03:17,340 hier is for the filesystem hierarchy. 70 00:03:17,340 --> 00:03:18,173 There we go. 71 00:03:18,173 --> 00:03:20,460 This is a filesystem hierarchy as defined 72 00:03:20,460 --> 00:03:22,110 by the Linux Foundation. 73 00:03:22,110 --> 00:03:23,573 And if in any case you think, 74 00:03:23,573 --> 00:03:27,480 oh, what is this or that directory used for? 75 00:03:27,480 --> 00:03:30,600 You can see it is pretty specific and details 76 00:03:30,600 --> 00:03:33,333 what is happening in the filesystem hierarchy. 77 00:03:34,260 --> 00:03:35,760 Now I would like to 78 00:03:35,760 --> 00:03:38,310 to show you the directories in the route. 79 00:03:38,310 --> 00:03:42,300 So I'm going to use cd / to go 80 00:03:42,300 --> 00:03:45,990 to the root directory, the root directory of this system, 81 00:03:45,990 --> 00:03:50,990 and I'm using ls - l so that we can see what is going on. 82 00:03:51,060 --> 00:03:53,220 And what do we see? 83 00:03:53,220 --> 00:03:55,980 Well, we see the different directories that exists, 84 00:03:55,980 --> 00:03:59,520 and hey, there is even a new directory, afs. 85 00:03:59,520 --> 00:04:03,297 Just wondering, man hier do we have afs? 86 00:04:03,297 --> 00:04:05,130 afs is so new that it's not 87 00:04:05,130 --> 00:04:07,590 in the filesystem hierarchy, and you know what? 88 00:04:07,590 --> 00:04:10,890 That means that we can safely ignore it for now. 89 00:04:10,890 --> 00:04:13,560 Let's have a look at the directories that do matter. 90 00:04:13,560 --> 00:04:15,720 To start with this one, bin. 91 00:04:15,720 --> 00:04:18,150 bin in Linux stands for binary, 92 00:04:18,150 --> 00:04:20,640 and a binary is a command, a command file, 93 00:04:20,640 --> 00:04:24,270 a program file that can be used by ordinary users. 94 00:04:24,270 --> 00:04:25,803 Likewise, there is sbin. 95 00:04:25,803 --> 00:04:27,420 sbin is a system binary. 96 00:04:27,420 --> 00:04:31,053 You need pseudo privileges in order to use that. 97 00:04:32,190 --> 00:04:34,050 An interesting thing here is going on 98 00:04:34,050 --> 00:04:37,500 and that is that bin is pointing to usr/bin. 99 00:04:37,500 --> 00:04:39,690 That's what we call a symbolic link. 100 00:04:39,690 --> 00:04:41,100 The thing is that in the past 101 00:04:41,100 --> 00:04:42,780 the bin directory had its function. 102 00:04:42,780 --> 00:04:47,010 Nowadays, the bin directory doesn't have a function anymore 103 00:04:47,010 --> 00:04:49,140 according to the filesystem hierarchy, 104 00:04:49,140 --> 00:04:51,000 and everything that in the past was stored 105 00:04:51,000 --> 00:04:54,330 in bin, nowadays, should be stored in usr/bin. 106 00:04:54,330 --> 00:04:58,230 And this symbolic link makes that if you're using any script 107 00:04:58,230 --> 00:05:00,810 or anything else that is still referring to bin, 108 00:05:00,810 --> 00:05:02,970 you automatically end up in usr/bin, 109 00:05:02,970 --> 00:05:04,863 which is the appropriate location. 110 00:05:06,750 --> 00:05:09,810 Now related to bin and sbin, 111 00:05:09,810 --> 00:05:13,410 we also have lib and lib64, which is about libraries. 112 00:05:13,410 --> 00:05:15,780 These are the libraries, belonging 113 00:05:15,780 --> 00:05:17,643 to the files in bin and sbin. 114 00:05:17,643 --> 00:05:18,690 Then we have boot. 115 00:05:18,690 --> 00:05:21,330 boot contains everything that you need to boot. 116 00:05:21,330 --> 00:05:24,930 If you check the contents of boot, then, what do we see? 117 00:05:24,930 --> 00:05:29,930 We see files like, where is it? vmlinux. 118 00:05:30,030 --> 00:05:32,190 vmlinux, that's your Linux kernel. 119 00:05:32,190 --> 00:05:34,530 That's the heart of the operating system 120 00:05:34,530 --> 00:05:35,940 and other stuff that is needed 121 00:05:35,940 --> 00:05:38,704 to boot your system is right there. 122 00:05:38,704 --> 00:05:40,440 Then we have the dev directory 123 00:05:40,440 --> 00:05:44,400 and the dev directory is where you will find devices. 124 00:05:44,400 --> 00:05:47,400 Devices is what allows you to access your hardware, 125 00:05:47,400 --> 00:05:51,547 and one famous device that I want to talk about is sda. 126 00:05:52,527 --> 00:05:54,900 sda typically is your primary hard disk, 127 00:05:54,900 --> 00:05:57,600 or sr0, which is your primary, 128 00:05:57,600 --> 00:06:01,620 which is your CDROM, your optical device. 129 00:06:01,620 --> 00:06:03,720 We will talk about these devices later, 130 00:06:03,720 --> 00:06:05,340 but let's not do that now. 131 00:06:05,340 --> 00:06:06,900 Let's have a look at etc. 132 00:06:06,900 --> 00:06:09,150 So what do we find in etc? 133 00:06:09,150 --> 00:06:11,520 Text-based configuration files. 134 00:06:11,520 --> 00:06:12,990 That's the nice thing about Linux. 135 00:06:12,990 --> 00:06:15,150 Anything you do on Linux is stored 136 00:06:15,150 --> 00:06:17,970 in a text-based configuration files. 137 00:06:17,970 --> 00:06:22,410 For instance, if we do cat on etc/passwd, 138 00:06:22,410 --> 00:06:25,740 that's the user database, and we can see all the users 139 00:06:25,740 --> 00:06:27,780 currently existing on the system. 140 00:06:27,780 --> 00:06:29,760 More about users later. 141 00:06:29,760 --> 00:06:31,830 Back to these directories, what else do we have? 142 00:06:31,830 --> 00:06:33,210 We have home. 143 00:06:33,210 --> 00:06:35,910 So ls on /home is showing 144 00:06:35,910 --> 00:06:37,740 that there is the home directory 145 00:06:37,740 --> 00:06:40,473 for the one and only user on this system. 146 00:06:41,340 --> 00:06:43,050 Then we have media and mnt. 147 00:06:43,050 --> 00:06:46,170 media and mnt is for mounting stuff. 148 00:06:46,170 --> 00:06:47,790 Now, what is mounting stuff? 149 00:06:47,790 --> 00:06:49,200 Mount is what you use 150 00:06:49,200 --> 00:06:53,343 on Linux to connect a specific device to a directory. 151 00:06:54,210 --> 00:06:55,770 I will also explain that later 152 00:06:55,770 --> 00:06:57,840 but the thing is, if, for instance, you want to 153 00:06:57,840 --> 00:07:01,470 access a USB thumb drive, the USB thumb drive 154 00:07:01,470 --> 00:07:04,800 needs to be mounted to make it accessible, and media 155 00:07:04,800 --> 00:07:07,860 and mnt is where the mounts are happening automatically. 156 00:07:07,860 --> 00:07:10,468 We'll talk about it in more detail later. 157 00:07:10,468 --> 00:07:14,070 opt is an optional directory, not always used. 158 00:07:14,070 --> 00:07:16,860 proc is a very special directory. 159 00:07:16,860 --> 00:07:20,430 This is providing an interface to what the kernel is doing. 160 00:07:20,430 --> 00:07:22,950 We'll talk about it later as well. 161 00:07:22,950 --> 00:07:24,210 root, can we go there? 162 00:07:24,210 --> 00:07:25,140 No, we can't. 163 00:07:25,140 --> 00:07:26,190 And why is that? 164 00:07:26,190 --> 00:07:29,220 Well, that is because this is the home directory 165 00:07:29,220 --> 00:07:31,383 for our root user. 166 00:07:32,580 --> 00:07:33,413 Then we have run. 167 00:07:33,413 --> 00:07:36,600 run is for temporary files. 168 00:07:36,600 --> 00:07:38,160 It's a relatively new addition 169 00:07:38,160 --> 00:07:40,470 to the Filesystem Hierarchy Standard, 170 00:07:40,470 --> 00:07:42,540 and the standard describes that 171 00:07:42,540 --> 00:07:45,630 if a process dynamically creates files, 172 00:07:45,630 --> 00:07:49,473 it can do it in a private environment in the run directory. 173 00:07:50,850 --> 00:07:52,027 Next, there is srv. 174 00:07:52,940 --> 00:07:57,570 srv is for services, not used by default. 175 00:07:57,570 --> 00:07:58,587 There is sys. 176 00:07:58,587 --> 00:08:02,250 sys for managing hardware, a pretty advanced directory. 177 00:08:02,250 --> 00:08:05,400 You have no business going there if you are new to Linux. 178 00:08:05,400 --> 00:08:08,580 We have tmp, which is for your temporary files, 179 00:08:08,580 --> 00:08:12,543 one of the few directories that is writeable by anybody. 180 00:08:13,410 --> 00:08:14,787 We have usr. 181 00:08:14,787 --> 00:08:18,120 usr is so important that we should have a look at it. 182 00:08:18,120 --> 00:08:23,120 In usr, you find a hierarchy for your bin, your binaries, 183 00:08:24,240 --> 00:08:28,770 your sbin, your program binaries, your libraries 184 00:08:28,770 --> 00:08:30,690 and there is usr/local. 185 00:08:30,690 --> 00:08:34,230 Hey, now I'm just wondering, man hier, do we find anything 186 00:08:34,230 --> 00:08:38,700 about /usr/local in man hier? 187 00:08:38,700 --> 00:08:40,140 And there we go. 188 00:08:40,140 --> 00:08:43,080 It's telling us this is where programs, which are local 189 00:08:43,080 --> 00:08:44,790 to the site typically go. 190 00:08:44,790 --> 00:08:46,140 And you know what that means? 191 00:08:46,140 --> 00:08:47,400 This is an important one. 192 00:08:47,400 --> 00:08:51,150 If you are writing a script or anything and you 193 00:08:51,150 --> 00:08:54,720 want to provide it on the system, you should copy it 194 00:08:54,720 --> 00:08:59,130 to usr/local and preferably to usr/local/bin 195 00:08:59,130 --> 00:09:01,650 because the script will be considered a binary, 196 00:09:01,650 --> 00:09:05,100 and binaries need to be in usr/local/bin 197 00:09:05,100 --> 00:09:07,920 Last but not least, there's the var directory. 198 00:09:07,920 --> 00:09:11,130 var is for stuff that is created dynamically, 199 00:09:11,130 --> 00:09:14,700 with one of the most commonly known directories in var, 200 00:09:14,700 --> 00:09:16,920 the var/log directory. 201 00:09:16,920 --> 00:09:21,510 That is where your logging service might write files. 202 00:09:21,510 --> 00:09:23,460 And I say might write files 203 00:09:23,460 --> 00:09:26,760 because nowadays, there is a systemd-journald, and because 204 00:09:26,760 --> 00:09:30,570 of systemd-journald, you may find an empty directory here, 205 00:09:30,570 --> 00:09:33,543 but that's also a topic that we will talk about later.