On Linux operating systems, the cp command is used for copying files and directories.
Copy a file to another file:The simplest is to copy a file to another file in the current working directory.
For example, to copy a file named file.txt to a file named file_backup.txt in the current directory, we would run the following command:
$ cp file.txt file_backup.txt
If the destination file exists, it will be overwritten. To get a confirmation prompt before overwriting the files, we use the -i option.
Example:
$ cp -i file.txt file_backup.txt
Copy a file to a directory:To copy a file to a directory, we specify the absolute or the relative path to the directory.
In the following example, we are copying the file file.txt to the /backup directory:
$ cp file.txt /backup
If we want to copy the file under a different name, we need to specify the desired file name:
Example:
$ cp file.txt /backup/new_file.txt
Copying Directories to another directory:To copy a directory, including all its files and subdirectories, we use the -R or -r option.
In the following example, we are copying the directory Pictures to Pictures_backup:
$ cp -R Pictures Pictures_backup