1

Lets say there are two files in the working directory:

test201612290900.txt
test201612290901.txt

When I run

ftp -n $HOST <<SCRIPT
user $USER $PASSWORD
put test*.txt test.txt
bye
SCRIPT

with the ftp executable (linux), only one file gets put to the ftp site.

EDIT: I use this * because I don't now the exact filename in advance.

I found out it is always the first match (test201612290900.txt) that gets copied to the ftp site.

My question: is this documented behaviour? and, can I get control over this behaviour?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
codesmith
  • 1,381
  • 3
  • 20
  • 42

3 Answers3

1

The put command uploads a single file only.

The pattern in your put command is actually resolved by a shell even before the ftp is executed (similarly to the environment variables like $HOST).

So this may answer your question:
Does bash's * match files in alphanumeric order?


Though note that syntax put a b c is wrong. It's even surprising it does not error out. So you cannot expect it to be documented.

You better do something like:

FILES=(test*.txt)
FILE=${FILES[0]}

ftp -n $HOST <<SCRIPT
user $USER $PASSWORD
put $FILE test.txt
bye
SCRIPT

Credits: How can I get the first match from wildcard expansion?

Community
  • 1
  • 1
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
1

You are using a here document without quoting. This means that your shell will expand wildcards and interpolate variables in the here document before ftp even runs.

So in practice, you are sending

put test201612290900.txt test201612290901.txt test.txt

Quoting the here document solves that, but of course, that won't make your program work, because the FTP put command doesn't support wildcards. As indicated in Ipor Sircer's answer, the mput command does; but again, you will need proper quoting around the here document, too.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • what is a 'here document'? – codesmith Dec 29 '16 at 10:12
  • So, if `put test201612290900.txt test201612290901.txt test.txt` is executed, what will that do? According to the docs this is the syntax: `put local-file [remote-file]`. – codesmith Dec 29 '16 at 10:22
  • 1
    The behavior is probably undocumented and undefined. Some clients will ignore the third argument, others will ignore the second, some will throw a syntax error, some will do something else. – tripleee Dec 29 '16 at 10:25
  • See e.g. http://stackoverflow.com/questions/22697688/how-to-cat-eof-a-file-containing-code-in-shell/22698106#22698106 for how exactly to quote here documents. – tripleee Dec 29 '16 at 10:27
0

Viewing manpage of ftp is free today!

 mput local-files
             Expand wild cards in the list of local files given as argu‐
             ments and do a put for each file in the resulting list.  See
             glob for details of filename expansion.  Resulting file names
             will then be processed according to ntrans and nmap settings.
Ipor Sircer
  • 3,069
  • 3
  • 10
  • 15