How to transfer files and folders in Linux system – SCP commands

Share this article

SCP (secure copy) command in Linux system you can think of it as cp command only difference is that it is used to copy files and folders between Linux servers with more security. SCP command uses the same authentication and security as it is used in the Secure Shell (SSH) protocol. Also, the SCP command encrypts and decrypts the transferring file or folder.

Copying folders from your machine to remote host

Command syntax.

scp -r <directory_path> remote_user@remote_host:<path>

Example:

Transferring directory from 192.168.122.1 to 192.168.122.105
test@test:~$ scp -r test foofunc@192.168.122.105:/home/foofunc/backup/

The above mentioned example will copy the test directory from the present working directory in my system (192.168.122.1) to the remote host (192.168.122.105) in /home/foofunc/backup/ directory

Coppying files from your machine to remote host.

Command Syntax:

scp <file1> <file2> remote_user@remote_host:<file copy path>

Example command for copying a single file.

test@test:~$ scp test1 foofunc@192.168.122.105:/home/foofunc/backup/

The above mention command will copy the test file from my system (192.168.122.1) to the remote system (192.168.122.105) in /home/foofunc/backup/ directory

Example command for copying multiple files.

test@test:~$ scp test1 test2 foofunc@192.168.122.105:/home/foofunc/backup/

Copying file from remote host to your machine

Command Syntax:

scp -r remote_user@remote_host:<file path>  <copy path>

Example:

test@test:~$ scp foofunc@192.168.122.105:/home/foofunc/backup/test1 .

Above command will copy the test file from /home/foofunc/backup/ remote system (192.168.122.105) to my system (192.168.122.1) to the present working directory.

Copying files between two remote host

The idea here is you will be running SCP command in your system and it will copy the file from one remote system to another.

Command syntax:

scp remote1_user@remote_host1:<file path> remote2_user@remote_host2:<copy path>

Example:

test@test:~$ scp foofunc@:192.168.122.105/home/foofunc/backup/test.txt admin@192.168.122.227:/home/admin/backup/test

Above command will copy file on path /home/foofunc/backup/test.txt from remote machine1 (192.168.122.105) to remote machine2 (192.168.122.227) inside /home/admin/backup/ directory

Some of the most widely used SCP command options

  • -P : Specify server SSH port (with upper-case)
  • -p : Preserves permissions, modes and access time of files (with lower-case)
  • -q : Quiet mode, don’t display progress or messages
  • i : Identity file or private key
  • -C : Compress the file and then transfer.

For other command options visit the manual page.

Leave a Comment

Your email address will not be published. Required fields are marked *