If you are new to Linux, it’s worth knowing that command line is a very powerful tool, capable of doing almost all those tasks that you can do through the graphical interface. However, more often than not, you’ll find yourself doing simple tasks on the command line. One such simple task is to copy files/directories.
The Linux cp command provides you the power to copy files and directories through the command line. In this tutorial, we will discuss the basic usage of this tool using easy to understand examples. But before we do that, it’s worth sharing that all examples/instructions mentioned in this article have been tested on Ubuntu 16.04LTS.
Linux cp command
Need to perform a copy operation on the command line, cp should be your go-to command. According to its man page, the tool can be used to copy both files and directories. Here’s the command’s generic syntax:
cp [OPTION]… SOURCE DEST
The above command copies SOURCE to DEST. The following Q&A type examples will give you a better idea of how the tool works.
Q1. How to perform basic copy operation?
The basic usage of cp is very easy – all you have to do is to specify the source and the destination. For example, the following command copies ‘file1’ present in the current working directory to the Desktop directory:
cp file1 ~/Desktop
Q2. How to make cp prompt before overwriting?
If the destination where you are copying the file already contains a file of same name, then the cp command silently overwrites the existing file. However, if you want, you can make the tool prompt before overwriting is done. This can be done by running cp in interactive mode, which is enabled using the -i option.
cp -i file1 ~/Desktop/
So as you can see in the screenshot above, the -i option makes cp ask the user whether or not to overwrite existing file of the same name in the destination directory.
Q3. How to force cp to not overwrite existing file?
Sometimes, you might not want cp to overwrite an existing file, and you also don’t want to enable the interactive option we just discussed in the previous section. Then for these kind of situations, you can use the -n command line option.
cp -n file1 ~/Desktop/
Please note that -n overrides the -i option, meaning even if you’ve used -i with -n, the command will not run in interactive mode.
Q4. How to copy directories using cp?
By default, the cp command only works for files. If you try copying a directory, you’ll likely get a “cp: omitting directory” error. So, whenever the requirement is to copy a directory, use the -r command line option.
cp -r dir ~/Desktop
Q5. How to create symbolic links using cp command?
If you want, you can also ask cp to create a symbolic link instead of actually copying a file. This can be done using the -s command line option.
cp -s ~/htf-daily/file1 ~/Desktop/
Q6. How to make cp overwrite destination file only if source is newer?
Sometimes, the requirement is to overwrite existing file only when the source file is newer – think of this process as updating the file. This can be done using the -u command line option.
For example, suppose you want to copy ‘file1’ residing in current working directory to the Desktop directory, but the destination already has a file named ‘file1’. And you only want to copy if source is newer than destination. This can be accomplished using the following command:
cp -u file1 ~/Desktop
Q7. How to make cp not follow symbolic links in source?
By default, the cp command follows symbolic links in source. This means that, for example, if you are trying to copy a file which is a symbolic link to another one, then by default, the copy action is taken on the the file the symbolic link refers to. What I mean is, if ~/Desktop/file1 is a symbolic link to ~/htf-daily/file1, and you try copying ~/Desktop/file1 to ~/Downloads, then ~/htf-daily/file1 will get copied there.
However, if the requirement is to copy the symbolic link itself, the this can be made possible using the -P command line option, which asks cp to not follow symbolic links in source. So in our case, the command would be:
cp -P ~/Desktop/file1 ~/Downloads/
Q8. How to copy only file attributes?
Sometimes, the requirement could only be to copy attributes (like ownership and timestamps), and not the content of the file. This can be achieved using the –attributes-only command line option along with the –preserve option.
For example, file1 has following attributes:
-rw-rw-r-- 1 root himanshu 97 Jun 14 17:18 file1
And file2 has following attributes:
-rw-rw-r-- 1 root himanshu 179 May 25 15:09 file2
Note: You can use the ls command to fetch these attributes for a file. For more information on ls, head here.
And the requirement is to copy attributes of file2 and have them for file1 as well, then here’s how this can be done:
cp –attributes-only –preserve file2 file1
Here’s the aforementioned command in action:
As you’d agree, the basic usage of the cp command isn’t very difficult to understand. In this tutorial, we have tried to cover most of the useful command line options. Try them at your end, and after that, go to the tool’s man page to see what other features/options the command offers. In case you any doubt or query, drop a comment below.