How To Use Linux To Copy Files And Folders

Use Linux To Copy Files
Use Linux To Copy Files.

Introduction

This guide will show you how to copy files and folders from one place to another using the most popular graphical file managers and also by using the Linux command line.

Most people will be used to using graphical tools to copy files around their disks. If you are used to using Windows then you will be aware of a tool called Windows Explorer which makes it very easy.

Windows Explorer is a tool known as a file manager and Linux has a number of different file managers.

The one that appears on your system largely depends on the version of Linux you are using and to a certain degree the desktop environment you are using.

The most common file managers are as follows:

  • Nautilus (aka files)
  • Dolphin
  • Thunar
  • PCManFM
  • Caja

If you are running Ubuntu, Linux Mint, Zorin, Fedora or openSUSE then it is likely that your file manager is called Nautilus.

Anybody running the distribution with the KDE desktop environment will likely find that Dolphin is the default file manager. Distributions that use KDE include Linux Mint KDE, Kubuntu, Korora and KaOS.

The Thunar file manager is part of the XFCE desktop environment, PCManFM is part of the LXDE desktop environment and Caja is part of the MATE desktop environment.

How To Use Nautilus To Copy Files And Folders

Nautilus will be available via the menu within Linux Mint and Zorin or it will appear in the Unity Launcher within Ubuntu or via the dashboard view within any distribution using GNOME such as Fedora or openSUSE.

To copy a file navigate through the file system by double clicking on the folders until you get to the file you wish to copy.

You can use the standard keyboard commands to copy files. For instance clicking on a file and pressing CTRL and C together takes a copy of a file. Pressing CTRL and V pastes the file in the location you choose to copy the file to.

If you paste a file into the same folder then it will have the same name as the original except it will have the word (copy) at the end of it.

You can also copy a file by right clicking on the file and choosing the "copy" menu item. You can then choose the folder you wish to paste it in, right click and choose "paste".

Another way of copying a file is to right click on the file and choose the "copy to" option. A new window will appear. Find the folder you wish to copy the file to and click the "select" button.

You can copy multiple files by holding down the CTRL key whilst selecting each file. Any of the previous methods such as choosing CTRL C or selecting "copy" or "copy to" from the context menu will work for all selected files.

The copy command works on files and folders.

How To Use Dolphin To Copy Files And Folders

Dolphin can be launched via the KDE menu. 

Many of the features within Dolphin are the same as with Nautilus.

To copy a file navigate to the folder where the file resides by double clicking on the folders until you can see the file.

Use the left mouse button to select a file or use the CTRL key and the left mouse button to select multiple files.

You can use the CTRL and C keys together to copy a file.

To paste the file choose the folder to paste the file to and press CTRL and V.

If you choose to paste in the same folder as the file you copied a window appears asking for you to enter a new name for the copied file.

You can also copy files by right clicking on them and choosing "Copy". To paste a file you can right click and choose "Paste".

Files can also be copied by dragging them from one folder to another. When you do this a menu will appear with options to copy the file, link the file or move the file.

How To Use Thunar To Copy Files And Folders

The Thunar file manager can be launched from the menu within the XFCE desktop environment.

As with Nautilus and Dolphin you can select a file with the mouse and use the CTRL and C keys to copy the file. You can then use the CTRL and V keys to paste the file.

If you paste the file in the same folder as the original the copied file keeps the same name but has "(copy)" added as part of its name in much the same was as Nautilus.

You can also copy a file by right clicking on the file and choosing the "copy" option. Note that Thunar does not include a "copy to" option.

Once you have copied a file you can paste it by navigating to the folder to paste to. Now simply right click and choose "paste".

Dragging a file to a folder moves the file rather than copying it.

How To Use PCManFM To Copy Files And Folders

The PCManFM file manager can be launched from the menu within the LXDE desktop environment.

This file manager is fairly basic along the lines of Thunar.

You can copy files by selecting them with the mouse. To copy the file press the CTRL and C key at the same time or right click on the file and choose "copy" from the menu.

To paste the file press CTRL and V in the folder you wish to copy the file to. You can also right click and choose "paste" from the menu.

Dragging and dropping a file does not copy a file, it moves it.

There is an option when right clicking on a file called "copy path". This is useful if you want to paste the URL of the file in a document or on the command line for any reason.

How To Use Caja To Copy Files And Folders

You can launch Caja from the menu within the MATE desktop environment.

Caja is a lot like Nautilus and works much the same.

To copy a file locate it by navigating your way through the folders. Click on the file and then choose CTRL and C to copy the file. You can also right click and choose "copy" from the menu.

To paste the file navigate to the location where you wish to copy the file to and press CTRL and V. Alternatively right click and choose "paste" from the menu.

If you paste into the same folder as the original file then the file will have the same name but will have "(copy)" appended to the end of it.

Right clicking on a file also gives an option called "Copy To". This isn't as useful as the "copy to" option in Nautilus. You can only choose to copy to the desktop or the home folder.

Holding down the shift key on a file and dragging it to a folder will show a menu asking whether you want to copy, move or link the file.

Introduction

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.

Summary

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