The Linux cp command copies a file or a folder on a computer. You can move one or more files and folders at the same time. The syntax for cp is the name of the file to be copied followed by the place the copy should be located.
Copying files and folders is a task almost everyone who works with Linux performs on a daily basis. But, using the command line, it’s not as simple as dragging a file into another window on your computer. Rather, you need to use the cp command to copy files and folders.
In this guide, we’re going to discuss how to use the Linux cp command to copy files and folders on Linux systems.
Linux cp Command: Copy a File
The Linux cp command is used for copying files and directories to another location. To copy a file, specify “cp” followed by the name of a file to copy. Then, state the location at which the new file should appear. The new file does not need to have the same name as the one you are copying.
Let’s take a look at the syntax for the Linux cp command:
cp [source] [destination]
The “source” refers to the file or folder you want to move. “destination” is the target directory where you want to move that file or folder.
When “source” and “destination” are both files, the source file will be copied into the destination file. If you specify multiple files or directories as the “source”, “destination” must be a folder into which those files and directories can be moved.
cp Linux Examples
Let’s walk through a few examples to illustrate how this command works.
cp Command in Linux: Copy File in Current Directory
One of the most common usages of the Linux cp command is to copy content files in your current directory. Using the Linux ls command, we can see that our current folder contains the following files:
README.md app.sh config/ bin/ app/
Suppose we want to create a copy of the file “app.sh” into a file called “__init__.sh”. We could do so using this command:
cp app.sh __init__.sh
When we run this command, the contents of “app.sh” are copied into a file “__init__.sh”. Because the file “__init__.sh” does not exist, it is created.
If we run ls, we can see that our a regular file has been created based on our previous file:
README.md app.sh __init__.sh config/
We specified a new name for our copied file. We did not have to copy our file and then rename it using the Linux mv command.
If you specify the same name as a file that already exists, the file that already exists will be overwritten. Indeed, the cp command can overwrite an existing file.
cp Command in Linux: Copy File to Another Directory
We can also use the Linux cp command to copy a file into another directory. Suppose we want to create a copy of our “__init__.sh” file and store it in the “config” directory. We could do so using this command:
cp __init__.sh config/
We can use ls to see the contents of our “config” folder:
This command returns:
A copy of the “__init__.sh” file has been created in the “config” directory. This file is called our “destination file,” like we referred to in the syntax example earlier.
You can specify a new file name for the copy of the file you are creating. If you wanted to create a copy of “README.md” in the “config” directory and call it “readme.txt”, you could do so using this command:
cp README.md config/readme.txt
This is similar to how we copied our file in the last example. We specified a new name for our new file.
How to Copy a Folder Using the Linux cp Command
By default, the Linux cp command does not copy a directory. But, you can still use cp to copy a folder. To do so, you need to pass the -R flag.
This will copy a folder and all of its contents into another folder. The syntax for this command is:
cp -R [source] [destination]
Suppose we want to copy the folder “bin” to “bin_backup”. We could do so using this command:
cp -R bin bin_backup
If the destination directory you specify exists, the contents of the source directory will be copied into the destination directory. Otherwise, a new folder will be created with the value you specified for “destination”.
Copy Multiple Files and Folders
The Linux cp command allows you to copy multiple files and folders. The syntax for doing so is as follows:
cp [file1] [file2] … [destination]
Suppose we want to copy “app.sh” and “__init__.sh” into a directory called “bin”. We could do so using this command:
cp app.sh __init__.sh bin/
When you’re copying multiple files, the destination you specify must be a folder.
Alternatively, if you want to copy multiple folders, you would specify folders in the same place as we specified file names above. So, if you wanted to copy the folders “bin” and “config” into the folder “app”, you could do so using this command:
cp bin config app
The Linux cp command allows you to easily copy files and folders. You can use cp to copy individual files and folders, or to copy multiple files and folders.
If you want to find out more about this command, you can run man cp on your terminal. This will open up the Linux manual’s description of how to use the cp command. The manual is quite long but it will give you incredibly detailed information on using the cp command.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Read more