How to Copy Files and Directories in Linux

Copying files is one of the most quintessential skills that a beginner or any Linux user should have. In this guide, we demonstrate how you can copy files on the command line. There are 2 commands that are widely used when copying files. These are cp and scp commands for secured copy. Without much further ado, let’s look at each in turn.

Copying commands using the cp command

cp command is used for basic or routing copying tasks. The syntax for using the cp command is as shown:

$ cp [sourcefile] [destinationfile]

The simplest case of using the cp command involves copying a file in the same directory. For instance, to copy a file, say file1.txt to another file backup.txt run the command

$ cp file1.txt backup.txt


[ rootel inux ~]# cat filel. txt
Hey guys ! We lcome to Linux Operating system .
[ rootel inux ] #
[ rootel inux ~]# cp filel.txt backup.txt
[ root0l inux ~] # I
[ rootel inux ~]# cat backup.txt
Hey guys ! We lcome to Linux Operating system .
[[email protected] inux ~] #

To copy a file to a different directory, use the syntax

$ cp source file /path/to/directory

For instance, to copy file1.txt to /var/run/ execute the command

$ cp file1.txt /var/run/


[ rootel inux ~] #
[ rootlinux ~]# cp filel. txt /var/run/
[ rootel inux ~] #

To copy a file from a different source path to another destination use the syntax

$ cp /source/of/the/file  /destination/of/the/file

For example to copy /etc/ssh/ssh_config file to /var/log/ directory path, run

$ cp /etc/ssh/ssh_config  /var/log/


[rootClinux ~] #
[rootlinux ~]# cp /etc/ssh/ssh_ config /var/log/
[rootel inux ~ ] #

Options with cp command

Let’s take a peek at some of the options that can be used with cp command

Copy interactively using the -i option

If you are copying a file, say file1.txt to an existing file in the current directory, use the -i optionto prompt for confirmation.

$ cp -i file1.txt backup.txt


[ rootClinux ~] # cp -i filel.txt backup.txt 
cp: overwrite 'backup.txt'? Y

In the example below, the file backup.txt already exists. With the -i option, the user is prompted if they want to overwrite the file. to overwrite the file, type Y and hit Enter.

Display verbose output using the -v option

To capture the verbose output of the copying operation, use the -v flag as shown

$ cp -v file1.txt backup.txt


[[email protected] inux ~]# cp -v filel.txt /etc/yum/
'filel.txt' -> '/etc/yum/filel.txt'

Preserve file attributes using the -p option

When issuing the cp command, the new file shall be owned by the user. If you want to preserve attributes such as file ownership, and timestamps, use the-p optionas shown

$ cp -p  file1.txt backup.txt

Copy a directory recursively using the -R option

To copy a directory alongside all its contents use the -Rparameter as shown

$ cp -R  directory  /destination/path/

For example, to copy a directory called sales to /home/departments path, run

$ cp -R sales /home/departments/

To copy the contents of a directory but not the directory itself, use the wildcard symbol as shown

$ cp -R  directory/*   /destination/path/

Copying multiple files in a single command

To copy multiple files in a single command, use the syntax

$ cp file1.txt file2.txt file3.txt file4.txt …..  /destination/path/

When copying multiple files, the destination can only be a directory and not a file!
To copy multiple files bearing the same file extension, for example, .png, use the wildcard symbol (*) as shown below

$ cp *.png /destination/path/

Copy files securely using scp command

If you want to send files across 2 Linux servers, use SCP command for secure copy of these files. Short for Secure Copy, SCP is a command that rides on the SSH protocol to send files across Linux servers in a secure manner. This way, the transmission is encrypted and hackers cannot snoop or eavesdrop on the contents of the files being sent.

The basic syntax of SCP command is as shown:

$ scp source_file  [email protected]_IP_address:destination_folder

For example to send a file securely from a host Linux server to the root folder of a remote Linux server of IP, execute the command

# scp file1.txt  [email protected]:/root

If you are connecting to the remote system for the first time, you will be asked if you’d like to continue connecting as the local Linux system saves the fingerprint keys to a file called known_hosts located in /root/.ssh/ folder.

Press Y and hit Enter

Next, you will be prompted for the remote system’s password as shown. Type the password and hit ENTER to save the file to the remote server.

[rootelinux ~]# scp filel.txt [email protected]:/root
The authenticity of host ' (' can't be established.
ECDSA key fingerprint is SHA256:lFiz2O9xA3ITkGOdj3hOrdjnOa D+wbgPlchcrk6Cc8U.
ECDSA key fingerprint is MD5:ad:ca:cl:f7:df:56:l3:d5:4d:f9:e0:7a:d4:ce:6d:f4.
Are you sure you want to cont inue connecting (yes/no) ? yes
Warning: Permanently added " (ECDSA) to the list of known hosts .
[email protected]'s password:
filel. txt						100%	46	13.6KB/s	00:00
[rootElinux ~]#

This brings us to the end of this tutorial. We hope it has been informative and beneficial. So go on and try out the commands 🙂

Written by Rahul Raheja

Leave a Reply