Searching for files by name and/or extension

When we have large number of files available for viewing, sometimes we need to find a file among many without using the GUI searching tools or provide a better set of granular filters to reduce returned results. To search on the command line, there are a few facilities/commands we can use:

  • locate (also a sibling of the updatedb command): Used to find files more efficiently using an index of files
  • find: Used to find files with specific attributes, extensions, and even names within a specific directory

The find command is far more suitable for the command line and widespread (often being on embedded devices), but the locate command is a common facility for use on desktops, laptops, and servers. Locate is far more simpler and involves recursively indexing all of the files it is configured to keep track of and it can generate very quick file listings. The file index can be updated using the following command:

$ sudo updatedb
Updating the database for the first time or after large amounts of files have been created, moved, or copied may result in longer than average times to update the database. One particular mechanism to keep the database frequently up to date automatically is through the use of cron scheduler. More about this topic will be covered later.

The locate command can also be used to test for the existence of a file before reporting its location (the database may be out of date), and also limit the number of entries returned.

As noted, find does not have a fancy database, but it does have a number of user configurable flags, which can be passed to it at the time of execution. Some of the most commonly used flags used with the find command are as follows:

  • -type: This is used to specify the type of file, which can be either file or directory
  • -delete: This is used to delete files, but may not be present, which means that exec will be required
  • -name: This is used to specify searching by name functionality
  • -exec: This is used to specify what else to do upon match
  • -{a,c,m}time: This is used to search for things such as time of access, creation, and modification
  • -d, -depth: This is used to specify the depth searching may delve recursively into
  • -maxdepth: This is used to specify the maximum depth per recursion
  • -mindepth: This is used to specify the minimum depth when recursively searching
  • -L, -H, -P: In order, -L follow symbolic links, -H does not follow symbolic links except in specific cases, and -P never follows symbolic links
  • -print, -print0: These commands are used to print the name of the current file on a standard output
  • !, -not: This is used to specify logical operations such as match everything, but not on this criteria
  • -i: This is used to specify user interaction on a match such as -iname test
Please be aware that your platform may not support all of GNU find's features. This may be the case with limited shells for embedding, resource constraints, or security reasons.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.184.42