Wildcards

Wildcards
Wildcards are extremely useful when you are dealing with files and directories.  The process of matching expressions containing wildcards to file names is wildcard expansion or globbing.

Lesson 4 | Lesson 6

 

Wildcard Definition
* Matches any number of characters
? Matches one character only
[abcd] Match any one character listed
[a-g] Match any one character in the list
[!dfge] Match any character not listed
[!q-z] Match any one character not in the list
{truck,car,plane} Match one word in the list




Create a test directory, move into the test directory and then execute this command.
touch {tree,rock,dirt}_{asia,us,ca}_{fall,winter,spring,summer}

Examples:
List all files with the _fall ending.

ls *_fall
dirt_asia_fall  dirt_us_fall    rock_ca_fall  tree_asia_fall  tree_us_fall
dirt_ca_fall    rock_asia_fall  rock_us_fall  tree_ca_fall

List all files that start with r or t.

ls [rt]*
rock_asia_fall    rock_ca_summer  tree_asia_fall    tree_ca_summer
rock_asia_spring  rock_ca_winter  tree_asia_spring  tree_ca_winter
rock_asia_summer  rock_us_fall    tree_asia_summer  tree_us_fall
rock_asia_winter  rock_us_spring  tree_asia_winter  tree_us_spring
rock_ca_fall      rock_us_summer  tree_ca_fall      tree_us_summer
rock_ca_spring    rock_us_winter  tree_ca_spring    tree_us_winter

List all files that do not start with r or t.

ls [!rt]*
dirt_asia_fall    dirt_asia_winter  dirt_ca_summer  dirt_us_spring
dirt_asia_spring  dirt_ca_fall      dirt_ca_winter  dirt_us_summer
dirt_asia_summer  dirt_ca_spring    dirt_us_fall    dirt_us_winter

List all files that start with dirt followed by one letter and then have the text string ca followed by any amount of letters.

ls dirt?ca*
dirt_ca_fall  dirt_ca_spring  dirt_ca_summer  dirt_ca_winter

 


Copyright CyberMontana Inc. and BeginLinux.com
All rights reserved. Cannot be reproduced without written permission. Box 1262 Trout Creek, MT 59874