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 [email protected] /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

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

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

$ scp -r ~/local_dir <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="add8dec8dfedc5c2ded983cec2c0">[email protected]</a>:/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.

Method 2

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

$ scp ~/local_dir/* <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cdb8bea8bf8da5a2beb9e3aea2a0">[email protected]</a>:/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.

Method 3

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/. <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="eb9e988e99ab8384989fc5888486">[email protected]</a>:/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.

Method 4

Follow these steps:

  1. Copy directory local_dir with all its sub-directories:
    scp -r ~/local_dir <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0277716770426a6d71762c616d6f">[email protected]</a> /var/www/html/target_dir
  2. copy only the contents of local_dir and not the directory local_dir itself:
    scp -r ~/local_dir/* <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8df8fee8ffcde5e2fef9a3eee2e0">[email protected]</a> /var/www/html/target_dir
  3. Do not use: scp -r ~/local_dir/. [email protected] /var/www/html/target_dir as it throws an error(just tested and received the following error):
    scp: error: unexpected filename: .

Method 5

Also

rsync -avP ~/local_dir/ <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3b4e485e497b5354484f15585456">[email protected]</a>:/var/www/html/target_dir/

should work.

And you can run it with -n at its end

rsync -avP ~/local_dir/ <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1e6b6d7b6c5e76716d6a307d7173">[email protected]</a>:/var/www/html/target_dir -n

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


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