Cyberithub

3 Best Linux Copy File Command Examples

Advertisements

In this Article, I will take you through 3 Best Linux Copy File Command Examples. There are many open source tools that can be used in Linux to copy the files from a source to destination in a localhost or copy files from localhost to remote server. We have commands like cp for copy within the host, scp for copy between 2 nodes and rsync to copy within the host or between the nodes. We will also go through cp options, scp options and rsync options which can be used to perform variety of tasks.

3 Best Linux Copy File Command Examples 1

Linux Copy File Command 

Also Read: 10 Best Examples of cp command in Linux

1. CP Command in Linux

Everyone working or dealing with Linux OS must be aware of cp command in Linux which is usually used to copy any files and directories within a local system. Below is the syntax to use cp command in Linux.

Syntax

cp [OPTION]... [-T] SOURCE DEST
cp [OPTION]... SOURCE... DIRECTORY
cp [OPTION]... -t DIRECTORY SOURCE...

a)Copy Files Interactively Using cp command in Linux

You can use -i option to copy files interactively from source to destination. In this example, we are trying to copy test.txt file interactively to /opt path as shown below.

[root@localhost ~]# cp -i test.txt /opt

b)Check all cp command options using --help

You can check all the command options that can be used with cp command in Linux using --help option as shown below.

[root@localhost ~]# cp --help
Mandatory arguments to long options are mandatory for short options too.
-a, --archive same as -dR --preserve=all
--attributes-only don't copy the file data, just the attributes
--backup[=CONTROL] make a backup of each existing destination file
-b like --backup but does not accept an argument

c)Performing Linux Copy File using -p option

If you want to save all the file attributes and permissions during copy then you can use -p option to copy all the attributes as well.

[root@localhost test]# cp -pv file2.txt /root/
'file2.txt' -> '/root/file2.txt'

2. RSYNC Command

RSYNC command can be used as faster copy operations and are generally considered as advance form of cp command in Linux. It will sync any change in data between source and Destination.

Syntax

Local:  rsync [OPTION...] SRC... [DEST]
Access via remote shell:
  Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST]
  Push: rsync [OPTION...] SRC... [USER@]HOST:DEST
Access via rsync daemon:
  Pull: rsync [OPTION...] [USER@]HOST::SRC... [DEST]
        rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST]
  Push: rsync [OPTION...] SRC... [USER@]HOST::DEST
        rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST

a)Copy File using rsync command

You can copy file using simple rsync command as shown below. You can also use verbose -v option to check the progress of copy operations.

[root@localhost test]# rsync -v file2.txt /root/
file2.txt

sent 3,980 bytes received 35 bytes 8,030.00 bytes/sec
total size is 3,893 speedup is 0.97

b)Check all rsync options using --help

You can check all rsync options that can be used using --help option as shown below. Using rsync options you can check and perform different rsync operations. You can check more about the usage of rsync options on 26 Popular Rsync Examples for Linux Professionals.

[root@localhost ~]# rsync --help

Options
-v, --verbose increase verbosity
--info=FLAGS fine-grained informational verbosity
--debug=FLAGS fine-grained debug verbosity
--msgs2stderr special output handling for debugging
-q, --quiet suppress non-error messages
--no-motd suppress daemon-mode MOTD (see manpage caveat)
-c, --checksum skip based on checksum, not mod-time & size

c)Sync Files Quietly with the Remote Server

If you want to sync all the source files quietly with the remote destination server, then you need to use -q option as shown below. This option will suppress all non-error messages. Here we are trying to Linux copy file to the remote server 192.168.0.110 under /opt path. Please note that here we are using password based authentication to reach out destination server.

[root@localhost ~]# rsync -q file/ root@192.168.0.110:/opt
root@192.168.0.110's password:

3. SCP Command

SCP is also known as secure copy due to the fact that it encrypts the data while performing Linux copy file operations to the destination server. This is probably the most widely tool used for remote copy.

Syntax

scp [-346BCpqrTv] [-c cipher] [-F ssh_config] [-i identity_file]
[-J destination] [-l limit] [-o ssh_option] [-P port] [-S program]
source ... target

a)Remotely Linux Copy a File using SCP Command

You can remotely copy a file using simple scp command as shown below. In this example, we are trying to copy test.txt to remote server 192.168.0.110 under /opt path.

[root@localhost test]# scp test.txt root@192.168.0.110:/opt
root@192.168.0.110's password:
test.txt 100% 0 0.0KB/s 00:00

b)Remotely Linux Copy file with the Attributes Preserved

Similar to cp command -p option as shown above, you can also use this option with scp command as shown below. This option will preserve all the file permissions during remote copy.

[root@localhost ~]# scp -p file2.txt root@192.168.0.110:/root/
root@192.168.0.110's password:
file2.txt 100% 3893 1.7MB/s 00:00

c)See Linux Copy file progress using verbose Option with scp command

If you want to view the progress of Linux copy file to remote server along with saving the file attributes then you need to use -pv option with scp command as shown below.

[root@localhost test]# scp -pv file2.txt root@192.168.0.110:/root/
Executing: program /usr/bin/ssh host 192.168.0.110, user root, command scp -v -p -t /root/
OpenSSH_8.0p1, OpenSSL 1.1.1c FIPS 28 May 2019
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Reading configuration data /etc/ssh/ssh_config.d/05-redhat.conf
debug1: Reading configuration data /etc/crypto-policies/back-ends/openssh.config
debug1: configuration requests final Match pass
debug1: re-parsing configuration
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Reading configuration data /etc/ssh/ssh_config.d/05-redhat.conf
debug1: Reading configuration data /etc/crypto-policies/back-ends/openssh.config
debug1: Connecting to 192.168.0.110 [192.168.0.110] port 22.
debug1: Connection established.

debug1: Remote protocol version 2.0, remote software version OpenSSH_7.4
debug1: match: OpenSSH_7.4 pat OpenSSH_7.0*,OpenSSH_7.1*,OpenSSH_7.2*,OpenSSH_7.3*,OpenSSH_7.4*,OpenSSH_7.5*,OpenSSH_7.6*,OpenSSH_7.7* compat 0x04000002
debug1: Authenticating to 192.168.0.110:22 as 'root'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: ecdsa-sha2-nistp256
debug1: Unspecified GSS failure. Minor code may provide more information
No Kerberos credentials available (default cache: KCM:)

debug1: Next authentication method: publickey
debug1: Trying private key: /root/.ssh/id_rsa
debug1: Trying private key: /root/.ssh/id_dsa
debug1: Trying private key: /root/.ssh/id_ecdsa
debug1: Trying private key: /root/.ssh/id_ed25519
debug1: Trying private key: /root/.ssh/id_xmss
debug1: Next authentication method: password
root@192.168.0.110's password:
debug1: Authentication succeeded (password).
Authenticated to 192.168.0.110 ([192.168.0.110]:22).

Sending file modes: C0644 3893 file2.txt
Sink: C0644 3893 file2.txt
file2.txt 100% 3893 2.0MB/s 00:00
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
debug1: channel 0: free: client-session, nchannels 1
debug1: fd 0 clearing O_NONBLOCK
Transferred: sent 5964, received 2588 bytes, in 0.1 seconds
Bytes per second: sent 57370.2, received 24895.1
debug1: Exit status 0

 

 

Also Read: 16 Zip command examples to compress folders and files in Linux/Unix

References:-

RSYNC Man Page

SCP Man Page

CP Man Page

Leave a Comment