Commands.page Logo

Check Number of Hard Links to File in Ubuntu

This guide explains how to identify the number of hard links associated with a specific file in Ubuntu. Understanding hard links is essential for effective file system management and storage optimization. You will learn the primary command used to display this information and how to interpret the output quickly.

Using the ls Command

The most common way to view the number of hard links pointing to a file is by using the ls command with the long-listing format flag. Open your terminal and type the following command, replacing filename with the actual name of your file:

ls -l filename

Interpreting the Output

The command returns a detailed list of file attributes. The second column in the output displays the hard link count. For example, if you see the following output:

-rw-r--r-- 3 user user 4096 Oct 10 10:00 filename

The number 3 indicates that there are three hard links pointing to this file inode. The first column shows permissions, while the columns following the link count represent the owner, group, size, date, and name.

Using the stat Command

For more detailed information, you can use the stat command. This tool provides a comprehensive view of the file status, including the link count explicitly labeled. Run the following command:

stat filename

Look for the line labeled Links in the output. This number represents the total count of hard links associated with the file. Both methods are effective, but ls -l is generally faster for quick checks during routine administration tasks.