Today we will learn about CP commands in Linux. Linux provides flexible and simple commands to perform various operations on files and folders. One of the commonly performed tasks is to copy files in Linux by running specific commands with various options for additional functionality. In this article, we will learn different cp commands in Linux to copy files with different options. In addition to this, you will learn how to create a system-wide backup to copy specific files.
Copy files to Linux using the CP commands
You can copy files and directories from one location to another using the cp command in Linux. It works similar to the mv command showhome which is used to move files or directories from one location to another. Well, every Linux distribution uses cp command. The following 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 Linux command will make a copy of my_file_name and then rename the new file to my_file_name.
By default, the copy command in Linux will be executed in the same directory you are in. However, two files with the same name cannot be in the same directory. If you want to copy a file that already exists in the target location, you need to rename the file to distinguish the new file.
Copy commands in the Linux option
Below have some extra copy command options that you can use with the cp command to perform various operations.
- –v Verbose: This option will help show the progress of running commands copying files.
- -p preserve: This helps keep the same attributes of the copied file like creation date and file permissions.
- -f force: This will help to force the copy file by deleting the existing file first
- -i interactive: This option will provide a prompt for confirmation before moving files.
- -R Recursive: This option will copy all files and subfolders in a directory
- –u Update: This option will only allow updates if the source is newer than the destination.
How to copy files to a different directory in Linux
If you want to copy a specific file from the current working directory to a location other than the working directory, you can use the following copy command to do so.
cp my_file_name.txt /new_directory
You can copy the file from a directory other than the current working directory by specifying the file path in the source and the target path as shown below.
cp /etc/my_file_name.txt /new_directory
The command above will allow you to copy the file without changing the directory. If new_directory does not exist then the copy file command will create this directory.
If we want to rename the file and copy it to a different path, you can run the command shown below.
cp my_file_name.txt /new_directory/my_file_name2.txt

This option is also useful if you want to create a backup of configuration files or copy data to a storage device. How to copy multiple files from one directory to another directory in Linux. Using the cp file command, you will be able to copy multiple files at the same time. You can specify 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 transfer the files but will create a copy of these files in the destination path. You can use wildcard characters to copy files as below.
cp /pictures/*.jpg /new_directory.
The above command will search for all files with .jpg extension and then copy them to the destination path.
If you want to copy the entire folder including its sub-directories, you can use the “-R” option with the cp command. Where, “-R” means recursive operation.
cp –R /documents /new_directory

rsync command to copy files
You can use the rsync command to synchronize or transfer data between two different locations. It is somewhat similar to the cp Linux command but shows some differences. If you want to copy a single file, you can run the following command.
rsync –a my_file.txt /new_directory/my_file_backup.txt
Conclusion
The CP commands in Linux is another commonly used command that will help create a copy of an existing file to another specified location. In the copy command, the files will be in the source folder as well as in the destination folder.
In this article, you will be able to understand the concept of the cp Linux command and how it works differently with different options. Learn more about bash scripting. Buy Linux VPS here.



