Logo

dev-resources.site

for different kinds of informations.

Master Linux File Types While Your Coffee Brews

Published at
1/16/2025
Categories
linux
security
links
sockets
Author
nedtechie
Categories
4 categories in total
linux
open
security
open
links
open
sockets
open
Author
9 person written this
nedtechie
open
Master Linux File Types While Your Coffee Brews

This article is part of my book:

Master Linux Permissions and File Types While Your Coffee Brews


A well-known expression says that everything in Linux is considered a file. In the following diagram, you can find a map that shows the most common file types.

Image description

Common File Types

The three most common file types are:

  • Regular files: A Regular file can contain any data and can be modified, moved, copied, or deleted.
  • Directories: A directory is a special file containing other files or directories, helping to organize the file system.
  • Links: A link is a pointer to another file or directory elsewhere in the same file system.

Less Common File Types

  • Block devices: A block device represents a virtual or physical device, typically a disk or other storage device.
  • Character devices: A character device represents a virtual or physical device, such as terminals or serial ports.
  • Sockets: A socket is a channel for communication between two programs.

Identify File Types

The easiest way to identify a file's type is to use the ls command,
using the long listing format.

Refer to the following table for more information.

File Type Symbol Permissions
Regular File - -rw-------
Directory d drwxr-xr-x
Symbolic Link l lrwxrwxrwx
Block Device b brw-rw----
Character Device c crw-rw----
Socket s srw-rw----

Example of using the stat command to identify the file type.

 stat -c "%n is a %F" /etc/passwd
/etc/passwd is a regular file
Enter fullscreen mode Exit fullscreen mode

Identify Regular Files

Regular files are marked with the - symbol.

This is an example of using ls to identify a regular file, is where the first letter in the output of ls -l represents the file type.

ls -l /etc/passwd
-rw-r--r-- 1 root root 3274 Dec 22 16:13 /etc/passwd
Enter fullscreen mode Exit fullscreen mode

Image description

Identify Directories

Directories are marked with the d letter.

Example of using ls to identify directories.

ls -ld /etc/
drwxr-xr-x 168 root root 12288 Jan  1 15:12 /etc/
Enter fullscreen mode Exit fullscreen mode

Image description

Identify Block Devices

Block devices are marked with the b letter.

This is an example of using ls to identify a block device.

ls -l /dev/nvme0n1
brw-rw---- 1 root disk 259, 0 Jan  3 12:52 /dev/nvme0n1
Enter fullscreen mode Exit fullscreen mode

Image description

Identify Character Devices

Character devices are marked with the c letter.

An example of using ls to identify a character device.

ls -l /dev/tty
crw-rw-rw- 1 root tty 5, 0 Jan  7 18:22 /dev/tty
Enter fullscreen mode Exit fullscreen mode

Image description

Identify Socket Devices

Sockets are marked with the s letter.

An example of using ls to identify a socket device.

ls -l /run/systemd/notify
srwxrwxrwx 1 root root 0 Jan  3 12:52 /run/systemd/notify
Enter fullscreen mode Exit fullscreen mode

Image description

Links

Image description

Links are special types of files and there are two types:

  • Symbolic links: These types of links point to other files or
    directories.

  • Hard links: These types of links point to the same place on the
    disk, known as inode, just as the original file.

Symbolic Links

A symbolic link is a special type of file that points to the path of
another file. It's like a shortcut or alias.

Identify Symbolic Links

Symbolic links are marked with the l letter.

Example of using ls to identify a symbolic link.

ls -l /dev/core
lrwxrwxrwx 1 root root 11 Jan  3 12:52 /dev/core -> /proc/kcore
Enter fullscreen mode Exit fullscreen mode

Image description

Creating Symbolic links

The command used to create a symbolic link is ln but with the -s
option.

Example that demonstrates how to create a symbolic file.

touch target_file.txt
ln -s target_file.txt the_soft_link.txt
Enter fullscreen mode Exit fullscreen mode

To check the results use the command ls -l as follows.

ls -l
-rw-r--r-- 1 root root  0 Nov  6 12:23 target_file.txt
lrwxrwxrwx 1 root root 15 Nov  6 11:54 the_soft_link.txt -> target_file.txt
Enter fullscreen mode Exit fullscreen mode
Notes
  • Symbolic links can point to a file or directory.
  • You can create symbolic links on different partitions.
  • You can create a symbolic link to a non-existent file.
  • Symbolic links are useful when you need to create a link to a file or directory that is located on a different file system.
  • You can identify a symbolic link in the output of ls, where the first character on the permissions for a symbolic link is 'l'.

