One of the commands that you must know in Linux is cp. It’s often called the copy command in Linux and it is actually short for copy and it does exactly as it name suggests: it copies.
cp is used for copying files from one location to other. cp can also be used to copy entire directories into a new location. You can use it to copy multiple files and directories as well.
7 Examples of using cp command in Linux
Let’s see how you can use cp command for various purposes:
1. How to copy a file
The simplest example would be to copy a file. To do that, you just have to specify the source file and the destination directory or file.
cp source_file target_directory/target_file
In the above example, if the target_fille doesn’t exist in the target_directory, it will create target_file.
However, if the new_file already exists, it will overwrite it without asking. Which means the content of the existing target file will be changed with the content of the source file.
I’ll show you how to deal with overwriting of files later in this tutorial.
Keep in mind: By default, cp commands overwrites if the target file already exists. This behavior can be changed with -n or -i option, explained later.
2. How to copy multiple files
If you want to copy multiple files at once to a new location, you can do that in the following manner:
cp file1 file2 file3 fileN target_directory
This will copy all the specified files to the target directory. If the target directory has file(s) matching the name of the source file(s), it will be overwritten.
3. Multiple ways of dealing with overwriting while copying files
You probably won’t always want that your existing target files are overwritten and that’s totally logical.
To prevent overwriting existing files, you can use the -n option. This way, cp won’ overwrite existing files.
cp -n source_file target_directory
But maybe you want to overwrite some files. You can use the interactive option -i and it will ask you if you want to overwrite existing file(s).
cp -i source_file target_directory cp: overwrite 'target_directory/source_file'?
You can enter y for overwriting the existing file or n for not overwriting it.
There is also an option for making automatic backups. If you use -b option with cp command, it will overwrite the existing files but before that, it will create a backup of the overwritten files.
cp -b file.txt target_dir/file.txt ls target_dir file.txt file.txt~
The backup of the file ends with ~.
You can also use the update option -u when dealing with overwriting. With the -u option, source files will only be copied to the new location if the source file is newer than the existing file or if it doesn’t exist in the target directory.
- -i : Confirm before overwriting
- -n : No overwriting
- -b : Overwriting with backup
- -u : Overwrite if the target file is old or doesn’t exist
4. How to copy a directory in Linux
You can also use the cp command to copy entire directory including all its files and sub-directories. You have to use the -r option here which stands for recursive.
cp -r source_dir target_dir
This will copy the entire source_dir into target_dir. Now the source_dir will be a subdirectory of the target_dir.
ls target_dir source_dir
5. How to copy only the content of a directory, not the directory itself
In the previous example, you copied the entire directory into a new location.
But if you just want to copy the content of the source directory into the target directory, you should add /. at the end of the source directory. This will indicate that you only want to copy the content of the source directory.
Let’s see it with an example:
ls source_dir source_file_1 source_file2
Now copy the content of the source directory:
cp -r source_dir/. target_dir
If you check the contents of the target directory now, you’ll see that only the contents of the source directory have been copied.
ls target_dir source_file_1 source_file2
6. How to copy multiple directories
You can also copy multiple directories at once with cp command in Linux.
Just use it the same way you did for a single directory.
cp -r source_dir1 source_dir2 source_dir3 target_dir
It’s always the last argument in the command that is taken as the target directory.
If you want to copy only the content of multiple directories at once, you can do that as well:
cp -r source_dir1/. source_dir2/. source_dir3/. target_dir
In fact, you can mix directories, their content and files altogether.
cp -r source_dir1 source_dir2/. source_file target_dir
Tip: You can use the verbose mode with option -v to see what files are being copied.
7. How to preserve the attributes while copying
If you want to retain the attributes of the original file, you can copy the files with the option -p.
Let’s see it with an example.
ls -l /etc/services -rw-r--r-- 1 root root 19183 Dec 26 2016 /etc/services
If I try to copy this file normally, its attributes will be changed:
cp /etc/services . ls -l services -rwxrwxrwx 1 abhishek abhishek 19183 Nov 25 20:45 service
But if I use the option p, the copied file will retain the mode, ownership and the timestamp.
cp -p /etc/services . ls -l services -rw-r--r-- 1 abhishek abhishek 19183 Dec 26 2016 services
As you can see, you preserved the access mode and the timestamp pf the source file with the -p option.
But wait! Was it not supposed to preserve the ownership of the source files as well? But here the owner (root) of the source file has been changed to abhishek.
This is because only root has the permission to change the ownership of a file owned by root. If you use the -p option with a file not owned by root, it will preserve the ownership. Or, you can run the command with sudo to preserve the ownership of a file owned by root.
You can also specify the attributes you want to preserve. But then you’ll have to use the –preserve option.
cp --preserve=timestamp /etc/services . ls -l services -rw-r--r-- 1 abhishek abhishek 19183 Dec 26 2016 services
As you can see in the above output, it preserved only the timestamp of the source file.
You can further explore the cp command by browsing its man page. The examples shown here are the most common ones that you’ll be using as Linux user, sysadmin or software developer.
If you liked this tutorial, please share this article on social media and various forums.