- Basic Linux tutorials for beginner
- Linux basic concepts, installation and troubleshooting
- Linux administration
- Linux commands
- Linux network tutorials
- Linux security tutorials
- Linux software configuration
- Linux desktop tutorials for beginner
- Collection of Linux tips
Linux cp command - Copy file and directory
Submitted by jinlusuh on Wed, 02/25/2009 - 06:52
Linux 'cp' command is used to copy files and directories in Linux command line terminal. To copy file means to duplicate a file or make an extra copy of a file. The cp command is quite important and used frequently when we work in the command line terminal. Normally, we use the cp command to backup a file by making extra copy of the original file. A situation example is when we are going to edit an important file such as a server configuration file. We can either copy one file to a given destination, or copy many files to a destination directory. The cp command also can be used to copy directory in Linux. Let's see how to use the Linux cp command.
Linux cp command syntax
A command syntax is a synopsis, the format on how to issue the command to do specific task. We can check Linux cp command syntax using the 'man cp' command. Here is a part of the cp command's manual page:
If you are really a Linux beginner and cannot understand the synopsis in manual page above, below are the Linux cp command syntax which we can use to copy file or directory in Linux system:
Linux cp command syntax to copy a single file or directory:
cp [option] SOURCE DESTINATION
Here are the syntax explanations:
- [option] - options available that we can used to customize cp command.
- SOURCE - the original file (or directory). Provide a full directory path if you are running cp command from other directory.
- DESTINATION - the new target or directory to put the duplicate file (or directory).
Linux cp command syntax to copy multiple files:
cp [option] [SOURCE1 SOURCE2 SOURCE3 ...] DESTINATION
The [SOURCE1 SOURCE2 SOURCE3 ...] is multiple files that are going to be copied. If sources are from different directories, you have to provide a complete path for each source.
Here are important Linux cp command's options that regularly used to copy files in Linux system:
Let's see how to use the cp options above with some examples.
Linux cp command examples
Here are examples and explanations on how to copy files and directories in Linux system.
Linux copy file examples
The Linux cp command's option is not mandatory if we want to copy files only. The cp command can be used without any option. See the Linux copy file example below:
In the example above, BUKUMERAH-BM.pdf is the source file that we want to copy. The '../example' is the destination directory. If you wonder why there is a '..' before the /example, the explanation is, the '..' is a 'path' for the destination directory. The '..' means the /example directory is one directory above the current directory we are working now. We can also write the full path like '/home/luzar/examples/'. You can check Linux file system structure overview for more information about Linux directory.
Our next example is how to use Linux cp command to copy multiple files to a target directory. Here is the example:
When we use '-i' option with the cp command, it will prompt us if the file we are copying already exist in the destination directory, so we can confirm whether to overwrite it or not. You can answer with y (yes) or n (No). This is perfect if we are going to copy multiple files in a regular update directory. Also notice that this time we used a full path for the destination directory.
We can also force Linux to copy file using the 'cp -f' command. Here is the example:
Another example of Linux cp command's option usage is the 'cp -s', which makes a symbolic links instead of copying the real file. The command syntax is different here. When using cp with -s option, we mention the target file first followed by the name of the symbolic link. Here is the example:
Here is the explanation. First I change directory to the example directory and delete a file named 'new-file.txt, the file that I forcefully copied in the Linux copy file example earlier. Then I use the 'cp -s' command to create a symbolic link in the current directory pointing to the original file in awareness directory.
Linux copy directory examples
We can also use cp command to copy directory. The '-R' or 'r' option is needed to recursively copy the sub-directory and files in that directory. See the Linux copy directory example below:
In the example above, we copy a directory named slackware to another directory named examples.
We can verbosely copy directory to see what files and directories have been copied. Invoke cp with -v from the command prompt will print details about what happens. See Linux cp example below:
This is an example of the Linux cp command with '-u' option. The cp -u copy only when the SOURCE file is newer than the destination file or when the destination file is missing:
Here is the explanation of the example above. To show how 'cp -u' command works, we create a new file in the awareness directory (touch awareness/newfile.txt). When we invoke cp -rvu awareness/ examples/, only the 'newfile.txt' which is the new file were copied. Don't worry about duplicate file or directory. This is really useful when backing up a regularly updated directory.
Here is another example on how to use Linux copy command to copy file and directory in Linux command line terminal. Example below shows how to copy file to the current directory:
First, here is an example content of a directory named 'awareness':
If I want to copy 'configuration.php' file to the current directory, the copy command would be like the example below:
The symbol '.' represents current directory.
To copy the whole directory to the current directory, we can use the same method but with -r option added. See the example below:
That's all examples of Linux copy file and Linux copy directory using cp command. The important thing to remember about Linux cp command is, to copy directory, you have to provide the -r or -R option(switch). It's optional to use an option(or arguments) with cp command to copy file depends on your need.
The different between Linux cp command and Linux mv command is, the cp command duplicate the source file and move it to a new location while the mv command move the source (original) file. So the Linux cp command is the perfect choice to backup files and directories in Linux system.
Back to Linux basic commands main page.