From the previous articles Linux commands part 1 and part 2 we covered the basic administrating commands needed for creation/deletion of directories/files, view files. Today we will discuss how to copy files and directories in a Linux operating system with it’s syntax.
cp : copy command in Linux
We can use “cp” command to copy a single or multiple files and directories as well. Simply what does this command do ? “cp” command copy something from source location and pastes on the destination.
syntax : cp <options> <filename1> <filename2>
* here filename2 can be a directory path also.
eg_1 : How to copy a single file
Here, we are in foo3 directory and we have a file named example_1. Below image shows it’s content by using ‘cat example_1‘ command.
Now we copy example_1 file and pastes it with another name as example_2. What it has done ? It has copy content of first file and pastes that content with another file with a different name.
So, if you need to copy a file and save it in another name, you can do as above.
eg_2 : How to copy multiple files
Here, we have created a directory named papers.
And we need to copy previously created two files ( example_1 & example_2 ) to papers directory.
Like the above, you can copy any number of files to a single destination. You can’t copy multiple files to a single file. The destination should be a directory when you copy multiple files. It should be.
You can do the above operation by using Wildcards also. We discussed about wildcards in a previous article, Wildcards – Linux.
We will create another directory named paper_2 and we use example_* in the command. what does it means ? it copies files that starts with “example_” and * mark indicates the end can be anything. So it catches example_1 and example_2 files. Else you can use it as example_[1,2] .
eg_3 : How to copy a directory
Here, i will introduce another command for listing the files as a tree from the current directory.
You can use command tree.
* If tree is not installed in your OS, then install it by switching to root account. Do as below.
su – : To switch to root account, put the password and login. ( su – command to switch user )
yum install tree -y : To install tree package in your OS.
* above commands will be discussed in future articles.
After installing tree package, come back to your account and the previous location. Run tree command as above image.
So, For copying directories we created a directory named COPY.
From the basic knowledge, we will try to copy the directory papers to COPY directory. It gives an error. Check the below image.
What is the reason ? It’s because that is a directory. It contains files. So we need to copy the directory Recursively. We use option R to make it recursively. Recursively means it copies including all the content inside of that directory.
Above and Below both commands do the same thing. It copies the directory with it’s content to another directory.
But, check the below example. In the command it uses papers/* . Please note it copies not the directory, it copies only the files or directories inside of papers directory. Please check the below image. There it uses * Wildcard.
Now, you have learned all the mostly useful syntax’s used with cp command. There are other options we can use with cp command. Please refer the man pages for referring more. man cp, cp –help.
Other Linux commands resources