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.
Copying files in Linux using the “cp” command
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
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.
Copying the File to Another Directory in Linux
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.
Copying Multiple Files from One Directory to Another in Linux
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
Copying Using the rsync Command
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.