Master Linux File Types While Your Coffee Brews

This article is part of my book:

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.

Common File Types

The three most c…


This content originally appeared on DEV Community and was authored by Nedim Hadzimahmutovic

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

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

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/

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

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

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

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

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

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
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

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

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

echo "Hi, there." > myfile
ln myfile mylink

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

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

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


This content originally appeared on DEV Community and was authored by Nedim Hadzimahmutovic


Print Share Comment Cite Upload Translate Updates
APA

Nedim Hadzimahmutovic | Sciencx (2025-01-16T07:42:16+00:00) Master Linux File Types While Your Coffee Brews. Retrieved from https://www.scien.cx/2025/01/16/master-linux-file-types-while-your-coffee-brews/

MLA
" » Master Linux File Types While Your Coffee Brews." Nedim Hadzimahmutovic | Sciencx - Thursday January 16, 2025, https://www.scien.cx/2025/01/16/master-linux-file-types-while-your-coffee-brews/
HARVARD
Nedim Hadzimahmutovic | Sciencx Thursday January 16, 2025 » Master Linux File Types While Your Coffee Brews., viewed ,<https://www.scien.cx/2025/01/16/master-linux-file-types-while-your-coffee-brews/>
VANCOUVER
Nedim Hadzimahmutovic | Sciencx - » Master Linux File Types While Your Coffee Brews. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/01/16/master-linux-file-types-while-your-coffee-brews/
CHICAGO
" » Master Linux File Types While Your Coffee Brews." Nedim Hadzimahmutovic | Sciencx - Accessed . https://www.scien.cx/2025/01/16/master-linux-file-types-while-your-coffee-brews/
IEEE
" » Master Linux File Types While Your Coffee Brews." Nedim Hadzimahmutovic | Sciencx [Online]. Available: https://www.scien.cx/2025/01/16/master-linux-file-types-while-your-coffee-brews/. [Accessed: ]
rf:citation
» Master Linux File Types While Your Coffee Brews | Nedim Hadzimahmutovic | Sciencx | https://www.scien.cx/2025/01/16/master-linux-file-types-while-your-coffee-brews/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.