官术网_书友最值得收藏!

  • Bash Cookbook
  • Ron Brash Ganesh Naik
  • 519字
  • 2021-07-23 19:17:42

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.
主站蜘蛛池模板: 聊城市| 海晏县| 拜城县| 札达县| 陵川县| 林州市| 册亨县| 新巴尔虎左旗| 新河县| 阿拉善左旗| 商洛市| 大名县| 正蓝旗| 庆元县| 康平县| 武川县| 武宁县| 乌拉特中旗| 昌图县| 平利县| 蕲春县| 开原市| 石林| 曲阳县| 平远县| 南乐县| 广平县| 泰安市| 长子县| 察雅县| 寿宁县| 明星| 酒泉市| 遂川县| 丘北县| 太仓市| 永州市| 榆树市| 兴宁市| 礼泉县| 明光市|