File globbing in Linux in C++

C++Server Side ProgrammingProgramming

p>File globbing also known as Path Name Expansion. It is the method of recognizing wildcard patterns in linux and  then finding the file path expansion based on these patterns.


Wildcard Patterns are strings that are used for selection of multiple files based on patterns.

The character patterns like “?” , “[ ]” , “*” are used for pattern matching and multiselection of the files.

Example of wildcard characters using in file globbing:

  • Asterisk (*) : the * pattern is used when we need to match 0 or more character after the string in the filename.

For example: file* will match all files with name file, files, file2, or with anything after file.

  • Question Mark (?): The ? pattern is used when we need to match exactly one character after the string in the filename.

For example: file* will match filename file1, but not file01 or file.

  • Square Brackets ([]): The [] pattern is used when we need to match a specific number of characters after the string in the filename. It square brackets you can mention number as well as range for matching.

For example: file[2] will match all filenames with exactly 2 more characters at the end like file01 but not file1 or file211.
          File[1- 3] will match all filenames with 1, 2 or 3 more characters at the end. Like file1, file01, file211 but not more than that.

One more additional thing that can be inside the brackets is the negation of a specific count, it is done using ! or exclamation mark, this excludes the count instead of the specific number of characters.

For example: file[!2] will match all file names which have extra characters elser than 2. Like file1, file211 but not file01.

Here is how to implement these in linux commands −

We have created some files in a folder −

file , file1, file01, file211, files.

Then used all globbing patterns on it. Here is the result −

raja
Published on 22-Jan-2021 13:38:43
Advertisements