Using sftp to Transfer a Directory?

When I try to use sftp to transfer a directory containing files, I get an error message:

skipping non-regular file directory_name

The directory contains a couple of files and two subdirectories.

What am I doing wrong?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

sftp, like cp and scp, requires that when you copy a folder (and its contents, obviously), you have to explicitly tell it you want to transfer the folder recursively with the -r option.

So, add -r to the command.

Method 2

I can only suggest, you use rsync. It is somewhat of an industry standard, when moving files over secure connections.

rsync -alPvz ./source_dir server.com:destination_dir

It is what I’ve been using for years by now.

(the -a option takes care of things like directory recursion)

Method 3

This works for me:

1) connect via sftp to remote host

2) change into the remote directory you wish to copy. (Example: cd Music)

3) change to the local directory you wish to copy stuff to. (Example: lcd Desktop)

4) Issue this command: get -r *

Method 4

You may also be able to use use scp. The general format is

scp -rp sourceDirName <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="770204120519161a1237041205011205">[email protected]</a>:destDirName

scp means “secure copy”. The flags are

  • -r recurse into subdirectories
  • -p preserve modification times

I believe the rest of the items are self-explanatory

Method 5

If rsync is not an option, I would next recommend lftp:

lftp sftp://<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c9bcbaacbb89a1a6babde7aaa6a4">[email protected]</a>/path/path/

Then use the mirror command to recursively upload, like this:

mirror -R

(Note that recursion is the default. The -R is for reverse — to make the mirror command upload instead of download. Try adding --dry-run to do a trial to make sure it’s doing what you expect.)

Or to upload without recursion:

mirror --no-recursion

You have to cd into the directories you want to mirror. Works great!

Method 6

If you question is actually ‘how do I use sftp to transfer a directory’, then

 sftp -r <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6f1a1c0a1d2f1c0a1d190a1d">[email protected]</a>

But if you transferring a directory, I might suggest two better options, depending on your needs. sftp requires logging into the remote server in a ssh-like command prompt, but if all you want is the file(s), there are easier and quicker ways.

Scp

For a one time transfer, to upload try:

 # upload source directory to remote server
 scp -rp source_dir <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e491978196a4978196928196">[email protected]</a>:dest

 #download directory from remote server:
 scp -rp <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="91e4e2f4e3d1e2f4e3e7f4e3">[email protected]</a>:source_dir dest

 #specifying ssh key:
 scp -rp -i ~/.ssh/key <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5b2e283e291b283e292d3e29">[email protected]</a>:source_dir dest

 #remote directory has spaces
 scp -rp -i ~/.ssh/key <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="166365736456657364607364">[email protected]</a>:"source\ dir" dest

Rsync

If you plan on syncing the directories on a regular basis, using rsync makes more sense. It performs deltas between the two directories, saving transfer time and data over the wire.

rsync -r -a -v -e ssh --delete source_dir <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="99eceafcebd9eafcebeffceb">[email protected]</a>:dest

Here is a list of some of the most common rsync options: (taken from here)

  • –delete – delete files that don’t exist on sender (system)
  • -v – verbose (-vv will provide more detailed information)
  • -e “ssh options” – specify the ssh as remote shell
  • -a – archive mode – it preserves permissions (owners, groups), times, symbolic links, and devices
  • -r – recurse into directories
  • -z – compress file data during transfer
  • –exclude ‘foldername’ – excludes the corresponding folder from transfer
  • -P – show progress during transfer

Hope that helps!

Method 7

If you can, use sshfs. It’s a FUSE filesystem, available on most modern unices, and works with any SFTP server. This is a remote filesystem: it allows you to manipulate remote files (over the SFTP protocol) with the usual utilities.

mkdir /mount/point
sshfs server.example.com:/remote/path /mount/point
ls /mount/point
cp -Rp /mount/point/somedir /local/location
fusemount -d /mount/point

Method 8

It’s a bit of a kludge but what works for me is to:

  1. Use ssh to login to the remote machine
  2. Use sftp from the remote machine to your local machine
  3. Then use the get command with the -r option to move the directory and all of its files.

Method 9

you can get from the server to your local path by

scp -rp <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="116462746351627463677463">[email protected]</a>:directoryname(full path) .

Method 10

It’s a workaround,

  1. You need to connect via SFTP.
  2. Go to the directory where files reside.(Example: cd [remote
    directory name])
  3. If you have many files with the same format, you can use the get *.[file format]. Example: If you have many files with .csv format, then use,
get *.csv


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x