Linux: File types in Linux

Linux: File types in Linux

Hey guys, let's dive deeper into Linux with understanding the file types in Linux today. The table below illustrates the different types of files in Linux file system.

image.png

Why is it necessary to understand different file types when they are already color coded as being a directory or as a file like the image below.

image.png

In the above image, the blue colored file is a directory for the uninitiated. But, that depends from client to client. I use git bash and it shows up this way in it. You might use a different ssh client and it might show up differently to you basis the UI of that client.

Hence, it's very important to understand the file types.

To know what's the file type, we just have to use the command file <path of the file>(absolute path-if you're in some other directory) or file <file name>(relative path-if in the same directory).

For more understanding, let's see the following example.

  1. File type by relative path:

image.png

  1. File type by absolute path:

image.png

Both of the commands gave us the same output. It tells us the file type i.e. text.

What if we wanted to check what's the file type of a directory?

image.png

The above image answers the question.

The files that start with 'c' as their first character can be found in the /devdirectory that signifies that these are character files. The easiest way to understand this is by thinking of it as the commands we run of keyboard gets captured as character files.

The files starting with 'b' signifies that they are block files created when we attached block disks to our system.

image.png

We saw almost all relevant file types. Now let's understand link file type in a bit more depth.

We might have a file in a very deep directory structure and we would want to access it more often than not. So it's wise to have shortcut to access this file. Creating softlinks does this in Linux.

Suppose there's a file that I have created that is nested in many directories. It's difficult to access this file from the home directory as its path is very long. Hence, we create its shortcut by means of a softlink command so that we can access the same file from the home directory itself.

[root@centos7 ~]# vim a/a1/a2/a3/sidtest.txt

The above command created a file enclosed within multiple directories.

So, we create a softlink to that file for easier accessibility.

image.png

As can be seen from the above image, sidtestdummy and sidtestdummy.txt are the softlinks highlighted by the blue color.

But if we change the path of the original file, then the softlink will not work and throw errors. Also, the softlink file color will change to red indicating that it's not functional anymore.

Command such as unlink or rm removes the link from the directory.

image.png

See the directory after unlinking the softlink file below.

image.png

Now that we have understood links better than when we started discussing about it, let's end our article by discussing a few more options pertaining to l command.

A simple ls-l would list down the files in alphabetical order like the image below.

image.png

The command ls-lt would sort the files based upon the timestamps showing the latest files first followed by the old once in descending order of their timestamps.

image.png

And, finally, if you want to reverse the sort provided by ls-lt, use the command ls-ltr which will display the latest files at the end of the list.

image.png

One last command for today will be to change the hostname. We achieve this by editing the hostname file in /etc/hostname by using the vim editor, edit the name of the hostname, save and come out of the file.

Post that, we run the command: hostname <new host name>and log out of the machine and log in back to see the new hostname in our prompt.

That's all for this article. I hope you enjoyed this article. I hope I did not bore you awesome people. We'll catch up again tomorrow with some new Linux learning.

Stay tuned!