Need your help in putting multiple files in the server. I have created a script but it is not putting all the files

cd /buffer/miscellaneous/new/
cd $(ls -t | head -n 1)
latest=$(find -mmin -60 | cut -d'/' -f2 | tail -n +2 | tr '\n' '\t')
/usr/local/bin/sshpass -p password sftp -oBatchMode=no adris@XX.XXX.XXX.XX:/folder1 << 1
mput $latest*

For this command

find -mmin -60 | cut -d'/' -f2 | tail -n +2 | tr '\n' '\t'

Output is 2 csv files:

cgi_20111210_235524.csv cgi_20203127_125504.csv

Now the issue is mput is putting only the first file and not putting the other files.

The output of the find command is as follows

find -mmin -60
  • 1
    If $latest is a space-delimited string with two filenames in it, why do you add * at the end of the string when you use it with mput? You also seem to have missed the termination of the here-document (should be a lone 1 on a line after bye since you started the redirection with <<1). – Kusalananda Jan 28 at 10:57
  • even if i remove the * still output remains the same this is what i get sftp> mput cgi_20111210_235524.csv cgi_20203127_125504.csv Uploading cgi_20111210_235524.csv to /adris/cgi_20203127_125504.csv sftp> bye – Richa Mohan Jan 28 at 11:03
  • Your commands might not work as expected if you have files in subdirectories. Instead of combining the find output to a space-delimited string you could create multi-line input for sftp. To propose alternative commands it would help to see the output of find -mmin -60. Please edit your question to add information. – Bodo Jan 28 at 11:13
  • @Bodo : added the info of find command. – Richa Mohan Jan 28 at 11:18
  • I assume cut -d'/' -f2 is to remove ./ and tail -n +2 to skip the . line, right? – Bodo Jan 28 at 11:20

The command cut -d'/' -f2 to skip the line . in the find output can be omitted by adding -type f to find only files, not directories.

The command cut -d'/' -f2 will not result in the file name if there are files in subdirectories, also the command for sftp would not work in this case. So I suggest to limit the depth for find.

I suggest to create separate commands for sftp from the file names printed by find and pipe this into your sftp command.

{ find -maxdepth 1 -type f -mmin -60| cut -d'/' -f2 | sed 's/^/put /' ; echo bye ; } | /usr/local/bin/sshpass -p password sftp -oBatchMode=no adris@XX.XXX.XXX.XX:/folder1

To see what commands are created for sftp you can omit the part | /usr/local/bin/sshpass...


To log the file names you can insert something like tee logfile into the pipe after the find or cut command. If you want to append to an existing logfile instead of overwriting it, use tee -a logfile


{ find -maxdepth 1 -type f -mmin -60 | cut -d'/' -f2 | tee logfile | sed 's/^/put /' ; echo bye ; } | /usr/local/bin/sshpass -p password sftp -oBatchMode=no adris@XX.XXX.XXX.XX:/folder1
| improve this answer | |
  • Thank you @Bodo works as expected :) – Richa Mohan Jan 28 at 12:16
  • I want to do some modification in the command. I also want to make a log file in which the filename of all the files being transferred in the command are written in the file. { find -maxdepth 1 -type f -mmin -60| cut -d'/' -f2 | sed 's/^/put /' ; echo bye ; } | /usr/local/bin/sshpass -p password sftp -oBatchMode=no adris@XX.XXX.XXX.XX:/folder1 – Richa Mohan May 19 at 6:43
  • @RichaMohan Do you want to have the output of the find command (or after the cut command) into a file? – Bodo May 19 at 14:55
  • Thanks I used tee command and it worked well :) – Richa Mohan May 20 at 5:40

Sure your sftp version offers the mput command? It is not listed in any sftp man page I searched recently; I guess they might accept it for back compatibilty reasons as an "alias" for the put command, which in turn can transfer multiple files by itself. man sftp:

put [-afPpr] local-path [remote-path]

Upload local-path and store it on the remote machine. If the remote path name is not specified, it is given the same name it has on the local machine. local-path may contain glob(7) characters and may match multiple files.

With two parameters, (m)put uses the second as the target file name, and thus transfers one file only. Any further parameters are ignored:

sftp> mput file1 file2

Uploading file1 to /user/TMP/file2
100% 48 1.9KB/s 00:00

You might want to consider sftp's batch mode to put multiple non-globable files.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.