From LQWiki
Jump to navigation Jump to search

A Glob is a pattern used by shells to match filenames, for example *.jpg to match all JPEG files.

The main glob metacharacters are, * (match zero or more characters) and ? (match one character). One can also use groups, [chars] to match one of the given characters (for example *.[cs] to match all files ending in .c or .s).

Globs have to match the entire file name, one can not use jpg to match tux.jpg (as one would in a grep regex), and dotfiles are typically not included even though they match. For example, *screensaver* will match xscreensaver-4.22.tar.gz, but not .xscreensaver (since this file won't show up on ls (unless -a is used), and because it's a configuration file which probably should not be deleted, especially accidentally).


#upload PIC0001.jpg, PIC0002.jpg, etc to a host
scp PIC????.jpg 
#delete all realmedia-files
rm *.rm *.ra
#list KDE and X applications (amongst other things) in /usr/bin
ls /usr/bin/[kx]*


  • When a glob matches nothing, the default in Bash is to not expand it. The bash option 'nullglob' can be set to instead expand it to the empty set. Since most applications perform entirely different functions when invoked without arguments, this is of limited use. (with no .o-files (object files), ls *.o would list the current directory instead of nothing, rm *.o would give usage information, and cat *.o would start reading stdin).
  • Newer shells usually support extended glob capabilities. Bash, if extglob is enabled (shopt -s extglob), allows constructs like rm !(*.c|*.h) to delete all files except .c and .h. Zsh supports recursive globs, where **/*.avi matches all .avi files in this directory and all its subdirectories. These types of globs are not likely to work on other shells.
  • To match dot-files in Bash, enable the dotglob option.