DEV Community

Cover image for Mastering File Search & Links in Linux: find, locate, and Links Demystified
SAHIL
SAHIL

Posted on

Mastering File Search & Links in Linux: find, locate, and Links Demystified

🧠 Mastering File Search & Links in Linux: find, locate, and Links Demystified

Linux offers robust tools for handling filesystems — from locating files like a pro to understanding the very structure of how files are tracked. In this post, we’ll explore the powerful find and locate commands, followed by an essential look into hard and soft links. But before we jump in — we need to grasp what makes Linux files tick: Inodes.


📦 What Are Inodes in Linux?

In Linux, every file or directory is represented by an inode — short for index node. Think of it as a metadata blueprint of the file.

An inode doesn’t store the filename, but it does store everything else:

  • File type (e.g., file, directory, symlink)
  • Permissions (read/write/execute)
  • Owner and group
  • File size
  • Timestamps (created, modified, accessed)
  • Pointers to actual data blocks on disk

Every filename in a directory is simply a link to an inode. That’s the magic behind hard and soft links, which we’ll cover soon.

🔎 View a file’s inode number:

ls -i filename
Enter fullscreen mode Exit fullscreen mode

🔍 find: The Real-Time File Search Powerhouse

The find command performs recursive, real-time directory scans to locate files or directories.

📘 Syntax:

find [path] [options] [expression]
Enter fullscreen mode Exit fullscreen mode

⚡ Examples You’ll Use Often:

  • 🔎 Find a file by name:
  find /home -name "myfile.txt"
Enter fullscreen mode Exit fullscreen mode
  • 📁 Find files with a .log extension:
  find . -type f -name "*.log"
Enter fullscreen mode Exit fullscreen mode
  • ⏱️ Find files modified in the last 7 days:
  find . -mtime -7
Enter fullscreen mode Exit fullscreen mode
  • 💾 Find files larger than 100MB:
  find . -size +100M
Enter fullscreen mode Exit fullscreen mode
  • 💥 Execute a command on each result:
  find . -name "*.log" -exec rm {} \;
Enter fullscreen mode Exit fullscreen mode

🧠 Pro Tip: find offers unmatched flexibility but can be slower due to real-time scanning.


🚀 locate: Lightning-Fast File Search

The locate command searches a prebuilt database (/var/lib/mlocate/mlocate.db) to instantly return matching files.

✅ Example:

locate myfile.txt
Enter fullscreen mode Exit fullscreen mode

To refresh the database (recommended if you just added/deleted files):

sudo updatedb
Enter fullscreen mode Exit fullscreen mode

⚠️ locate is blazing fast, but may miss recent changes unless the database is updated.


🧮 Quick Comparison Table: find vs locate

Command Option What It Does
find -name "pattern" Search by file name
find -type f/d Filter by file or directory
find -mtime -N Find by modified time
find -size +100M Find by file size
find -exec cmd {} \; Run command per result
locate filename Instant match from DB
updatedb (no option) Refresh locate database

🔗 Linux Links: Hard vs Soft, Explained

Linux lets you create multiple references (links) to the same file — either through inodes (hard links) or paths (soft links).


📌 Hard Links

  • Directly link to the same inode as the original file.
  • Can’t link directories.
  • Deleting the original file? No worries — content still exists through other hard links.

🛠 Create a Hard Link:

ln original.txt hardlink.txt
Enter fullscreen mode Exit fullscreen mode

🧾 Check inode numbers:

ls -i original.txt hardlink.txt
Enter fullscreen mode Exit fullscreen mode

👀 Example Output:

sahil@sahilHP-dv:~$ ls -i hardlink abc
30873279 abc  30873279 hardlink
Enter fullscreen mode Exit fullscreen mode

➡️ Both share the same inode = same content, multiple entry points.

You’ll also see a link count after file permissions. If it’s 1, it means no other hard links exist (besides the original). A number like 2 or more? That means multiple hard links exist.

sahil@sahilHP-dv:~$ ls -la hardlink 
-rw-rw-r-- 2 sahil sahil 0 Jul  9 13:56 hardlink
Enter fullscreen mode Exit fullscreen mode

📎 Soft (Symbolic) Links

  • Point to the file path, not the inode.
  • Can link to files or directories.
  • If the original file is deleted, the link breaks.

🛠 Create a Soft Link:

ln -s /path/to/original.txt symlink.txt
Enter fullscreen mode Exit fullscreen mode

🧭 Use ls -l to view links — look for the arrow ():

sahil@sahilHP-dv:~$ ls -al softlink monitor_only.sh 
-rwxrwxr-x 1 sahil sahil 63 Jul  3 21:27 monitor_only.sh
lrwxrwxrwx 1 sahil sahil 15 Jul  9 14:02 softlink -> monitor_only.sh
Enter fullscreen mode Exit fullscreen mode

🧾 Check the inodes — they’ll be different:

sahil@sahilHP-dv:~$ ls -i softlink monitor_only.sh 
30857352 monitor_only.sh  30873620 softlink 
Enter fullscreen mode Exit fullscreen mode

✅ Quick Comparison: Hard Link vs Soft Link

Feature Hard Link Soft Link
Points to Inode File path
Cross-filesystem support
Breaks if original deleted?
Can link directories? ✅ (not always recommended)
Inode Same Different

HardLink vs Softlink

source: linuxhandbook.com


That’s a solid foundation on how Linux manages file lookup and links.

🛠 Whether you’re scripting automation, managing servers, or just exploring, mastering these concepts will save you time, frustration, and help you build smarter tools.

💬 Found this helpful? Drop a comment, share it forward, or bookmark it for future Linux adventures.

Top comments (0)