How To Copy Files Using The Linux cp Command

Copy Linux Files
Copy Linux Files.


This guide will show you how to copy files and directories around your system using the Linux command line.

The command used to copy files is cp. 

This is the latest in a series of articles about creating, editing and moving files and folders around using the command line.

You will probably want to check out this article showing how to make directories using the mkdir command, this article showing how to create files using the cat command or this one showing how to create files using the nano editor.

For renaming files you will want to read up on the rename command and the mv command.

How To Copy A File From One Directory To Another Using Linux

The syntax for copying a file from place to another is as follows:

cp /source/path/name /target/path/name

For example imagine you have the following folder structure:

  • home
    • gary
      • documents
        • folder1
          • file1
          • file2
          • file3
        • folder2
          • file4

If you want to copy file1 from its current location in /home/documents/folder1 to /home/documents/folder2 then you would type the following in the command line:

cp /home/gary/documents/folder1/file1 /home/gary/documents/folder2/file1

There are some shortcuts you can make here.

The /home portion can be replaced with the tilde (~) which is explained in this article. That changes the command to this

cp ~/documents/folder1/file1 ~/documents/folder2/file1

You can simply omit the file name for the target if you intend to use the same file name

cp ~/documents/folder1/file1 ~/documents/folder2

If you are already in the target folder you can simply replace the path for the target with a full stop.

cp ~/documents/folder1/file1 .

Alternatively if you are already in the source folder you can simply provide the file name as the source as follows:

cp file1 ~/documents/folder2

How To Take A Backup Before Copying Files In Linux

In the previous section folder1 contains a file called file1 and folder2 does not.

Imagine however that folder2 did have a file called file1 and you ran the following command:

cp file1 ~/documents/folder2

The above command would overwrite the file1 that is currently in folder 2. There are no prompts, no warning and no errors because as far as Linux is concerned you have specified a valid command.

You can take precautions when copying files by getting Linux to create a backup of a file before it overwrites it. Simply use the following command:

cp -b /source/file /target/file

For example:

cp -b ~/documents/folder1/file1 ~/documents/folder2/file1

In the destination folder there will now be the file that has been copied and there will also be a file with a tilde (~) at the end which is basically a backup of the original file.

You can change the backup command to work in a slightly different way so that it creates numbered backups. You might want to do this if you have already copied files before and suspect backups already exist. It is a form of version control.

cp --backup=numbered ~/documents/folder1/file1 ~/documents/folder2/file1

The file name for the backups will be along the lines of file1.~1~, file1.~2~ etc.

How To Prompt Before Overwriting Files When Copying Them Using Linux

If you don't want backup copies of files lying around your file system but you also want to make sure a copy command doesn't overwrite a file indiscriminately you can get a prompt to show up asking whether you want to overwrite the destination.

To do this use the following syntax:

cp -i /source/file /target/file

For example:

cp -i ~/documents/folder1/file1 ~/documents/folder2/file1

A message will appear as follows: cp: overwrite './file1'?

To overwrite the file press Y on the keyboard or to cancel press N or CTRL and C at the same time.

What Happens When You Copy Symbolic Links In Linux

A symbolic link is a bit like a desktop shortcut. The contents of a symbolic link is an address to the physical file.

Imagine therefore you had the following folder structure:

  • home
    • gary
      • documents
        • folder1
          • file1
        • folder2
          • file1 (symbolic links points to folder1/file1)
        • folder3

Look at the following command:

cp ~/documents/folder1/file1 ~/documents/folder3/file1

This should be nothing new as it is copying a physical file from one folder to the other.

What happens however if you copy the symbolic link from folder2 to folder3?

cp ~/documents/folder2/file1 ~/documents/folder3/file1

The file that is copied to folder3 isn't the symbolic link. It is actually the file pointed to by the symbolic link so in fact you get the same result as you would by copying file1 from folder1.

Incidentally you can get the same result by using the following command:

cp -H ~/documents/folder2/file1 ~/documents/folder3/file1

Just to be sure though there is one more switch that absolutely forces the file to be copied and not the symbolic link:

cp -L ~/documents/folder2/file1 ~/documents/folder3/file1

If you want to copy the symbolic link you need to specify the following command:

cp -d ~/documents/folder2/file1 ~/documents/folder3/file1

To force the symbolic link to be copied and not the physical file use the following command:

cp -P ~/documents/folder2/file1 ~documents/folder3/file1

How To Create Hard Links Using The cp Command

What is the difference between a symbolic link and a hard link?

A symbolic link is a shortcut to the physical file. It doesn't contain any more than the address to the physical file. 

A hard link however is basically a link to the same physical file but with a different name. It is almost like a nickname. It is a great way of organising files without taking up any further disk space. 

This guide tells you everything you need to know about hard links.

You can create a hard link using the cp command however I would normally advocate using the ln command.

cp -l ~/source/file ~/target/file

As an example as to why you might use a hard link consider that you have a folder called videos and in that videos folder you have a really large video file called honeymoon_video.mp4. Now imagine you also want that video to be known as barbados_video.mp4 because it also has footage of Barbados which is where you went on honeymoon.

You could simply copy the file and give it the new name but that means you are taking up twice the amount of disk space for what is essentially the same video.

You could instead create a symbolic link called barbados_video.mp4 which points at the honeymoon_video.mp4 file. This would work well but if somebody deleted the honeymoon_video.mp4 you would be left with a link and nothing else and the link still takes up disk space.

If you created a hard link however you would have 1 file with 2 file names. The only difference is that they contain different inode numbers. (unique identifiers). Deleting the honeymoon_video.mp4 file doesn't delete the file but just lowers the count for that file by 1. The file will only be deleted if all links to that file are removed.

To create the link you would do something like this:

cp -l /videos/honeymoon_video.mp4 /videos/barbados_video.mp4

How To Create Symbolic Links Using The cp Command

If you want to create a symbolic link instead of a hard link you can use the following command:

cp -s /source/file /target/file

Again I would personally generally use the ln -s command instead but this works as well.

How To Only Copy Files If They Are Newer

If you want to copy files to a folder but only overwrite the destination files if the source file is newer then you can use the following command:

cp -u /source/file /target/file

It is worth noting that if the file doesn't exist on the target side then the copy will take place. 

How To Copy Multiple Files

You can provide more than one source file within the copy command as follows:

cp /source/file1 /source/file2 /source/file3 /target

The above command would copy file1, file2 and file3 to the target folder.

If the files match a certain pattern then you can also use wildcards as follows:

cp /home/gary/music/*.mp3 /home/gary/music2

The above command would copy all of the files with the extension .mp3 to the folder music2.

How To Copy Folders

Copying folders is the same as copying files.

For example imagine you have the following folder structure:

  • home
    • gary
      • documents
        • folder1
          • file1
          • file2
        • folder2

Imagine you want to move the folder1 folder so that it now lives under folder 2 as follows:

  • home
    • gary
      • documents
        • folder2
          • folder1
            • file1
            • file2

You can use the following command:

cp -r /home/gary/documents/folder1 /home/gary/documents/folder2

You can also use the following command:

cp -R /home/gary/documents/folder1 /home/gary/documents/folder2

This copies the contents of folder1 as well as any sub-directories and files within sub-directories.


This guide has given most of the tools you require for copying files around within Linux. For everything else you can use the Linux man command.

man cp

More From Us