curl-users
Re: Wildcards in ftp file uploads
Date: Wed, 12 Jun 2013 11:46:34 -0400
On Wed, Jun 12, 2013 at 10:44:26AM -0400, Michael Peterson wrote:
> I’m hoping someone can tell me more about how exactly cURL works, specifically
> the “-T, --upload-file <file>” flag. I’m working on a script to upload files
> automatically to a server. The desired behavior is to upload all files of a
> certain type(s) to the server, ideally something like “-T {*.zip,*.log,*.sh}
> ftp://1.1.1.1%e2%80%9d. Obviously this command doesn’t work. I started looking into how
> –T would handle wildcard characters.
> The only two good examples dealing with my situation seemed to be from the curl
> mailing list in 2003 and 2007 stating that curl can’t/doesn’t handle wildcards
> for uploads. However, this is demonstratively false now. I don’t know when
> exactly it was added though, or if it's intended.
>
> On the script I’m trying to build I’ve been able to use *.zip to upload a
> single zip file. After not being able to get other extensions to work I built a
> quick test server and have witnessed the following behavior. I put 3 files on
> the local server named test.txt test2.txt and test.zip; curl version is 7.29.0.
>
> curl -v ftp://1.1.1.1 –u user:Pass -T *.txt ftp://1.1.1.1
> transfers only test2.txt to the remote server, first one on the file list?
I don't think this is correct. Since you've not quoted the glob, your
shell expands it to one or more words before curl sees it. You're
essentially passing the following commandline to curl, which you'll see
is not what you want:
curl -v ftp://1.1.1.1 -u user:Pass -T test.txt test2.txt ftp://1.1.1.1
What you meant to write is:
curl -v ftp://1.1.1.1 -u user:Pass -T "*.txt" ftp://1.1.1.1
The remainder of your analysis is also incorrect based on this same
mistake.
> So, what I think I've shown is that curl does support wildcards, to an extent.
> I'm not sure if this is intended behavior though, or if this was added sometime
> in the last ~6 years since the topic was discussed on the mailing list.
>
> I should note that shifting the -T to the left (curl -v -T test.txt ftp://
> 1.1.1.1 –u user:Pass) works for single file uploads (exact file names), but
> doesn't seem to generate the same results. i.e. -T *.txt or test.* uplodads
> nothing, instead of the test2.txt and test.txt respectivly.
>
> Trying to glob the expressions i.e. {*.txt}, {test.*}, or {*.txt,*.zip} doesn't
> seem to work at all, nothing is uploaded.
>
> So, back to the topic. We know curl can upload multiple files by globbing them
> together inside {}. We know that curl can handle wildcards. Can someone tell me
> if this behavior is intended? and either way, how I can use it to upload
> multiple files with wildcards?
curl(1) is non-direct about its use of quoting for the -T option, but
it's very clear that you can specify the -T option multiple times if you
instead want to expand the globs yourself. You could do something like
(in bash):
uploads=()
for f in *.txt; do
[[ -e $f ]] && uploads+=(-T "$f")
done
if (( ${#uploads[*]} )); then
curl -v ftp://1.1.1.1 -u user:Pass "${uploads[@]}" ftp://1.1.1.1
fi
Cheers,
Dave
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-users
FAQ: http://curl.haxx.se/docs/faq.html
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2013-06-12