Purpose of cp Command in Linux
The cp (copy) command is one of the most commonly utilized commands in Linux. This powerful CLI tool enables you to copy files and directories from one location to another. Additionally, it offers various options to customize the copying process, making it a versatile tool for managing files like a pro. Whether you’re coping with a single file or multiple directories, the cp command can save you time and effort.
In this article, we’ll learn how to use the cp command in Linux via practical examples.
Syntax of cp Command
As mentioned earlier, the cp command copies files and moves them to another place. Moving onward, let’s now cover the basic syntax of the cp command:
$ cp [options] source destination
Let’s breakdown the above syntax:
- Options: Options/flags customize the behavior of the
cp
command. - Source: Name of file or directory you desire to copy.
- Destination: The destination path where the copy should be placed.
Copying File/Directory with a New Name in Linux
To copy a file, we’ll first create a file named “me1.txt” and then execute the following cp command to copy this file in the same directory with a different name, “me2.txt“:
$ cp me1.txt me2.txt
The above command copies the file me1.txt and creates a duplicate me2.txt file in the same directory or the current working directory. After executing the cp
command, use the ls
command to verify if the new file was created.
Similarly, we can copy the contents of a directory named work into a new directory named work2 inside the current working directory by executing the following cp command:
$ cp -r work/ work2/
The -r
flag in the above command is used for recursive copying, which copies the directory and all its contents.
Copying File in a Different Directory
Now that we’ve seen how to copy a file in the same directory, let’s learn how to copy a file to a different directory. For that purpose, we can use the following syntax:
$ cp source_file destination_directory/
Let’s now examine the above syntax:
- Source_file: The file you want to copy.
- Destination_directory: Path of the directory where you wish to copy the file.
For instance, we can copy the me1.txt file in the /Downloads directory by executing the following command:
$ cp me1.txt ../Downloads/
Notably, we successfully copied me1.txt from the current directory (Documents) to the Downloads directory. We can also copy a directory to another path in a similar manner.
Copying Multiple Files in Linux
In this section, we’ll learn how to copy multiple files using the cp command in Linux. The general syntax for this is as follows:
$ cp file-1 file-2 file-3 destination
For demonstration, let’s copy two files, namely “me1.txt“, “me2.txt“, from Documents (current directory) to Downloads simultaneously:
$ cp me1.txt me2.txt ../Downloads/
After executing the cp
command, navigate to the destination directory and run the ls
command to confirm that the files were copied. Thus, we’ve successfully explored how to copy multiple files simultaneously. Additionally, you can utilize the same cp command to copy more than one directory recursively.
cp Command Options
Many commands in Linux come with various flags or options, each serving a specific purpose. Some commands offer numerous options, while others have only one or two. We’ve curated all the options/flags available for the cp command into a table, along with their purposes, to help you get the most out of the cp command and copy files efficiently.
Options | Purpose |
---|---|
-a | Stand for Archive mode. It preserves symbolic links, file permissions, user & group info, timestamps, and more. |
-f | Force copies by overwriting destination files without prompting. |
-i | Stands for Interactive mode. It prompts before overwriting existing files. |
-r | Copy directories recursively, including all their contents. |
-R | Same as the -r flag, used for copying directories recursively. |
-u | Copies only if the source file is newer than the destination file or if the destination file does not exist. |
-v | Stands for Verbose mode. It displays details of the files being copied. |
-n | Stand for No-clobber. It prevents overwriting existing files. |
-p | Preserve the file attributes (permissions, timestamps, and ownership). |
-l | Creates hard links instead of copying the files. |
-t | Specifies the target directory, allowing multiple source files to be copied. |
–backup | Make a backup of the destination files before overwriting. |
–remove-destination | Removes the destination files before copying. |
–parents | Preserves the directory structure while copying files. |
-x | Stays on the same filesystem (don’t cross filesystem boundaries). |
Now, you can combine multiple cp command flags to perform the desired copying task. For instance, you can execute this cp command to copy me1.txt and me2.txt to the /home/user/Backup directory, but only if the source files are newer than the destination files while preserving file attributes and displaying detailed progress during the copy:
$ cp -u -v --backup=numbered me1.txt me2.txt /home/user/Backup
Enjoy efficient file management in Linux!
Conclusion
All in all, the cp (copy) command in Linux is an essential tool for duplicating files and directories within the file system. Furthermore, we discussed the purpose, syntax, and flags available for the cp command, along with practical examples to illustrate its usage.
I’m Malaikah, a Digital Forensics and Cyber Security student and CEH certified, with a passion for writing about Linux and the tech world.