Hello the beautiful people and welcome to this video all about file extensions in Linux.
Now on operating systems such as Windows file extensions.
Allow me to tell what type of file that you're dealing with at a glance.
For example, we know that files with names ending in txt are text files and files with names ending
in jpg or PNG are likely to be images, but in Linux, file extensions usually don't matter and the
system tends to look at the contents of the file rather than just its name.
But sometimes file extensions really do matter.
So in this video, we're going to take a bit of a deep dive into understanding file extensions in Linux
and shine some light onto their strange behavior.
And by the end of the video, you'll know what freedoms the Linux operating system gives you when naming
your files, what interesting things can happen, and what you need to be aware of.
Oh, and you'll also have a new command in your toolkit that will help you when dealing with files on
your file system.
So this is all very useful stuff, very exciting stuff.
Let's go ahead and jump right into it.
All right.
So I've gone ahead and put some files here on my desktop.
First of all, I've got an image of Tux, the traditional Penguin logo for Linux here in P and G format.
Now, Tux, in case you're interested, is short for the word tuxedo, which is a fancy type of black
and white suit, usually worn by waiters and partially dressed people at high class parties.
Now penguins are said to look like they're wearing little tuxedos because of their black and white coloring.
And that's where Tux gets his name from the word tuxedo, in case you were interested.
But besides a picture of of the lovely tux here and random trivia, I've also got a text file here with
some Shakespeare in it called Shakespeare Text.
And I also have a spreadsheet file with some random numbers in it.
So let's go ahead and take a look at the file command to see how Linux handles file types.
So if I go ahead and open a terminal using the control alternate keyboard shortcut and just drag it
a bit out of the way so we can still see the files.
I'm going to introduce you to a new command called the File Command.
Now, the file command will tell us basically what type of file that we are dealing with.
So, for example, if we try to have a look at tux here, if we do file tux PNG, we actually need to
go to our desktop first.
So let's shift over to our desktop.
Clear the screen and do file tux.
And I can actually just just so you see, I can actually just with t you go ahead and press tab and
it will autofill that as well just showing you that tab.
Auto completion works perfectly fine for far names, just like it does for folders.
So it's a great, great thing that we learned in the last video.
Okay, so let's have a look file of tux PNG and it will tell us that this is PNG image data.
It'll tell us the size of the file and loads of other stuff as well, but it tells us the file type.
Okay.
Now if I rename txt png if I rename this to tux dot jpg, which is another type of image file, you
can see that it's been renamed there.
But let's do that again.
Let's try.
File of txt jpg and let's see what happens.
Now you can see here that it's still says that it is a p and g file.
Now this is important.
Point number one in Linux file extensions don't matter.
Unlike Windows, Linux doesn't determine file type using the file extension, but instead it reads a
piece of code inserted at the top of every file.
And that piece of code is known as a header and it serves as a kind of label for Linux to read.
And when Linux reads that label, it knows the file type.
So changing just the name of the file won't make a difference because that file header won't have changed.
Okay, so here we have some Shakespeare script in text format.
So if I open that up, you can see that there is plenty of text in there which I just copied and pasted
from Romeo and Juliet.
Okay, so that's the kind of stuff that I do.
So when we check the file type we have here, if we do file Shakespeare text, it'll tell us that it
is an ASCII text file.
But now if I try to turn this into a PDF, so if I rename it and try to say, okay, this is now Shakespeare
dot PDF and I rename it now the system, it appears to have changed the icon.
Right?
It looks like that's a PDF file, but if I try to run the file file command again file Shakespeare dot
pdf, it still tells us that it's ASCII text.
So Linux read the Linux, read the header of the file and saw that it was still a text file.
But what happens when we try to open it because it looks like the system thinks it's a PDF because you
can see the logo is changed but it's still telling us that it's a text file.
What's going on there?
Why is it think it's different?
Well, if we try to open it, what happens?
Well, now we get an error.
We see that the default program to open files with names ending in PDF is a PDF reader.
And when we try to open this file in the PDF Reader, we get an error from the program as it's trying
to open our file as a PDF file.
And what the system basically did is say, Ah, that's a file that ends in PDF, it's probably a PDF
file, so let me be helpful and allow the user to open it in a PDF viewer.
Now obviously a PDF reading program isn't designed to read text files, it's designed to read PDFs,
and that's what's causing the issue here.
Now, this is important.
This is an important distinction to make.
Important point number two, although the operating system will know what file format the actual file
is by reading the header, the extra programs that are installed on top of the operating system such
as this PDF viewer are trying to open the files that might require certain formats or other file extensions
in order to work.
So what's happening is it's all this to make this automatically want to open as a PDF.
It just saw the file name, it just looked at the file name, but the content is still text and it says
here are file type.
Plain text is not supported, but it got the program got tricked, it saw the file name and that's why
it wanted to open it as a PDF.
If I rename it back to text, just change the text, change that back.
You'll see now that it will open fine as a text file.
Okay, so that's the important distinction to make there.
The programs that are installed on top of the operating system such as PDF viewers and stuff like that,
they might need the file to have a specific file extension in order to open them, but the operating
system itself does not care.
So let's see this in a bit more detail.
If I take a look at the spreadsheet here, it's called data downloads.
If I do file of data downloads, it says that it's an ODS file which stands for open document spreadsheets.
Okay, now this means we can open it up in LibreOffice Calc, which is basically the Linux version of
Microsoft Excel or the Apple equivalent.
It's like this spreadsheet program and it's got it's got a few numbers in there.
Okay.
But, um, now if I close the spreadsheet and rename that, for example, to a text file, so if I rename
it to data txt rename that, you can see that now it wants to be opened up in a dot txt format.
Okay.
And now I try to open that up in LibreOffice Calc.
So if I click up here, I type calc and it comes up, I click LibreOffice Calc.
Wait for that to boot up.
Go on.
File open.
Go to my desktop.
So home and then desktop and then try to open up data like text.
You can see now that we've got all kinds of problems happening here.
So if I tried to just open it like that, let's try and click.
Okay, we're going to get an error.
Okay.
And this is because the dot txt file extension is fooling LibreOffice Calc.
LibreOffice Calc made the mistake of reading the file extension and trying to open the file as a dot
text file.
Where is the actual content?
As we can see, here is a spreadsheet file.
It's not a text file.
Okay.
But if we rename that back and we rename it so that it's data downloads now, it will open fine.
So this is the important distinction that we need to make.
I'm going to hammer this home.
The file extension isn't important to the Linux operating system, but it might be important to the
software packages that we install on top of Linux.
So do bear in mind that don't basically don't give your file names, really weird file extensions that
don't match their content because it might confuse third party software that is installed even though
you don't necessarily have to.
Okay, so let's give the spreadsheet a complete lead.
Let's try something else.
Let's see something quite interesting.
Let's give the spreadsheet a completely made up file extension.
So if I call it spreadsheet dot sublime, I'll call it spreadsheet problem.
Now it's not interesting.
Notice how it automatically still wants to open it up in the spreadsheet software.
Now we notice here that we didn't fool the the the software and it still knows it's an ODS file and
we can actually open it up if I just go ahead and double click that.
It's going to open up perfectly.
Fine.
You see, we've got our data in here.
No funny business this time.
So why can this system, why can it open up a weird file name but not a simple one like dot txt?
Well, the reason is the software just simply didn't recognize the dot sablan file extension, so it
might have looked to the operating system to help determine the file type instead.
But when we called it a dot txt file, when we actually named it directly to be txt, the software thought
it would be a bit clever and it thought, aha, I know how to deal with this.
And it made a little bit of a shortcut to itself and was like, I'm just going to open this automatically
as a text file.
But that assumption was wrong in this case because the content is a spreadsheet, we just named it txt
and that caused the program to throw an error when it found out it wasn't a txt file after all, but
actually was a spreadsheet.
Now the bottom line is that you have a lot of freedom to name files, whatever you want in Linux.
You can even call them.
You can even give them file extensions like Dot Shibboleth, which of course you couldn't do in any
other operating system.
This is common for having files.
For example, if I just try and copy and paste this, if I copy and paste this file, you know, you
might have a backup.
You might, for example, call one file in original so you'd say, okay, I'm going to call this one
dot ridge, and then I'd call this one, you know, maybe I'll call another one dot back or a copy it
and call it dot, dot, dot copy or something like that.
Okay.
And this is how you might name your file.
So I'd be like, Ah, this is the original, this is the copy I can see just by looking at the file
name without it actually affecting the way the system works.
So this is sometimes used to allow people to read file extensions that are a lot more human readable
than, you know, things like dot odds or dot whatever.
But just make sure that if you name your files in such a way, don't give them file names.
That might confuse the software that you expect to open them.
Because not all softwares like to play ball with strange file names.
Okay.
So that will bring our discussion of Linux file extensions to a close.
So as a summary of what you learn in this video, you learn that you can use the file command to know
for sure what type of file that you're dealing with.
And you learn that file extensions aren't important to the Linux operating system, which means you
can name files, whatever you want.
Yes, even dot qibla.
But while you're doing that, make sure you try not to confuse third party software that might need
to open the file because it might be expecting specific file extensions.
And if you try to confuse it, then you'll just get errors in a very, very bad time.
So coming up next in the next video, we're going to take your command line skills to a whole new level
by looking at the concept of wild cards.
So let's say you had a really messy desktop and you wanted to move all of your images to one folder
and all of your PDFs to another folder and all of your spreadsheets and office files to another folder
using the command line.
How can you use a command that's actually so specific?
Well, you can do that using wild cards.
Wild cards allow you to make your commands really, really specific and also really general, thereby
allowing you to level up in your control over what commands actually operate on.
Now, this is an incredibly useful ability when dealing with files.
So for another dose of epic superpowers, I'll see you in the next video.