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)