Operations 6 min read

How to Locate Duplicate Files on Linux Using Inodes and Simple Scripts

This guide explains how Linux inodes identify identical files, demonstrates using ls -i, sort, and find -samefile commands, and provides a Bash script to automatically list hard‑linked duplicates, helping you reclaim disk space efficiently.

Liangxu Linux
Liangxu Linux
Liangxu Linux
How to Locate Duplicate Files on Linux Using Inodes and Simple Scripts

When a computer stores many files, duplicate copies can waste disk space and slow down the system. On Linux, files that share the same inode value are essentially the same file, even if they have different names or locations.

Understanding inodes and hard links

An inode is a data structure that records all metadata of a file except its name and content. If two or more files have identical inode numbers, they are hard links to the same underlying data. Soft links (symbolic links) have their own inode and merely point to a target file.

Listing files with inode numbers

Use ls -i to display inode numbers, then sort them numerically to spot duplicates:

$ ls -i | sort -n | more
...
788000 myfile    <==
788000 mytwin    <==
801865 Name_Labels.pdf
...

The first column shows the inode; identical numbers indicate duplicate files.

Finding hard‑linked copies of a specific file

The find command with the -samefile option quickly lists all hard‑linked copies of a given file:

$ find . -samefile myfile
./myfile
./save/mycopy
./mytwin

These paths all share the same inode, confirming they are duplicates.

Bash script to automatically detect duplicate inodes

#!/bin/bash
# searches for files sharing inodes
prev=""
# list files by inode
ls -i | sort -n > /tmp/$0
while read line; do
    inode=$(echo $line | awk '{print $1}')
    if [ "$inode" == "$prev" ]; then
        grep $inode /tmp/$0
    fi
    prev=$inode
done < /tmp/$0
# clean up
rm /tmp/$0

Running the script prints inode numbers followed by the filenames that share them, e.g.:

$ ./findHardLinks
788000 myfile
788000 mytwin

Searching the entire filesystem by inode

You can also locate every file with a given inode using find -inum and suppress permission errors by redirecting them to /dev/null:

$ find / -inum 788000 -ls 2> /dev/null
788000 4 -rw-r--r-- 4 liangxu liangxu 228 Apr 12 19:37 /tmp/mycopy
788000 4 -rw-r--r-- 4 liangxu liangxu 228 Apr 12 19:37 /home/liangxu/myfile
...

This approach helps you identify and remove redundant files, freeing up valuable disk space.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Linuxinodehard linksduplicate filesfind command
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.