Don't understand german? Read or subscribe to my english-only feed.

mika’s advent calendar – day 24: zsh globbing

The advent calendar started with a Zsh tip, let’s end the advent calendar session with a Zsh tip: globbing.

Globbing is like pattern matching. When running ‘ls *.txt’ you’re using globbing. But the globbing feature inside Zsh is much more powerful. Quoting the H-Glob function (“help globbing”) of grml’s Zsh configuration:

% H-Glob

    /      directories
    .      plain files
    @      symbolic links
    =      sockets
    p      named pipes (FIFOs)
    *      executable plain files (0100)
    %      device files (character or block special)
    %b     block special files
    %c     character special files
    r      owner-readable files (0400)
    w      owner-writable files (0200)
    x      owner-executable files (0100)
    A      group-readable files (0040)
    I      group-writable files (0020)
    E      group-executable files (0010)
    R      world-readable files (0004)
    W      world-writable files (0002)
    X      world-executable files (0001)
    s      setuid files (04000)
    S      setgid files (02000)
    t      files with the sticky bit (01000)

  print *(m-1)          # Files modified up to a day ago
  print *(a1)           # Files accessed a day ago
  print *(@)            # Just symlinks
  print *(Lk+50)        # Files bigger than 50 kilobytes
  print *(Lk-50)        # Files smaller than 50 kilobytes
  print **/*.c          # All *.c files recursively starting in $PWD
  print **/*.c~file.c   # Same as above, but excluding 'file.c'
  print (foo|bar).*     # Files starting with 'foo' or 'bar'
  print *~*.*           # All Files that do not contain a dot
  chmod 644 *(.^x)      # make all plain non-executable files publically readable
  print -l *(.c|.h)     # Lists *.c and *.h
  print **/*(g:users:)  # Recursively match all files that are owned by group 'users'
  echo /proc/*/cwd(:h:t:s/self//) # Analogous to >ps ax | awk '{print }'<

So as you can see the ‘.’ matches just the plain files. Execute ‘ls -la *(.)’ and I’m sure you get the idea. The lovely about this is that you’ll get a very powerful commandline if you combine globbing with according keybindings and the completion system. Want to list the ten newest files in the current directory? Run ‘ls -rl *(D.om[1,10])’ (or just ‘lsnew’ when using grml’s zsh configuration). This works remote as well! Just give it a try: copy the five most recent files from $HOME/incoming on $SERVER to your local disk running ‘scp $server:incoming/*(D.om[1,5]) .’.

Notice: Just press <tab> behind the pattern to expand it on the command line. Or if you prefer to see what the current pattern will be expanded to without actually expanding it right now on the prompt use the list-expand widget: press ‘ctrl-x g’ (when using emacs keybindings) or ‘ctrl-g’ (using vi bindings) when the cursor is directly behind the pattern.

If you’re interested in further information check out the official docs about globbing and the examples provided in the zsh-lovers manpage.

This was the last tip of I my advent calendar. I hope you enjoyed reading it.

3 Responses to “mika’s advent calendar – day 24: zsh globbing”

  1. Karl Says:

    Thank you _very_ much for your advent calendar! As usual, I got a lot of new ideas even for tools I use on a daily basis!

  2. Julius Says:

    Hey mika! By the way, I also copied this fragment from the grml configs, although I modified it:

    help-glob() {
    zle -M ”
    [help text]”
    }
    zle -N help-glob
    bindkey ‘^Xg’ help-glob

    This way, you can view the glob with a single keystroke without screwing up the command line.
    Cheers!

  3. mika Says:

    @Julius: rocking, thanks! :)

    regards,
    -mika-