Copy Files in Linux

How to Copy Files in Linux (2 Ways)

Learning file management in Linux is the key to efficient workflow. One of the most basic tasks that you would need to learn as a Linux beginner is copying files. Terminal is your friend in Linux and it can be used to most even the simplest of tasks quickly. As a beginner, if you’re struggling with copying files, here’s a tutorial with two different methods detailing how to copy files in Linux.

How to Copy Files in Linux Using Terminal

Copying files in Linux via the terminal is simple enough. All you need to do is use the “cp” command followed by two directories — one containing the file with the file name and the other where you want the file to be copied. cp stands for copy and this is the easiest Linux command to copy files from one location to another.

Here’s the syntax for cp command in Linux:

cp /path/to/file /path/to/destination

Here’s an example where we copy the file Beebom.txt in the /Desktop directory to /Downloads.

cp /home/abubakarmohammed/Desktop/Beebom.txt /home/abubakarmohammed/Downloads
Copying files in Linux using cp command

In the above example, we copied Beebom.txt which was in the Desktop directory to the Downloads directory. If you have a file in a directory different than what you’re currently in and want to copy the same in the current directory you’d need to specify the path to the directory with the file and just the name of the directory you want to copy the file in.

For copying a file from a different directory to the current directory, use this syntax:

cp /path/to/file ./directory

For example, let’s copy Beebom.txt to a directory called /example_directory.

cp /home/abubakarmohammed/Desktop/Beebom.txt ./example_directory

1. Copy Multiple Files into a Directory using cp Command

There are two ways to go about this — by mentioning every file in cp and moving them to a directory, or by using a Regex-like method to copy files with similar extensions to a directory. Here’s the syntax.

cp /path/to/file path/to/file2 /directory

Here’s an example to copy multiple files in a directory one by one. Using the below command, we’re moving the files Beebom.txt and Beebom1.desktop to /example_directory.

cp /home/abubakarmohammed/Desktop/Beebom.txt /home/abubakarmohammed/Desktop/Beebom1.desktop ./example_directory
Copying multiple files in Linux using cp command

2. Copy Files with Matching Extensions using cp Command

To copy files of similar extensions in Linux, the syntax would be:

cp /path/to/file/*.txt ./directory

This example will copy every .txt file from Desktop into the example_directory.

cp /home/abubakarmohammed/Desktop.*.txt ./example_directory
How to Copy Files in Linux (2 Ways)

2. Copy a Directory into Another Directory using cp Command

To copy a directory into another directory in Linux, you’d need to use the “-r” identifier. This lets the cp command know that you intend to copy a directory into another. Here’s the syntax:

cp -r ./path/to/directory ./directory

A real-time example of the command would be:

cp -r ./home/abubakarmohammed/Desktop/beebom ./example_directory
Copying a directory into another directory using cp command

How to Copy Files in Linux using GUI

Like on Windows, most distros ship with their own file managers and shortcuts to copy/paste by dragging/dropping files and folders. For example, Nautilus is a files app that ships with distros that use GNOME, and Dolphin ships with KDE distributions, both support dragging and dropping files and copy/paste. If you’re new to Linux and don’t find Terminal that appealing, they’re your best bet. Here’s how to copy files using Nautilus.

1. Launch Nautilus > go to the file directory > right-click on the file.

Copy to in nautilus

2. Clicking Copy will copy the file whereas clicking Copy to will open another Nautilus pop-up window where you can choose where you want to copy the file to.

Copy to pop up nautilus

3. Click on a directory where you want to copy the file and then click Select at the top-right corner. That’s as simple as it gets.

Leave a Reply