Using wildcards in commands with zsh

39

15

Using commands such as rsync and scp with ZSH I've run into trouble. Instead of the (normal) behaviour of giving me all matching files, it won't run and returns:

➜  ~  rsync -azP user@server:~/* ~/
zsh: no matches found: user@server:~/*

How can I fix this?

My .zshrc

ZSH=$HOME/.oh-my-zsh
ZSH_THEME="robbyrussell"
plugins=(git brew)
source $ZSH/oh-my-zsh.sh
export PATH=$PATH:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/git/bin:/usr/local/sbin

Morgan

Posted 2013-04-17T20:50:13.033

Reputation: 611

Answers

50

This is related with how ZSH manage globbing characters to generate filenames. By default, ZSH will generate the filenames and throw an error before executing the command if it founds no matches.

There are many ways to bypass this behavior, here are some of them:

  • The quickest is to enclose the globbing characters with quotes.
$ rsync -azP "user@server:~/*" ~/
  • For a permanent change, you'll have to add the following in your .zshrc file:
unsetopt nomatch

This will prevent ZSH to print an error when no match can be found.

  • Another possibility is to disable globbing for a particular command by using the noglob command modifier. By setting an alias in .zshrc for example:
alias scp='noglob scp'

Spack

Posted 2013-04-17T20:50:13.033

Reputation: 1 203

Thanks! Never had to do that with bash. – Morgan – 2013-04-18T06:36:13.103

@Morgan That's weird, actually. Without the quotes, Bash should expand the tilde before rsync ever sees it. Could it be that you were just using the same path for the home directory on both servers? – slhck – 2013-04-18T06:52:05.857

@slhck No, he's right. zsh has some more options to configure wildcards so this behavior can be changed in the zshrc. – Spack – 2013-04-18T07:30:04.937

I wasn't saying Morgan was wrong. I was just wondering why Bash doesn't expand the tilde before rsync, while Zsh seems to do that? – slhck – 2013-04-18T07:36:00.427

@Spack - you say it could be changes in .zshrc: can you elaborate a bit on how to configure this (perhaps edit your answer)? I'm looking for a way to do this without quoting the wildcard path. – sa125 – 2013-04-18T10:28:32.503

1@sa125 I've edited my answer. – Spack – 2013-04-18T18:09:20.720

1@slhck: bash only expands a tilde when it begins a word, or is the first character following a : or the first = in a variable assignment. Otherwise, it is treated literally. – chepner – 2013-04-22T18:08:29.600

8

I have been using zpretzo for quite a few months and also experienced this issue. I came across a neat and useful solution if you don't want to make any changes: simply prepend backslash to the command.

~/p/b/a/files ❯❯❯ scp *.* myserver@host:~/
*.*: No such file or directory

~/p/b/a/files ❯❯❯ \scp *.* myserver@host:~/
jquery.min.js                              100%   93KB  92.6KB/s   00:00
json2.min.js                               100%   3377   3.3KB/s   00:00

I hope this helps!

superuseroi

Posted 2013-04-17T20:50:13.033

Reputation: 251

Great solution!! – yorch – 2017-03-28T00:59:29.330

Perfect, this worked great for me on OSX! – sMyles – 2017-11-30T19:33:33.570

Awesome!! But any reason why/how this works? – Kannan Ramamoorthy – 2019-04-05T10:17:12.640

3

This solves your problem without having to manually quote the URLs

autoload -U url-quote-magic  
zle -N self-insert url-quote-magic

# sort it out for SCP
some_remote_commands=(scp rsync)
zstyle -e :urlglobber url-other-schema \
  '[[ $some_remote_commands[(i)$words[1]] -le ${#some_remote_commands} ]] && reply=("*") || reply=(http https ftp)'

Francisco

Posted 2013-04-17T20:50:13.033

Reputation: 1 250

And this goes in .zshrc? – Morgan – 2013-04-18T15:31:00.910

yes, this goes into your zsh configuration. FWIW, just start a new shell (zsh -f for a canonical shell conf), copy&paste the commands in your shell, and type (or paste) your rsync command. You'll see the magic at work ;-) (special chars at the URL will get automatically quoted) – Francisco – 2013-04-19T11:33:44.543

you should accept my answer :-P this is a lot better than quoting or turning off globing for whole command. – Francisco – 2013-04-19T13:28:11.303