Get the App
SLTechnology News&Howtos  ›  Development  › 

How to find a file vaguely by file name by Linux

Shulou Source: shulou.com Published: 2022-06-01 21:54:59 09月27日 Update

This article mainly explains the "Linux how to find files according to the file name fuzzy", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Linux how to fuzzy search files according to the file name" bar!

Fnmatch int fnmatch (const char * pattern, const char * string, int flags); the parameter indicates that FNM_NOESCAPE, if this flag is set, handles the backslash as normal characters rather than escape characters. FNM_PATHNAME if this flag is set, the slash in string only matches the slash in pattern, it does not match the asterisk (*) or question mark (?) Metacharacters also cannot match a bracket expression ([]) that contains a slash. FNM_PERIOD if this flag is set, the starting point in string must match the period in pattern. A period is considered the starting point, if it is the first character of string, or if FNM_PATHNAME is also set, the period immediately following the slash. FNM_FILE_NAME this is a synonym for FNM_PATHNAME GNU. FNM_LEADING_DIR if this flag (GNU extension) is set, the pattern must match the initial fragment of string that follows the slash. This flag is mainly for internal use by glibc and is only implemented under certain conditions. FNM_CASEFOLD if this flag (GNU extension) is set, pattern matching ignores case.

The return value 0:string matches pattern; FNM_NOMATCH: no match, or other non-zero values, if an error occurs.

Example code is as follows:

# include # include using namespace std; vectorWildcardSearch (const char* pattern,const char* filePath) {vectorres; struct dirent * entry; DIR * dir; int ret; dir = opendir (filePath); if (dir! = NULL) {while ((entry = readdir (dir))! = NULL) {ret = fnmatch (pattern,entry- > dumbname.FNMASPATHNAME | FNM_PERIOD) If (ret = = 0) {res.push_back (entry- > d_name);} else {continue;} closedir (dir); return res } int main (int argc,char*argv []) {vectortmp; char* pattern = argv [1]; char* path = argv [2]; tmp = WildcardSearch (pattern,path); for (int iTun0) I for example:. / test "2018-*.bmp". / path thank you for reading, the above is the content of "Linux how to fuzzy find files according to the file name", after the study of this article, I believe you have a deeper understanding of how Linux fuzzy search files according to the file name, the specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Files slashes flags file names characters learning content patterns examples start normal code metacharacters parameters synonyms and at the same time size that is ideas situations Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS MySQL OPPO Reno Linux Microsoft