DEV Community

Cover image for Master Linux File Types While Your Coffee Brews
Nedim Hadzimahmutovic
Nedim Hadzimahmutovic

Posted on • Edited on • Originally published at blog.nedtechie.com

4

Master Linux File Types While Your Coffee Brews

This is the first article from a series compiled from my notes while taking the LPI exams.

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 pointing to another file's path. 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. 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.
  • The mv command can rename or move hard links. 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

This article is part of my book:

Master Linux Permissions and File Types While Your Coffee Brews


Top comments (0)