• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • VCS
  • Interview Questions
  • Database
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

How to Transfer files securely using SCP Command in Linux

by admin

The scp(secure copy) command allows you to copy files or directories (use the -r option to copy directories) between remote systems. A connection is established, files are copied, and the connection closes.

Transfer file from local server to remote server

To copy a file to a remote system (upload), the format of the scp command is:

# scp [options] local-file [user@]to-host[:remote-file]

For example, to copy a file named test to the remote user’s home directory on host03 with user root.

# scp /var/tmp/test root@host03

To transfer the file to any other location on remote host than the user’s home directory :

# scp /var/tmp/test root@host03:/data

Transfer file to remote host with new name on remote host

To copy the same file to the same location but rename it to new_test use the below command.

# scp test root@host03:/var/tmp/new_test

Transfer file from remote server to local server

To copy a file from a remote system (download), the format of the scp command is:

# scp [options] [user@]from-host:remote-file local-file

For example, to copy a file named new_test from user’s home directory on remote host03:

# scp host03:new_test .

The dot (.) here represents the current directory of the user on local server.

To transfer file from remote host to local host with new name

To copy a file named new_test from user’s home directory on remote host03 and rename it to newer_test:

# scp host03:new_test newer_test

Transfer directories

To transfer directory from local server to remote server(host03) recursively use the -r option with scp command.

# scp -r /dir root@host03:/data

Similarly to transfer directory from remote server to local server recursively:

# scp -r root@host03:/data /var/tmp

Use different port than 22 with scp

By default the port used by ssh/scp commands is 22. If you want to use a port other than this use the -P switch with scp command. For example:

# scp -P 2222 testfile root@host03:/var/tmp

Filed Under: Linux

Some more articles you might also be interested in …

  1. fgrep Command Examples in Linux
  2. Linux OS Service ‘o2cb’
  3. CentOS / RHEL : How to collect sosreport
  4. mktemp: command not found
  5. The Squid Service in Failed State with Error: “Failed to make swap directory /var/spool/squid/00: (13) Permission denied”
  6. kexec: command not found
  7. btrbk: command not found
  8. fg: Run jobs in foreground
  9. eva: Simple calculator REPL, similar to bc, with syntax highlighting and persistent history
  10. hardinfo: command not found

You May Also Like

Primary Sidebar

Recent Posts

  • gixy Command Examples
  • gitsome Command Examples
  • gitmoji Command Examples
  • gitlint Command Examples

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright