How to copy all files from a directory to a remote directory using scp?

My goal is copy only all files from ~/local_dir to user@host.com /var/www/html/target_dir using scp and do not create local_dir category in local_dir.

/var/www/html/target_dir/files..

but not

/var/www/html/target_dir/local_dir/files.. when use -r parameter

Asked By: Edgaras Karka

||

scp has the -r argument. So, try using:

$ scp -r ~/local_dir user@host.com:/var/www/html/target_dir

The -r argument works just like the -r arg in cp, it will transfer your entire folder and all the files and subdirectories inside.

Answered By: parazyd

If your goal is to transfer all files from local_dir the * wildcard does the trick:

$ scp ~/local_dir/* user@host.com:/var/www/html/target_dir

The -r option means “recursively”, so you must write it when you’re trying to transfer an entire directory or several directories.

From man scp:

-r 
Recursively copy entire directories. Note that scp follows symbolic links encountered in the tree traversal.

So if you have sub-directories inside local_dir, the last example will only transfer files, but if you set the -r option, it will transfer files and directories.

Answered By: tachomi

Appending /. to your source directory will transfer its contents instead of the directory itself. In contrast to the wildcard solution, this will include any hidden files too.

$ scp -r ~/local_dir/. user@host.com:/var/www/html/target_dir

Credit for this solution goes to roaima, but I thought it should be posted as an actual answer, not only a comment.

Answered By: raphinesse

Follow these steps:

  1. Copy directory local_dir with all its sub-directories:

    scp -r ~/local_dir user@host.com:/var/www/html/target_dir
    
  2. copy only the contents of local_dir and not the directory local_dir itself:

    scp -r ~/local_dir/* user@host.com:/var/www/html/target_dir
    
  3. Do not use: scp -r ~/local_dir/. user@host.com:/var/www/html/target_dir as it throws an error(just tested and received the following error):

    scp: error: unexpected filename: .
    
Answered By: Syed Faraz Umar

Also

rsync -avP ~/local_dir/ user@host.com:/var/www/html/target_dir/

should work.

And you can run it with -n at its end

rsync -avP ~/local_dir/ user@host.com:/var/www/html/target_dir -n

so that it simulates the operation and you can check that the result is what you wish for.

Answered By: Tms91
Categories: Answers Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.