We help IT Professionals succeed at work.

How to SFTP multiple file from source to dest in Linux?

MIHIR KAR
MIHIR KAR asked
on
Hi Expert,

I have a SFTP job which is failing while transfer file for source to dest using "put" command.  As put command only works for single file.

Here my requirement is i want to transfer all the files available in "source/dir1/dir2" and want to merge them into single file then wanna SFTP.

Note:The script should also need to check how many files are there in the source directory before merge them!

thanks in advance.
Comment
Watch Question

David FavorFractional CTO
Distinguished Expert 2019

Commented:
Use mput rather than put.
Top Expert 2007

Commented:
Hi,

Your question is not clear,

you want to merge all files into one file then transfer? Why?

Also, below is not clear:

"The script should also need to check how many files are there in the source directory before merge them!"
nociSoftware Engineer
Distinguished Expert 2019

Commented:
if sftp is used how about:   (sftp uses ssh links underneath, so scp should also work).

scp /from/where/ever/* user@remote_system:/to/dest/

Open in new window

recursive with:
scp -r /from/where/ever/* user@remote_system:/to/dest/

Open in new window

You would tar the files up at the source first, then put it on the destination

tar -czf transferfile.tgz source/dir1/dir2
then you can put transferfile.tgz in the destination.

You probably should be using rsync for this instead, as that will allow you to continue after an interruption.  rsync -az source/dir1/dir2 destination/
sftp uses ssh links underneath, so scp should also work

Or RSYNC over SSH for that matter

To recreate source directory and transfer contents - (source directory created)
rsync -avz -P --stats source-folder this-user@some-domain.com:/path/to/destination-folder/

Open in new window


To transfer contents of source directory - (source directory not created)
rsync -avz -P --stats source-folder/ this-user@some-domain.com:/path/to/destination-folder/

Open in new window


adding "--delete" before source folder will delete files that are not in subsequent transfers or are older than what's being transferred.
Be careful with this, if you're in the wrong location you could delete important files

If the files are the same, they are skipped