How to Copy Files in Linux


Copying files and directories from one location to another is one of the most common tasks every computer user performs on an almost daily basis. While the users coming from Microsoft Windows might already be familiar with the GUI mode of doing things through the file manager, there are other methods and commands that you can employ to move files and directories between locations.

In Linux-based operating systems the terminal comes equipped with everything you may need to operate a computer. For copying files and directories, some built-in commands and utilities can help you move items between locations. In this guide, we will understand how can you leverage these commands and methods through the terminal.

For this guide, I will demo all of the commands using Ubuntu 22.04. The same commands can work on Debian, but if you are working on any other distributions, these can work with minor adjustments. Let’s begin.

Creating a copy of the file

This is the primary way for moving items between locations. Pretty much all Linux distributions can employ the “cp” command. Here is the basic syntax for using the command:

$cp [options] [source file] [target file]

So, let’s say we want to create a copy of our file with a new name in the same directory, we can do so using the following:

$cp sample_file sample_file_copied

How-Copy-Files-Linux

As soon as this executes, you will see the new file created in your same directory.

Remember that the cp command works in the same directory by default, and since we cannot have the same file in a directory twice, we have to rename the new copy to avoid a conflict. You can append anything like “_copy” or “_new” to the end of the file name to keep a track of it. That said, the command doesn’t give you any warning if a file with the same name already exists in your directory. So be careful before executing the command to avoid any unwanted overwriting of the files.

cp command options

You can use any of the mentioned options to gain more control over the whole process and customize the output of the process per your requirements. Here are the options that you can use:

  • “-u” creates a copy of the file only if the source is newer than the destination.
  • “-v” creates a copy and details the progress as output in the terminal.
  • “-R” creates a copy of all files and subfolders in the specified directory.
  • “-p” creates a copy of the file while keeping the same attributes such as permissions and creation date.
  • “-i” creates a copy of the file after confirming the operation with the user.
  • “-f” creates a copy of the file and deletes the existing file.

Copying a file to another location

If you want to create a copy of a file in another directory, you can do so using the following:

$cp sample_file ~/target_directory

Through this command, you can create a copy in the new directory with the same name. You don’t have to rename it unless the target directory contains a file with the same name. If you want to change the name for the copy of the file, your command should look like this:

$cp file ~/Downloads/copy_sample_file

How-Copy-Files-Linux

Copying multiple files to another location

To create copies of multiple files to another directory, you need to list all of the files to be copied in the command. For instance, if you have numbered files that you want to copy to a folder within the specified directory, you can do so using this:

$cp 1 2 3 4 5 6 7 ./new_directory

How-Copy-Files-Linux

This will not give any output and create copies of these files in the specified directories unless there was an error.

Similarly, if you want to move all the copy all of the text files in the directory into a new one, you can employ wildcards to do so. It will look something like this:

$cp *.jpg ~/Pictures/sample

Copying all files and subfolders to another directory

As discussed in the beginning you can use the “-R” option to copy a specified directory and all of its contents to your specified location. If you want to copy your “Downloads” directory to another location, you can do so using:

$cp -R /home/sysadmin/Downloads /home/sysadmin/Documents/download_copy

How-Copy-Files-Linux

You can use the “ls” command to view the contents of the newly created directory.

Use the “rysnc” command

The “rsync” command is similar to the “cp” command. The “rsync” command was built to sync the contents of the source directory with the destination directory. You can also use it to copy single files from one location to another. If you want to copy a single file, you can do so using the following:

$rsync -a sample_file.txt ~/Downloads/sample_file.txt

Similarly, if you want to create a copy of a directory, you can do so using:

$rsync -a /home/sysadmin/Downloads/sample /home/sysadmin/Documents/downloads_copy

This will copy all of the contents of the source directory into the destination directory. Just ensure to keep slashes in the path and the breadcrumbs accurate in the command.

Conclusion

Getting a better handle on the terminal and all of these commands will help you better understand the process and help you get along in your journey to Linux Expertise. If you have issues at any step of the process, feel free to reach out to us and we’ll help you out. Cheers.

Print Friendly, PDF & Email
Categories