File Commands :- Wildcards (LINUX)

wildcard is a symbol used to replace or represent one or more characters. They are used  to find the file and directory names which increases the flexibility and  efficiency of searches.

  1. *   :-  Asterisk symbol is used to represent any character(s).
  2. ?  :-  Question mark represents any single character.
  3. []  :-  Square brackets represents a range of characters.

How to use :-
* (Asterisk) Wildcard :-

-> $  ls .txt   :- This would list all the files having .txt extension .
-> $  ls *mom  :- This would list all the files that have the letters “mom” anywhere in their filenames.
-> $  ls a*  :- This would list all the files starting with letter a.
-> $  ls *z  :-
This would list all the files ending with letter z.

? (Question Mark) Wildcard

-> $ ls file?  :- This would list all the files that begin with “file” and have one more valid character.(file?.txt for files having extension as txt)
-> $ ls  ?i*  :-  This would list all the files whose second letter is i .
-> $ ls *.???  :- This would list all the files having three letter extension.

[] (Square Brackets) Wildcard

-> $ ls [fg]  :- This would list every file whose name either begins with a f or g.
-> $ ls [p-s]  :– This would list all files that start with “p,q,r or s”.
-> $ ls a[p-s]  :- This would list all files start with the letter “a”  and second letter contains “p,q,r or s”.
-> $ ls [a-c,A-C] :- This would list all files start with the letter “a,b,c,A,B,C”. The [ , ] imply that this entire range indicates ONLY ONE letter which can be from any of the two given sub-ranges.
-> $ ls .[tx]  :- This would list all the files that have extension that begins with t or x.
-> $ ls [!a-c]  :- This would list all files except those that start with “a, b or c”.

Leave a comment