The Linux File Types - or - "Content Matters The Most"
The statement “everything is a file” is a kind of Unix/Linux-philosophy.
The idea here is the following:
No matter what you wanna accomplish on a system - everything you need for getting the job done are tools to work somehow with files.
Print them out, modify them, copy them, move them … you name it.
Well - that doesn’t apply to really everything. But as you are working more and more on Linux systems, you will recognize this pattern again and again.
So let’s talk about files …
The filename extension doesn’t matter
On a Linux system, you will often see files with names that end with an extension like “.conf”, “.sh” or “.pdf” just to name a few.
But on a Linux system - these extensions are cosmetic only.
They don’t have any meaning for how you can use the file on the system. A PDF-file could be named “report.txt” and a picture “kitten.conf”.
The only things that count for a Linux system are
- the type of the file and
- the content of the file
Seven Types of files
If on a Linux system, you’ll come in contact with several types of files. For the first steps on a Linux system the following three are most relevant: regular files, directories and symbolic links.
Type 1: Regular files
Regular files are files as we understand and know them from other systems: Some type of object that has some data as content “in it”.
In the output of “ls -l” regular files can be recognized by the dash as the first character of the output line:
robert@demo:~$ ls -l afile
-rw------- 1 robert robert 1676 Jan 14 08:53 afile
Type 2: Directories
Directories are known from other systems too. These are containers not for data but for other files.
You can recognize them in the output of “ls -l” by the character “d” as the first character of the output line.
robert@demo:~$ ls -l adirectory
drwx------ 1 robert robert 1676 Jan 14 08:53 adirectory
Type 3: Symbolic links
Sometimes it can be handy to reach the same file (or directory) via multiple different names. This can be accomplished with symbolic names.
These types of files are merely pointers to a different file.
On Linux systems you’ll see symbolic links at many places and you can recognize them easily by the leading “l” in the output of ls -l:
robert@demo:~$ ls -l alink
lrwxrwxrws 1 robert robert 1676 Jan 14 08:55 alink -> afile
And you’ll see at a glance which file the link refers to.
(BTW: If you are interested in how links at a Linux system are working, how to create them and what the difference between a hardlink and a softlinks is: have a look at Linux - Hardlinks and Softlinks explained)
The other four types of files
Just to be complete: The other four types of files are the following …
-
FiFO … recognizable by leading p
(usage: one way communication between processes) -
Socket … recognizable by leading s
(usage: two way communication between processes) -
Block Device Files … recognizable by leading b
(usage: communicate with block-oriented-devices) -
Character Device Files … recognizable by leading c
(usage: communicate with character-oriented-devices)
Content matters most (not the file-extension)
Content matters most - this is especially true if dealing with regular files.
If the filename extension doesn’t tell us what data we can expect from a file, we have no choice but to open the file and try to interpret its content.
Every type of content can somehow be recognized by looking at the start of the content. A PNG file for instance would start with “.PNG”, a PDF-file with “%PDF-“ and a Linux executable with “.ELF”.
But luckily we don’t have to do the work or interpretation ourselves.
Instead we can leverage the power of the tool “file”.
The command “file” does the real magic
If you wanna know the type of content for a file, just use the the command file and the the name of the file you wanna analyse as a parameter like this:
robert@demo:~$ file somefile
somefile: PDF document, version 1.5
and voila - you are done.
side note: The command “file” uses for recognizing the content-type a central library essential to every system: The library “libmagic”. It’s Pure Magic! ;-)
Here is what to do next
If you followed me through this article, you certainly have realized that knowing some internals about how things are working at the Linux command line, can save you a lot of time and frustration.
And sometimes it’s just fun to leverage these powerful mechanics.
If you wanna know more about such “internal mechanisms” of the Linux command line - written especially for Linux beginners
have a look at “The Linux Beginners Framework”
In this framework I guide you through 5 simple steps to feel comfortable at the Linux command line.
This framework comes as a free pdf and you can get it here.
Wanna take an unfair advantage?
If it comes to working on the Linux command line - at the end of the day it is always about knowing the right tool for the right task.
And it is about knowing the tools that are most certainly available on the Linux system you are currently on.
To give you all the tools for your day-to-day work at the Linux command line, I have created “The ShellToolbox”.
This book gives you everything
- from the very basic commands, through
- everything you need for working with files and filesystems,
- managing processes,
- managing users and permissions, through
- software management,
- hardware analyses and
- simple shell-scripting to the tools you need for
- doing simple “networking stuff”.
Everything in one single, easy to read book. With explanations and example calls for illustration.
If you are interested, go to shelltoolbox.com and have a look (as long as it is available).