Wildcards
A wildcard is a character that can be used as a substitute for any class of characters. Wildcards are useful to perform actions on more than one file at a time.
Three types of wildcards are used with Linux commands. Those are
- Star Wildcard
- Question Mark Wildcard
- Square Brackets Wildcard
Star Wildcard ( * )
The character * matches zero or more characters in a file or directory name. Here are the examples.
$ ls a* : It displays all files starting letter ‘a’
$ ls b*t : It displays all files starting letter is ‘b’ and ending letter ‘t’
$ ls *g : It displays all files ending letter ‘g’
$ ls *.txt: It displays all text files (.txt)
$ ls *.html : It displays all html files (.html)
$ ls */* : It displays all files in all directories which are existed in current directory.
Question Mark Wildcard ( ? )
The character “?” can represent any single character. Below are the examples.
$ ls a?c : It displays all three character length files but starting letter is ‘a’ and ending letter is ‘c’.
$ ls b??k : It displays all four character files or directories but starting letter is ‘b’ and ending letter is ‘k’.
Square Brackets Wildcard []
It is used to specify range. It allows you to limit to a subset of characters. Below are the examples.
$ ls [aeiou]* : It displays all files but first character of the filename to listed must be any of the letters given with in the square bracket and remaining can be anything.
ls [!aeiou]* It displays all files whose first character is anything others than letters given in the square bracket.
$ ls [k-v]* : It displays all files whose starting letter is between k an v.
$ ls *[0-9]* : It displays all files whose name contains number from 0 to 9.