We often take files for granted, but they're really important. In this video, explore what files are and how they work.
- [Instructor] On Linux, as with other operating systems, we organize information into files and we organize those files into folders. Files are collections of data that represent different kinds of information. From text files to binaries like applications, photos, videos and music. And files even represent hardware on the system, process information and kernel parameters. But whatever a file represents, it's just a series of binary information or bits on the disk. These bits are stored in blocks on the disk. And the locations of these blocks are tracked by the file system.
This information about the location of each files blocks are kept in a data structure called an inode. And the inodes are kept in an inode table. So while the actual data for files is scattered around the disk in blocks of binary information, we view the contents of the file system as a hierarchy of folders within folders. Like branches of a tree. What we see as files in this structure are really hard links to each files inode. And when you request the contents of a file in order to run it, edit it or view it, the system goes out and looks up all the blocks that represent pieces of that file and return the data from them.
File systems and how they represent data are a pretty interesting area to explore, if you're so inclined. But I'll leave that there for now. How the data that makes up a file is represented is one part of the story. But what's more important to many users is what the file represents. Sometimes we'll use files that have an extension. You're probably familiar with dot txt files, dot jpg files, dot html, dot css and dot js, if you're a web developer and so on. We use extensions as a kind of shorthand to indicate to humans and to software what a particular file is intended for.
The extension is added to the end of a file name. Separated by a dot or a period. But it's still just part of the files name. One of the aspects of metadata that the file systems stores. There are other elements of metadata for files. Their size, their creation and modification dates, settings for permissions or who can access them and in what way, ownership information and more. If I have a photo like cat dot jpg and I rename the file to just cat or cat dot mp3, it doesn't change the fact that the file represents a photo of a cat.
The information on disk that the file entry refers to remains the same even though photo management programs might stop recognizing the file as an image. Because file extensions aren't reliable indicators of what a file contains, we can use some other tools to get a better idea of what our file is. Files are stored in particular formats that are defined so that a program knows how to use the information that a file represents. And most of these file formats can be recognized by what's called a magic number. A particular value at the beginning of the file that more reliably represents what's in a file.
We can use the file command to read these magic numbers and tell us what kind of file something actually is. Alright, file cat dot jpg. And here I can see that this is JPEG image data. I also have a few files here that don't have extensions so let's figure out what those are. Alright, file mysterious file one. And it turns out this ones a JPEG as well. Mysterious file two is some text.
And mysterious file three is an audio file. In this case, an MPEG layer three or MP3 file. We can also find out more about our files metadata with the stack command. This shows the file size. How many blocks it takes up. And a bunch of other information that can be helpful to know about. I'll write stat cat dot jpg. And here I can see it takes up about 3.1 megabytes, 6200 blocks. Resides at inode 5259774.
And so on. While files are somewhat more complicated than they look at first glance, there's nothing mysterious about them. Even though there is a little bit of magic involved.
Note: Because this is an ongoing series, viewers will not receive a certificate of completion.