English
+370 5 205 5502 sales@monovm.com

How to Copy Linux Files

The Linux command line interface definitely requires some knowledge and experience to use, especially if you have only used the graphical user interface in the past. Today, we will show you how to copy files in Linux.

01 Oct, 21 by Antoniy Yushkevych 5 min Read

List of content you will read in this article:

Linux offers flexible and simple commands for performing various operations on the files and folders. One of the commonly performed tasks is copying the files and folders by running specific commands along with various options for additional functionality. 

In this article, we will be learning various commands for copying files along with various options. Apart from this, you will get to know how to create system-wide backups for copying specific files.

 

You can use the “cp” command for copying the files and directories from one location to another. It works showhome similar to the “mv” command that uses to move the file or directory from one location to another. Well, every Linux distribution uses the “cp” command. Below is the basic format of the “cp” command that you can use.

cp [additional_option] source_file target_file

For example:

cp my_file_name.txt my_file2_name.txt

The above command will help in creating a copy of the my_file_name and then renames the new file to the my_file_name.

By default, the “cp” will get executed in the same directory you are in. however, two files with the same name cannot exist in the same directory. If you want to copy a file that already exists in the target location, you need to change the name of the file to differentiate the new file.

 

Below are some additional options that you can use with the “cp” command to perform various operations. 

  • –v  verbose: this option will help in showing the progress of the running command that is copying the files.
  • –p  preserve: it helps in keeping the same attributes of the copied file such as creation date and file permissions.
  • –f  force: it will help in forcing the copy by deleting the existing file first
  • –i  interactive: this option will provide a prompt for confirmation before moving the file.
  • –R recursive: this option will copy all files and subfolders within a directory
  • –u update: this option will allow to update only if the source is newer than the destination.

 

If you want to copy a specific file from the current working directory to a different location other than the working directory, you can use the following command to do so.

cp my_file_name.txt /new_directory

You can even copy the file from a directory other than the current working directory by specifying the path of the file in the source and specify the path to the target as shown below.

cp /etc/my_file_name.txt /new_directory

The above command will allow you to copy the file without changing the directory. If the new_directory does not exist then the cp command will create this directory.

If we want to rename and copy the file to a different path, you can run the following command as shown below.

cp my_file_name.txt /new_directory/my_file_name2.txt

This option is also useful if you want to create backups of configuration files, or for copying data to a storage device.

 

Using the “cp” command, you will also be able to copy multiple files at the same time. You can mention multiple files separated by spaces followed by the path where you want to copy those files.

cp my_file_name.txt my_file2_name.txt my_file3_name.txt /new_directory

The above command will not move the files but create a copy of these files to the destination path. You can even use the wildcard character for copying the files as shown below.

cp /pictures/*.jpg /new_directory

The above command will search for all the files with the .jpg extension and then copy them to the destination path.

If you want to copy the entire folder along with its sub-directory then you can use the “-R” option along with the “cp” command. Where, “-R” stands for the recursive operation. 

cp –R /documents /new_directory

 

You can use the “rsync” command for synchronizing or transferring the data between two different locations. It is somehow similar to the “cp” command but shows some differences. If you want to copy a single file, then you can run the following command.

rsync –a my_file.txt /new_directory/my_file_backup.txt

 

Copying command in Linux is another commonly used command that will help in creating a copy of the existing file to another specific location. It works similar to the “mv” command but the difference is the “mv” command will move the file from source to target. But in the “cp” command, the files will remain in the source folder along with moving to the destination folder. 

In this article, you will be able to understand the nature of the “cp” command and how it works differently with the various options.

Antoniy Yushkevych

Master of word when it comes to technology, internet and privacy. I'm also your usual guy that always aims for the best result and takes a skateboard to work. If you need me, you will find me at the office's Counter-Strike championships on Fridays or at a.yushkevych@monovm.com