Copying is one of the many tasks one does when working with files. But when working in different operating environments the ways of copying files may vary. This is a guide to help you understand how you can copy files in Linux.
You can copy files and directories in Linux by executing commands from the terminal or shell prompt.
It is important to remember that any command in the terminal is typed at the dollar sign ($) prompt. This prompt indicates that the shell is ready to accept the user typed commands. The dollar ($) is not typed by the user. All commands in Linux work on the terminal app.
Using the cp Command to Copy a file
The primary method to copy an existing file in Linux is by using the cp command. The cp command works in all virtual Linux distributions as well.
The syntax is
$ cp [additional_options] source_file target_file
$ cp file1.txt new_file.txt
This command creates a file with the name new_file and copies the data of file1 to it. By default, the cp command runs in the directory that you are in. However, if you do not change the copied file name two files of the same name cannot be stored at the same location and thus your file will be overwritten without a warning.
$ cp file1.txt file1.txt
In this case, the first file will be overwritten and no new file will be created. To avoid such errors you can add a number, a letter, or assign a completely different name to the new file. You can also change the extension of the file.
Copying Multiple files:
You can also copy multiple files to a directory at the same time using the cp command.
The syntax would be:
$ cp Source1 Source2 Source3 SourceN Directory
$ cp main.c demo.h backup
In this case, the files main.c, and demo.h are copied in a directory named backup. Thus, multiple files are copied into a different directory at the same instant of time.
Cp command examples in different situations:
- Copy file to a directory
$ cp first.c backup
- Copy file to a directory using an absolute path
$ cp file.txt /home/user/backup/
- Copy all files in the current directory to subdirectory
$ cp *.c backup
- Copy a directory to another directory using an absolute path
$ cp src /home/user/backup/
Additional options can be used along with the cp command. These commands are used in combination to satisfy few required conditions along with copying the file:
- –v verbose: To view output when files are copied the -v (verbose) option is used.
$ cp -v file1.txt file2.txt
- –p preserve: With the -p option cp preserves the characteristics of each source file in the corresponding destination file: the time of the last data modification and the time of the last access, the ownership ,and the file permission-bits.
$ cp -p file1.txt file2.txt
- –f force: This forces the copy by deleting an existing file first, this means remove existing destinations but never prompt
$ cp -f file1.txt file2.txt
- –i interactive: It asks before overwrite, this is highly advised to avoid loss of data by unprompted overwriting
$ cp -i file1.txt backup
cp: overwrite ‘backup/file1.txt’? y
- –R recursive: Copies all files and subfolders in a directory, including the hidden files
$ cp -R origin backup
- –u update: Used to copy only if the source is newer than the destination
$ cp -u * backup
Using the rsync Command to Copy a file
rsync or remote synchronization in Linux is used to synchronize or transfer data between two locations. It is very similar to the cp command with slight differences in the syntax and usage, which are noted below.
Copying a single file with rsync
rsync –a original_file.txt /new_directory/new_file.txt
The -a stands for all and is used along with the rsync command to preserve symbolic links, and subdirectories.
Copying a directory with rsync
rsync –a /origin/directory/ /home/backup/new_directory/
This copies the contents of the origin/directory to the home/backup/new_directory. The backslashes must be mentioned as per the path of the directories or some error might be generated.