Globbing in Zsh doesn't work with scp

Posted by Orville Bennett on 18 September 2015
Read time: about 3 minutes

Since switching to zsh I've run into a variety of issues with globbing1 and other features not working the way I was used to with bash. This post is about resolving another of those issues. Today I was trying to scp some cachegrind files from a remote box and got the following error:

scp server-local:/var/www/server/cacheg* ~/Documents/cachegrinds
usage: scp [-12346BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]
           [-l limit] [-o ssh_option] [-P port] [-S program]
           [[user@]host1:]file1 ... [[user@]host2:]file2

Basically scp is telling me I was using it wrong. After doing some searching around I found the following information on the zsh mailing list.

I suspect that you just want setopt no_nomatch so that the glob pattern is left unexpanded when it doesn't find any matching files. (You may have to unsetopt null_glob csh_null_glob as well.) That's the only way I can think of that this would do as you seem to expect in bash but not in zsh.

After trying the first option—setopt no_nomatch—my command still errored out. The second one—unsetopt null_glob csh_null_glob— did allow me to type in my unmodified scp command and download my cachegrind files. I never like the idea of just typing in random commands without knowing what they do however. Before making the change persistent in my zsh config files I decided to do a little reading on the null_glob option. After doing that I didn't really think turning this on was the best option, so I decided to use the other alternative I found in the mailing list post.

I could just put single quotes around the server path to get it to work with globbing but after many years of bash I am having trouble getting into the habbit of using single quotes with scp and it get's pretty frustrating.

That's what I decided to do, and lo, it paid off. Globbing worked as expected.

scp server-local:'/var/www/server/cacheg*' ~/Documents/cachegrinds
cachegrind.out.1426                           100% 2578     2.5KB/s   00:00
cachegrind.out.1427                           100% 2574     2.5KB/s   00:00
cachegrind.out.1428                           100% 9233KB   9.0MB/s   00:00
cachegrind.out.1430                           100% 2574     2.5KB/s   00:00
cachegrind.out.1598                           100% 2574     2.5KB/s   00:00
cachegrind.out.1600                           100% 3021KB   3.0MB/s   00:00
cachegrind.out.1602                           100% 2174KB   2.1MB/s   00:00
cachegrind.out.1604                           100%   30KB  30.4KB/s   00:00
cachegrind.out.1605                           100% 2575     2.5KB/s   00:00
cachegrind.out.1778                           100%  235     0.2KB/s   00:00
cachegrind.out.1919                           100% 9190KB   9.0MB/s   00:00
cachegrind.out.1927                           100% 2174KB   2.1MB/s   00:00
cachegrind.out.2021                           100%  233     0.2KB/s   00:00
cachegrind.out.2157                           100% 2574     2.5KB/s   00:00
cachegrind.out.2293                           100%  233     0.2KB/s   00:00
cachegrind.out.2302                           100% 2578     2.5KB/s   00:00
cachegrind.out.2346                           100%  233     0.2KB/s   00:00
cachegrind.out.2393                           100%  233     0.2KB/s   00:00
cachegrind.out.2428                           100%  233     0.2KB/s   00:00
cachegrind.out.2445                           100% 2443     2.4KB/s   00:00
cachegrind.out.2954                           100%  233     0.2KB/s   00:00
cachegrind.out.3139                           100%  233     0.2KB/s   00:00
cachegrind.out.3170                           100%  233     0.2KB/s   00:00
cachegrind.out.3278                           100%  233     0.2KB/s   00:00
1

Globbing is a process that expands patterns to filenames.