Globbing

From Christoph's Personal Wiki

In computer programming, the verb glob or globbing is used to refer to an instance of pattern matching behavior. The noun glob is sometimes used to refer to a particular pattern, e.g. "use the glob *.log to match all those log files".

Examples

bash$ ls -l
total 2
-rw-rw-r--    1 bozo  bozo         0 Aug  6 18:42 a.1
-rw-rw-r--    1 bozo  bozo         0 Aug  6 18:42 b.1
-rw-rw-r--    1 bozo  bozo         0 Aug  6 18:42 c.1
-rw-rw-r--    1 bozo  bozo       466 Aug  6 17:48 t2.sh
-rw-rw-r--    1 bozo  bozo       758 Jul 30 09:02 test1.txt

bash$ ls -l t?.sh
-rw-rw-r--    1 bozo  bozo       466 Aug  6 17:48 t2.sh

bash$ ls -l [ab]*
-rw-rw-r--    1 bozo  bozo         0 Aug  6 18:42 a.1
-rw-rw-r--    1 bozo  bozo         0 Aug  6 18:42 b.1

bash$ ls -l [a-c]*
-rw-rw-r--    1 bozo  bozo         0 Aug  6 18:42 a.1
-rw-rw-r--    1 bozo  bozo         0 Aug  6 18:42 b.1
-rw-rw-r--    1 bozo  bozo         0 Aug  6 18:42 c.1

bash$ ls -l [^ab]*
-rw-rw-r--    1 bozo  bozo         0 Aug  6 18:42 c.1
-rw-rw-r--    1 bozo  bozo       466 Aug  6 17:48 t2.sh
-rw-rw-r--    1 bozo  bozo       758 Jul 30 09:02 test1.txt

bash$ ls -l {b*,c*,*est*}
-rw-rw-r--    1 bozo  bozo         0 Aug  6 18:42 b.1
-rw-rw-r--    1 bozo  bozo         0 Aug  6 18:42 c.1
-rw-rw-r--    1 bozo  bozo       758 Jul 30 09:02 test1.txt


bash$ echo *
a.1 b.1 c.1 t2.sh test1.txt

bash$ echo t*
t2.sh test1.txt

Notes

Filename expansion can match dotfiles, but only if the pattern explicitly includes the dot.

  1 ~/[.]bashrc    # Will not expand to ~/.bashrc
  2 ~/?bashrc      # Neither will this.
  3                # Wild cards and metacharacters will not expand to a dot in globbing.
  4 
  5 ~/.[b]ashrc    # Will expand to ~./bashrc
  6 ~/.ba?hrc      # Likewise.
  7 ~/.bashr*      # Likewise.
  8 
  9 # Setting the "dotglob" option turns this off.
 10 
 11 # Thanks, S.C.

External links

Personal tools