Matching file names using path name expansions
Globbing: Path name Expansion
Bash shell has a capability of path name matching, which is known as Globbing. To understand globbing we need to know about the “wildcard”.
It’s nothing but a set of building blocks that allows to create a pattern which define a set of files or directories. There are 3 basic set of wildcard –
* – It represents any string of zero or more characters
? – It represents a single character
[ ] – It represents a range of characters
Pattern Matching –
Globbing is an operation that expands a wildcard pattern into the list of path-names matching the pattern. Bash itself cannot recognize Regular Expressions. Command-line meta-characters are replaced by the match list prior to execution any command. So, Let starts with the common meta characters and pattern classes along with the demonstration.
A simple set of files and directories are useful to demonstrate expansion.
[[email protected] ~]# mkdir pattern
[[email protected] ~]# mkdir pattern
[[email protected] ~]# cd pattern
[[email protected] pattern]# touch arch backtrack centos debian elive fedora amigo beatrIX conectiva deli floppix
[[email protected] pattern]# ls amigo backtrack centos debian elive floppix arch beatrIX conectiva deli fedora
[[email protected] pattern]#
First, make pattern matching by using *
[[email protected] pattern]# ls a*
amigo arch
[[email protected] pattern]# ls i
amigo conectiva debian deli elive floppix
Pattern matching by using ?
[[email protected] pattern]# ls ??????
centos debian fedora
[[email protected] pattern]# ls a???
arch
[[email protected] pattern]# ls ?e????
centos debian fedora
[email protected] pattern]# ls *i?
floppix
Pattern Matching by using [] –
[[email protected] pattern]# ls [ab]*
amigo arch backtrack beatrIX
[[email protected] pattern]# ls [d]*
debian deli
[[email protected] pattern]# ls *[^ao]
arch backtrack beatrIX centos debian deli elive floppix
caret ( ^ ) means look for character which is not contains the characters which are mentioned inside the brackets. In above example all the name which contains “a” or “o” at the end are not listed.
Brace expansion –
It is used to generate discretionary strings of characters. Brace expansions can also be nested.
[[email protected] ~]# mkdir expansion; cd expansion
[[email protected] expansion]# touch {c,php,html,ruby,fortan,assembly,javascripts}.txt
[[email protected] expansion]# ls
assembly.txt c.txt fortan.txt html.txt javascripts.txt php.txt ruby.txt
[[email protected] expansion]# touch file{A..F}.txt; ls
fileA.txt fileB.txt fileC.txt fileD.txt fileE.txt fileF.txt
[[email protected] expansion]# touch file{a..d}{1,2}.txt; ls
filea1.txt fileb1.txt filec1.txt filed1.txt
filea2.txt fileb2.txt filec2.txt filed2.txt
[[email protected] expansion]# touch file{a{1,3,8},b{2,3},c}.txt; ls
filea1.txt filea3.txt filea8.txt fileb2.txt fileb3.txt filec.txt
Recent Comments