Monday, May 18, 2015

How to Use Wildcards in Linux Commands


Wildcard is a character that can be used as a substitute for any character in a search to increase the flexibility and efficiency of searches. Consult the following list for the usage:


* matches zero or more characters
? matches exactly one character
[abcde] matches exactly one character listed in square brackets
[a-e]  matches exactly one character in the range
[!abcde] matches any character that is not listed
[!a-e] matches any character that is not in the given range
{centos,rhel} matches exactly one entire word in the options given

To list all files in current directory which have an .html or a .jpeg extension:
$ ls *.html *.jpeg
To delete all files and folders in current directory which have the string behnam in their name:
$ rm -rf *behnam*
The following command provides data on all files and folders whose names are one, two or three characters in length:
$ file ? ?? ???
Or the following returns the list of all objects in the current directory that have a three-character or four-character extension:
$ ls *.??? *.????
To show all files that have an extension which starts with a, b or c:
$ ls *.[abc]*
And this one returns information about all files and folders whose names begin with any letter from "a" through "e" or begin with "m" or "n" or "o":
$ file [a-emno]* 
To copy all html and pdf files to home directory you can use curly brackets and enter: 
$ cp {*.html,*.pdf} ~
Note: Do not put space after the commas.  

Labels: ,