Access the man page for more info.

man ln
Enter fullscreen mode Exit fullscreen mode

Hard Links

Hard links are pointers to the same inode on the disk. This means that
two different hard links can point to the same data.

  • The TARGET file must exist before creating a hard link.
  • If you do not specify a the_link_name a hard link with the same name as the target_file will be created in the current directory.
  • When the target_file or the_link_name are not in the current directory then use full path also known as absolute paths.

Creating Hard Links

The command used to create hard links is the ln command.

The basic syntax is shown below.

ln target_file the_link_name
Enter fullscreen mode Exit fullscreen mode

In this example we create a new file named myfile and a new link named mylink.

echo "Hi, there." > myfile
ln myfile mylink
Enter fullscreen mode Exit fullscreen mode

Identify Hard Links

Hard links do not have a special symbol that we can use to identify
them. They are regular files. In order to identify hard links we use
ls with the -i option.

In this step, we check the inodes to make sure both files have the same inode.

ls -il myfile
2027339 -rw-r--r-- 2 root root 11 Dec 31 08:29 myfile
ls -il mylink
2027339 -rw-r--r-- 2 root root 11 Dec 31 08:29 mylink
Enter fullscreen mode Exit fullscreen mode

Notice the number 2. That means two files point to the same inode.

Another way to check that both files have the same inodes is using the stat command as follows.

stat -c "%n is a %F with inode %i" mylink
mylink is a regular file with inode 2027339

stat -c "%n is a %F with inode %i" myfile
myfile is a regular file with inode 2027339
Enter fullscreen mode Exit fullscreen mode

Notes on Hard Links

  • Hard links can be deleted using the rm command. However, deleting a hard link does not delete the underlying data as long as other hard links are pointing to it.
  • Hard links can be renamed or moved using the mv command. Since they point to the same inode, they can be relocated freely without affecting the data.
  • There is no risk of "breaking" a hard link when moving it. As long
    as the inode remains accessible on the filesystem, the hard link
    will continue to point to the same data.

  • You can NOT create a hard link to a directory.

  • You can NOT create a hard link to a file that is located on a
    different filesystem.

Access the man page for more info.

man ln
Enter fullscreen mode Exit fullscreen mode
sockets Article's
30 articles in total
Favicon
Master Linux File Types While Your Coffee Brews
Favicon
Why most HTTP servers are multithreaded and how to build one from scratch
Favicon
Talking to a Gmail POP3 server with Python
Favicon
You (probably) do not understand UDP
Favicon
Creating a nextjs chat app for learning to integrate sockets
Favicon
Tipos de Sockets em TCP e UDP: Escolhendo o Caminho Adequado
Favicon
Introdução aos Sockets em Python
Favicon
How to broadcast live data on your application?
Favicon
lets compare network sockets between Perl and Python
Favicon
Communicating with WebRTC or WebSocket
Favicon
A terminal real time application with Dart Sockets
Favicon
Building a Redis client from scratch in Go
Favicon
Socket sharding in Linux example with Go
Favicon
Real-Time Interactive Plotting (using Sockets, Python & Plotly)
Favicon
What are sockets in computer networks?
Favicon
Fun with Sockets!
Favicon
Integrating Sockets in Kotlin
Favicon
Basics of Sockets
Favicon
Simple tweet locator using Python, Flask SocketIO and Tweepy
Favicon
Captive Web Portal for ESP8266 with MicroPython - Part 3
Favicon
Using BSD Sockets in Swift
Favicon
I wrote a PHP Client for sonic, the autosuggestion engine, and now its official PHP client. Ask Me Anything 🤩
Favicon
Mercure (Simple Real-Time Updates)
Favicon
The Gopher Protocol in Brief
Favicon
Socket Programming in C: Introduction
Favicon
Receiving data from ESP8266 sensors
Favicon
Socket Programming in C: Communication Styles
Favicon
Socket Programming in C: What is a Socket?
Favicon
sockjs_test_server_nwjs – SockJS test server on NW.js
Favicon
Simplest technique to develop multi-threaded apps with UI update

Featured ones: