Difference between revisions of "Find and Locate"

From Free Knowledge Base- The DUCK Project: information for everyone
Jump to: navigation, search
m (Find)
m
 
(3 intermediate revisions by one user not shown)
Line 11: Line 11:
 
== Find ==
 
== Find ==
 
Use the find command to search for a file or pattern.
 
Use the find command to search for a file or pattern.
 +
 +
<big><big><big>
 +
$ find ./ -iname '*blah*'
 +
</big></big></big>
  
 
here are examples:
 
here are examples:
Line 28: Line 32:
 
  find / -mtime 30
 
  find / -mtime 30
  
See [https://alvinalexander.com/unix/edu/examples/find.shtml Alvin Alexander's big collection of Unix/Linux ‘find’ command examples]. ''(mirrored in talk section in the event link goes dead)''
+
*See [https://alvinalexander.com/unix/edu/examples/find.shtml Alvin Alexander's big collection of Unix/Linux ‘find’ command examples]. ''(mirrored in talk section in the event link goes dead)''
 +
*The find command is discussed on our age old [[How Do I: A Linux Q&A]] page demonstrating examples using file date ranges and modification time criteria.
  
 
== Locate ==
 
== Locate ==

Latest revision as of 10:01, 29 October 2023

Find and Locate - Searching for files in Linux

  • The find command works without the need to build a metabase
  • The locate command is much faster but depends on a metabase
  • find it typically present on any linux system while locate is part of mlocate / findutils which needs installed
  • find results are real-time, recent changes to the file system is not a problem
  • locate program of findutils scans one or more databases of filenames and displays any matches.
  • locate requires you run 'updatedb' to rebuild the metabase before recent changes on the file system can be searched
  • The find command is sometimes more useful for searching attached storage and network drives w/o the need to create database tables

Find

Use the find command to search for a file or pattern.

$ find ./ -iname '*blah*'

here are examples:

  find ./ -name filename                (locate filename in or below the current directory path)
  find / -name 'filename' 2>/dev/null   (locate filename anywhere up from root filesystem)
  find ./ -iname 'filen*'               (locate all files begin with filename and ignore case)

find a file ignore case

find . -iname filename 

find a directory

find . -type d -name directoryname

find all files with a specific permission

find / -type f ! -perm 777

find all Executable files

find / -perm /a=x

find all files modified within the last 30 days

find / -mtime 30

Locate

At first it might seem the 'find' command is a better choice, however, although it is more universal, the 'locate' command is better suited for typical searches as well as complex search operations. The locate utility works better and faster because instead of searching the file system when a file search is initiated it will look through a prebuilt database that was created by updatedb.

  • GNU findutils’ is documented in locatedb(5), and is pretty much just a list of files (with a specific compression algorithm);
  • mlocate’s is documented in mlocate.db(5), and can also be considered a list of directories and files (with metadata).

To locate a file on the filesystem

locate filename

You can limit your search returns to a required number to avoid redundancy with your search results using the -n command.

locate -n10 filename

display the count of all matching entries

locate -c filename

or with wildcards

locate -c filename*

case insensitive and suppress error messages,

locate -iq filename*

information about the locate metabase include last update

locate -s