locate Command in Linux

Learn via video courses
Topics Covered

Build an AI-First Career, Master the Complete Skillset

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program
NSDC Certified

AI Forward Deployed Engineer Program

Full-stack engineering, production AI and client-facing consulting

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program

Overview

The locate command in Linux is a powerful utility that allows users to search for files and directories based on their names. It is much faster than manually searching for files using the find command as it searches an index database instead of the file system.

Linux locate Command Syntax

The syntax for the locate command is as follows:

Where:

  • locate: The name of the command to be used in the terminal.
  • options: Various options and flags that can be used with the locate command.
  • pattern: The search pattern that is used to match against file and directory names.

locate Command Options:

  • -i, --ignore-case: Ignore case distinctions when matching patterns.
  • -w, --wholename: Match only the whole path name (default).
  • -r, --regexp: Interpret the pattern as a regular expression.
  • -c, --count: Instead of printing file names, print the number of matching entries.
Sharpen Your Fundamentals with Free Learning

Example Usages

  • Find a file named example.txt in the system.:

    Output:

    Explanation: The locate command searches for all files and directories that match the pattern example.txt and displays their full paths. In this example, it found two files matching the pattern, one in the user's home directory and one in the system directory.

  • Search for a file named example.txt case-insensitively.:

    Output:

    Explanation: The -i option tells locate to ignore case when searching for files. In this example, it found a file in the user's home directory with the name EXAMPLE.txt, which matches the pattern example.txt case-insensitively.

How Scaler Transformed Careers in Different Fields

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Tips

  • The locate command uses an index database to search for files, so it may not find files that have been recently created or modified. To update the database, use the updatedb command.

  • Be careful when using the locate command with sensitive information, as it can display all files that match the search pattern, including hidden files and files that the user may not have permission to access.

Advanced Use Cases of locate Command in Linux

  • Find all files and directories containing the word example in their name.:

    Output:

    Explanation: The search pattern *example* matches all files and directories that contain the word example anywhere in their name. The * characters are used as wildcards to match any number of characters before or after the word example.

  • Search for all files in the system that have been modified in the last 24 hours.:

    Output:

    Explanation: The date command generates a string representing the date and time 24 hours ago, which is used as a pattern to search for files modified in the last 24 hours. The -c option tells locate to print the count of matching files instead of their names. In this example, it found 37 files that were modified in the last 24 hours.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Conclusion

  • The locate command is a fast and efficient way to search for files and directories in Linux.

  • It uses an index database to find matches based on file and directory names.

  • The updatedb command can be used to update the index database if needed.