Saturday, April 11, 2009

Wildcard characters in linux

In linux there is wildcard characters *, ? and pair of bracket [] by which we can filter the output. Below is the usage of the wildcard characters in linux along with their examples.

1)* : * shows the matches any string or group of characters.
Example:

# ls
echo.sh quote.sh read.sh test_echo.bash test_echo.sh test_user.txt test_v.sh

test* shows the string that starts with test.

# ls test*
test_echo.bash test_echo.sh test_user.txt test_v.sh

t*er* shows string that starts with character t followed by any character or null, then er and then any strings.

# ls t*er*
test_user.txt

2)? : ? shows the matches of any single character. Note that instead of ? just only a any single character can be replaceable. For example if you write arju? then total string will be of length 5 like arjua , arjub etc. Below is an example,

# ls
arju arjua arjub

# ls arju?
arjua arjub

3)[] : The character within third bracket ([]) indicates any single character within the bracket to be matched. Suppose, if you write arju[ad] then only it will match the strings start with arju and then end with either a or d. Note that it to be matched with any single character within bracket but not both. Below is an example.

# ls
arju arjua arjuad arjub arjud

# ls arju[ad]
arjua arjud

Within bracket you can also include minus (-) sign between two characters to define a range. Below is an example.
# ls
arju bony richard robert zaman

# ls ?[o-z]*
arju bony robert

Here with [o-z] we define a range which means character start with o and ends with z, so o,p,q,r,s.....z falls within this range. We want 2nd character in the example to be fall under the range of [o-z] and thus filtered output of ls.

We can revert the search range of the characters between bracket by using ! or ^ sign just after starting left bracket.
For example if we use ls ?[!o-z]* or ls ?[^o-z]* then it will behave reverse of filter [o-z].
Example:

# ls
arju bony richard robert zaman

# ls ?[!o-z]*
richard zaman

# ls ?[^o-z]*
richard zaman

In both cases if the 2nd character inside string does not fall within range (o-z) (o,p,q,r,....y,z) then that characters will be displayed.
Related Documents
http://arjudba.blogspot.com/2009/04/rules-for-naming-variable-name-in-unix.html
http://arjudba.blogspot.com/2009/04/arithmatic-operation-and-expression-in.html

No comments:

Post a Comment