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.
How to Copy a File Using Linux cp Command
The Linux cp command allows you to copy files and folders in Linux. The syntax for this command is as follows:
cp [source] [destination]
The “source” refers to the file or folder you want to move and “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.
Let’s walk through a few examples to illustrate how this command works.
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
ls, 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. Now, if we run
ls, we can see that our new file has been created:
README.md app.sh __init__.sh config/
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:
You can see that a copy of the “__init__.sh” file has been created in the “config” directory.
You can also 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
How to Copy a Folder Using cp
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
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 to read the Linux manual’s description of how to use the cp command.