How To Use S to Copy From Local to Remote or vice-a-versa In Linux?


Gaming Laptop Series

There are various ways to copy a file or directory from remote to local or local to the remote machine. One of them is by using the S command.

S stands for Secure copy which is a remote file copy program available in Linux or Unix systems. It copies files between hosts on a network.

This is based on SSH, and uses the same authentication and security as SSH.

[su_note note_color=”#D7E3F4″ text_color=”#728095″ radius=”3″ class=”” id=””] NOTE:
S asks for a if it needed for authentication. While copying a file, if the file already exists on the remote system then it will replace the content of that file.
[/su_note]

Syntax of S Command

The syntax of s is similar to command which is used locally for copying a file –

The basic S syntax is as below (without the square bracket):

s [option] [@local_host:/path/to/local/file_or_directory] [@remote_host:/path/to/remote/dir_or_filename]

If the name is same on both remote and local you can drop the name from the command:

s [option] [local_host:/path/to/local/file_or_directory] [remote_host:/path/to/remote/dir_or_filename]

Some Examples of using S

Copying a file to a host

s [option] [@local_host:/path/to/local/file] [@remote_host:/path/to/remote/dir]

Copying a directory from the remote host:

s -r @host:/remote/source_dir local_target_dir

If the remote host uses a port other than 22 then it can be specified in the command

s -p port_no [@local_host:/path/to/local/file] local_target_dir

Options used with s command

Options Descriptions
-P Specifies the port to connect on the remote host
-p Preserves the modification time, access time and modes from the original file
-r Recursively copy entire directories
-v Enables verbose mode, This is helpful in debugging connections, authentication and configuration problem
-T Disable the strict filename checking
-4 Forces s to use IPv4 addresses
-6 Forces s to use IPv6 addresses only
-B Select batch mode (It prevents asking or phrases)
-C Use this option to enable compression
-c Select the cipher to use for encrypting the data transfer
-F Specifies an alternative per- configuration file for SSH
-i Select the file from which the identity (private key) for public-key authentication is read
-l Limits the used bandwidth specified in Kbit/s
-o Use to options to SSH in the format used in SSH_config

 

Using S to Copy Files

While sopying and file or directory you just need to understand which is the source and which is the destination machine.
Accordingly place the host and path information in the S command.

S From Local to Remote

Below are several examples that shows how to copy files or directories from local to remote Linux using S.

Now let’s look at the direction and understand the source and destination.

Local -> Remote
Source is Local machine and Destination is Remote machine

Copy a single file

To copy a file with the name let’s say file.txt to remote system’s /home directory, use the following command in your terminal.

s /home/file.txt [email protected]:/home

Copying multiple files

Instead of copying the whole directory containing multiple files. You can copy more than one file by using the following command.

s /home/file1.txt file2.txt file3.txt [email protected]:/home

Copying a folder

Now if you want to copy a directory name dir1 from local to the remote system then use the following command in your terminal.

s -r /home/dir1 [email protected]:/home/
[su_note note_color=”#D7E3F4″ text_color=”#728095″ radius=”3″ class=”” id=””]Important is the -r option.[/su_note]

S From Remote to Local

Below are several examples that shows how to copy files or directories from remote to local Linux machine using S.

Now let’s look at the direction and understand the source and destination.

Remote -> Local
Source is Remote machine and Destination is Local machine

Copying a single file

Now you want to copy a file name file.txt from home directory of the remote system to local system’s /home directory.

Use the following command in your terminal.

s [email protected]:/home/file.txt /home/

Copying multiple files

Similarly, if you want to copy more than one file without copying the whole directory from remote to the local machine. Then you should use the following command in your terminal.

s [email protected]:/home/file1.txt file2.txt file3.txt /home/

Copying a folder

Now if you want to copy a directory name dir1 from the remote system to local, then use the following command in your terminal:

s -r [email protected]:/home/ /home/dir1
[su_note note_color=”#D7E3F4″ text_color=”#728095″ radius=”3″ class=”” id=””]Important is the -r option.[/su_note]

Copying file to a specific port

If you want to use a specific SSH port (if not 22) then use option -P (in uppercase) in your command. Use the following command in your terminal.

s -P 21 [email protected]:/home/file.txt /home/
[su_note note_color=”#D7E3F4″ text_color=”#728095″ radius=”3″ class=”” id=””]Important is the -P option.[/su_note]

Conclusion

At times it may feel like the S command is a little hard to pick up.

The more you use it, the more you get familiar with this command.

To know more about s and the options see the manual page of s. To display it use the following command in your terminal:

man s

You can always leave a comment if you need any help from me.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.