copy command in linux with examples
![]() |
| linux command cp |
Hello, guys welcome to the next session on Linux command line tutorial. In this session we will see how we can use "cp" command in Linux. "cp" command is used to copy files and directories in Linux.
Let's see how we can use it. Just open your terminal by pressing "ctrl + alt + t" and let's see what are the options we have for "cp command".
So for writing "cp" command, you use "cp" and then options for example "-i" or "-v" or other options and then you give the name or location of your source file or directory and then you need to give the name of the destination where you want to copy the source.
$cp [option] [source] [destination]
cp command to copy a file
![]() |
| copy content of a file |
$cp [sourcefile] [destination file]
For example, I want to copy a file into a new file. So what I can do is I can just write "cp" and then I can just write the name of my "file1.txt" which I want to copy.
And then I want to copy this file into a new file called "file2.txt" which doesn't really exist. So what the CP command is going to do is, If this file doesn't exist it's going to create the new file and then copy the content of file1 to file2.
copy a file to the directory
![]() |
| Copy files and transfer to directory |
$cp [fileName] [folderName]
Now the second thing we can do here is we can copy a file to a directory. For example, I can just use "cp" and then I can just write the name of the file.
copy multiple files using cp
![]() |
| Copy multiple files |
$cp [file2] [file2] [destination]
If you want to copy the multiple files you can just say "cp" and you can give the multiple sources and then at the end, you can give the name of the destination.
linux command cp overwrite
![]() |
| Flag for cp to prevent overwrite |
$cp -i [fileName] [destination]
For example, I want to transfer two files to folder1. But the "file1.txt" is already existing inside the folder1
copy files using the path
![]() |
| copy past from parent directory |
$cp ../fileName
copy directory to new directory
![]() |
| Copy all files in a directory to other |
$cp -R [source directory] [destination directory]
Now there is one more interesting thing I want to show. For example, I want to copy the "folder1" to our "folder4" which doesn't really exist.
So in order to copy our directory which contains files. You can give a flag. Which is "-R" which is for recursive copying.
Just say "cp -R folder1 folder4" and press ENTER. And then you can see folder4 is created and we can list out the content of folder4 and you can see the content of folder1.
Conclusion
So this is how you can use a "cp command" and for more detail, you have a command "man cp" for seeing all the options which you can use with the "cp" command.
HAVE A NICE DAY 💖






