Globbing in Linux
Do not miss this exclusive book on Binary Tree Problems. Get it now for free.
Globbing involves bash interpreting glob characters to perform filename expansion of pattern searching in Linux. In this article, we will learn about globbing through various examples.
Table of contents.
- Introduction.
- Globbing patterns.
- Summary.
- References.
Introduction.
Globbing is the process whereby bash interprets glob characters and performs file expansion or pattern matching.
When the shell finds a glob, it expands the pathname and extends the glob with matching file names.
Globbing patterns.
1. asterisks (*).
Globbing is used with various commands to perform actions of a group of files once, for example, we can concatenate, list, remove, archive, etc using globbing.
For example, to list all files with .txt extension, we write:
$ ls *.txt
To remove them using the rm command we write:
$ rm *.txt
In the above examples, we use the * - globstar character to match any string before the .txt.
What if we know the first character of a file name, we can find the file by writing:
$ find f*.txt
The above command looks for all files starting with f followed by any characters between, and has a file extension .txt in the current directory.
2. Question mark (?).
We can also match a single character by using the ? - question mark glob as follows:
$ ls file?.txt
The above example assumes we have a certain naming convention, that is, we name our files, file1, file2, file3 ... and so on with some common file extension. In this case, ? matches all files using a single character that comes after file. This is not limited to digits, it could also be characters, however, note that the file extension must be .txt.
We can also further use this glob to match files with a specific number of characters, for example:
$ rm ????.txt
Here we remove all files with four characters that end with the .txt file extension.
We can also use the question mark glob to match a file extension. For example, to match all files with an extension with four characters such as .docx or .java, etc, we write:
$ cat test.????
In Linux, hidden files are preceded with a . character in their file names, to list all hidden files we write:
$ ls .*
Here we match all strings after the . dot character.
3. Square brackets.
We can also specify ranges, for example, to copy all files that have a digit in their file names, we write:
$ cp *[0-9]* [destination]
Here all files with a number between 0 and 9 in their filename are copied to the destination directory we specify, we can also limit this range, for example, to copy files with digits between 1 and 4 we use the range: [1-4].
Ranges are not limited to digits, we can also use characters, with characters we can specify upper or lower cases. We can also combine characters and digits.
For example to remove all files with digits between 1 and 4 and lowercase characters between f and j and upper case characters between A and C we write:
$ rm *[A-Cf-j1-4]*
We also use LC_CTYPE locale interpretations, for example, to list all files with digits, we write:
$ ls *[[:digit:]]*
To list all files with an upper case character in their file names, we write:
$ ls [[:upper:]]*
Other interpretations include;
- [[:alnum:]] - alphabets, digits, lower and upper cases included.
- [[:alpha:]] - only alphabets, lower and upper cases included.
- [[:punct:]] - punctuation characters, e.g, #, $, %, *, (, ), ^ etc.
- [[:lower:]] - lower cases only considered.
- [[:space:]] - space characters considered.
4. Caret(^).
We use this glob to match starting characters in a string. For example to list files starting with a specific uppercase character between A to F we write:
$ ls | grep '[^A-F]'
We can also use it with square brackets. When used outside of the square brackets, it matches files starting with a given range of characters. For example to display all strings in a file starting with characters between A and C we write:
$ grep '^[M-V]' File.txt
When used inside square brackets, it matches strings in the file with the specified character within the specifed range. For example, let's list all lines with characters between A and C
$ grep '[^A-C]' [file name]
5. Exclamation(!).
The exclamation glob performs a similar function as the carret glob. That is, we can do what we previously did above with an exclamation.
$ grep '[!A-C]' [file name]
6. Dollar sign($).
The dollar sign glob is the converse of the exclamation and carret globs. That is, it matches the ending character. For example, to display all lines ending with a specific character we use the grep command as follows:
$ grep E$ File.txt
In the example above we display all lines in the file File.txt ending with a letter E.
another example:
$ grep 'ONG TEXT
Sign up for FREE 3 months of Amazon Music. YOU MUST NOT MISS.