- 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
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
- Objective-C Memory Management Essentials
- INSTANT Weka How-to
- Java設(shè)計(jì)模式及實(shí)踐
- 網(wǎng)絡(luò)爬蟲原理與實(shí)踐:基于C#語言
- Redis Essentials
- Responsive Web Design by Example
- 劍指Java:核心原理與應(yīng)用實(shí)踐
- 跟戴銘學(xué)iOS編程:理順核心知識(shí)點(diǎn)
- Penetration Testing with the Bash shell
- C語言從入門到精通
- Java程序設(shè)計(jì)
- Learning TypeScript
- Extending Docker
- Python機(jī)器學(xué)習(xí)
- 前端Serverless:面向全棧的無服務(wù)器架構(gòu)實(shí)戰(zhàn)