Do you know how to copy a file in Linux? Copying files and directories in Linux is one of the most basic (but essential) Linux commands. And it can be a little more complex than you may think. When you copy a folder, text file, or another type of file to a destination directory, you may be copying it locally (on your own drive) or to another location (uploading it to a network drive). 

Laptop displaying code

On Linux, commands are based on whether you have permissions and access, so you need to understand the basics of permissions and access to copy files and directories.

The basics: how to copy a file in Linux using the CP command

Why should you use Linux? Once you know the right commands, you’ll find that a lot of things are easier. Today, we’re going to take a look at how to copy a file in Linux.

Linux Administration: The Complete Linux Bootcamp for 2022

Last Updated March 2022

Bestseller
  • 213 lectures
  • All Levels
4.7 (1,106)

Linux Administration (Ubuntu and CentOS) for Beginners. Get the Linux skills to boost your career and get ahead. | By Andrei Dumitrescu, Crystal Mind Academy

Explore Course

First, let’s copy a file or folder in Linux from a source directory to a destination directory. We will use the CP command to copy the file.

The syntax for the CP command is:

cp source_file destination_file 

In this example, we are copying the file “source_file” to the destination directory “destination_file.” Keep in mind that this is the entire directory path. So, we could be doing something like this:

cp backup.txt /backups/archives/backup/backup2.txt 

That’s not great naming etiquette. But it highlights the point that we are not just copying between files but also between directory paths.

The CP command in Linux copies the contents of the source file into the destination file. It’s the fastest, easiest way to copy a file directly from the command line.

How to copy multiple files in Linux using the CP command

If you want to copy multiple files at once, you can use the CP command with a wild card.

For example, if we wanted to copy the files “source_file” and “source_file2” to the destination directory “destination_file,” we would use the command: 

mv source_file* destination_file 

This command will copy both files and place them in the destination directory. But we need to be careful. For instance, if we did “s*”, we would be copying all the files that started with the letter “s.” That could be hundreds or even thousands of files in the wrong directory.

Linux makes it easy to do things you want to do — if you’re careful. But if you’re not vigilant, you can also easily make major mistakes.

How to copy a folder with the CP command

Copying a folder can be a little more complex than copying a file. Make sure you have the correct permissions and that the folder structure is maintained. 

To copy a folder in Linux, use the CP command with the -r switch. This switch tells cp to copy the folder and all of its contents, including subdirectories.

The syntax for the cp -r command is: 

cp -r source_directory destination_directory 

In this example, we are copying the folder “source_directory” to the destination directory “destination_directory.” 

When you use the CP command with the -r switch to copy a folder, the contents of the source folder will be copied into the destination folder, including all subdirectories. 

It’s important to note that Linux really treats folders the same as files. In many aspects, a folder is a file; it’s a file that contains a list of other documents. That’s why most of the command line functions that work on files will also work on folders, and the opposite is also true.

How to use RSYNC to copy files in Linux

If you want to copy files or directories between two locations, you can use the rsync command. You will need to first install RSYNC with the following command:

sudo apt-get rsync

Once RSYNC has been installed, you’ll use it with the following command syntax:

rsync source_file destination_file 

In this example, we are copying the file “source_file” to the destination directory “destination_file.” 

The rsync command in Linux copies files or directories between two locations, but it also synchronizes them. This means that it compares the files in the two directories and only copies the files that are different. This can be really helpful when you are copying a lot of files because it can dramatically reduce the amount of time it takes to copy them.

Today, we would generally use an FTP or SFTP program to do this. But RSYNC is an easy-to-use and lightweight replacement.

The rsync command can also be used to copy files or directories between two Linux servers. If you’re connecting to another network device or a server through the internet, you will need to know your host information, host login name, and host password. These can be included in the RSYNC command line.

How to use the MV command in Linux

The MV command in Linux is used to move files and directories. The syntax for the MV command is the same as CP:

mv source_file destination_file 

In this example, we are copying the file “source_file” to the destination directory “destination_file.”

The CP command copies the source file’s contents into the destination file. The MV command moves (or renames) the source file to the destination file. 

If the destination file already exists, the MV command will overwrite it without asking for confirmation. The CP command will not overwrite an existing destination file. 

Managing files and permissions in Linux

As noted, you need the right Linux permissions to copy a file in Linux.  The same is true for copying a folder. 

The permissions for a file or folder are controlled by the user, group, and world. There are three types of permissions: read, write, and execute. 

The read permission allows you to view the contents of a file. The write permission lets you to make changes to the file. The execute permission permits you to run the file as a program. 

If you can’t read a file, then you can’t copy it. If you can’t write to the drive that you want to write to, you won’t be able to copy it. So, you need to make sure that your permissions have been appropriately set before you start to move, copy, and otherwise manipulate files.

Note that you can usually run commands “as an administrator” if you find that you don’t have the right permissions (provided that you are an administrator). This only requires that you say “Sudo” before you run your command.

In the case of copying files, you would write:

sudo cp source_file destination_file

This would run the above command line function as an administrator. Administrators, by default, have read, write, and execute permissions on all files and folders.

Key takeaway: If you find yourself running into problems when copying, renaming or moving files, it’s likely to be a permissions issue.

Conclusion: what’s next?

If you’re learning how to copy a file in Linux, you’re likely just starting your Linux journey.  Why not start with these Top Linux Courses? Or it may be time for you to pick a

 Linux Distros for Programming, take a Linux class, and install Linux on your home machine. 
While Linux can seem intimidating, it’s easier to learn than it seems. Everything should fall into place once you get used to controlling your operating system through the command line. A few other Linux commands you should consider learning include how to kill a process in Linux and how to rename a file in Linux.

Page Last Updated: March 2022

Top courses in Linux

Linux Mastery: Master the Linux Command Line in 11.5 Hours
Ziyad Yehia, Internet of Things Academy
4.7 (18,808)
Learn Linux User Space Debugging
Linux Trainer
4.5 (104)
Linux Diagnostics And Troubleshooting
Shikhar Verma • 50k+ Students Worldwide
4.5 (678)
Linux for Cloud & DevOps Engineers
AR Shankar | Valaxy Technologies
4.6 (1,734)
Linux Administration: The Complete Linux Bootcamp for 2022
Andrei Dumitrescu, Crystal Mind Academy
4.7 (1,106)
Bestseller

More Linux Courses

Linux students also learn

Empower your team. Lead the industry.

Get a subscription to a library of online courses and digital learning tools for your organization with Udemy for Business.

Request a demo