Find and Locate

From Free Knowledge Base- The DUCK Project: information for everyone
Jump to: navigation, search

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