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

  • 1
    Does scp * user@host.com:/var/www/html/target_dir not do what you want? If so, please edit with more detail of what you're trying to do & have tried so far. – Michael Homer Sep 30 '15 at 8:05
up vote 45 down vote accepted

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.

  • 2
    Your command created local_dir directory in /var/www/html/target_dir my goal is copy only files. – Edgaras Karka Sep 30 '15 at 8:25
  • Sorry, I got it wrong. Then just repeat the command, but like this: $ scp -r ~/local_dir user@host.com:/var/www/html/ Then rename new directory if needed. – parazyd Sep 30 '15 at 8:31
  • 4
    Or $ scp -r ~/local_dir/* user@host.com:/var/www/html/target_dir Whatever floats your boat. – parazyd Sep 30 '15 at 8:34
  • How to exclude certain directories from local while copying to remote – Sushivam Nov 17 '16 at 6:09
  • 9
    Change the wildcard * to a dot . and you'll copy the directory contents (including any dot files) without copying the directory itself. scp -pr ~/local_dir/. user@example.com:/path/to/target_dir – roaima Jul 21 '17 at 22:25

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.

  • 1+ Because you need to read the comments in the accepted answer to get to the correct answer, while this one just explains it directly. – André Christoffer Andersen Jul 5 '17 at 10:01

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.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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