linux command cp for copy directory and files | Pro Tech guides

linux command cp for copy directory and files

cp command is used to copy files or group of files or directory. cp stands for copy. It provides an exact copy of content.

copy command in linux with examples

linux command cp
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.

Syntax :
$cp [option] [source] [destination]

cp command to copy a file 

linux copy files
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 to folder
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.

I want to copy it to a "folder1" which is empty right now.

So now I will do "cp" and then the name of the file.

For example, I want to copy "file1.txt" and then the name of the directory. which is my destination.

copy multiple files using cp

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

cp flag to 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 

And we don't want to overwrite the "file1.txt" which is inside folder1

So to avoid the overwriting what you can do is you can give cp and add a flag which is "-i" which stands for interactive.

Which ask before overwriting. So when you press ENTER. It will ask you the question?

If you give "y" then it's going to overwrite your file and if you give "n" then it's going to not overwrite it.

So you can use the flag "-i" for the interactive interface.

copy files using the path

copy files from parent directory to current directory
copy past from parent directory

$cp ../fileName

For example, I'm in "folder3" I want to give the "cp" command from here.

So the "file1.txt" and "file2.txt" exist in the desktop not the folder3 and now I'm in "folder3".

Just say "cp ../file1.txt ../file2.txt ."

So we can just go 1 step back or 1 folder above by using these two dots (..)

You can use this dot (.) to show or indicate that this is our current directory or destination.

Press ENTER and it's going to copy these two files to the "folder3"

copy directory to new directory

copy directory in linux
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.

So I hope you've enjoyed this tutorial please rate comment and bye for now.

HAVE A NICE DAY 💖

Name

Apps,3,Games,1,Linux,38,Linux Commands,37,Network-Devices,1,Networking,1,Terminal Apps,2,Termux,17,Userland,3,Windows,1,
ltr
item
Pro Tech guides: linux command cp for copy directory and files
linux command cp for copy directory and files
cp command is used to copy files or group of files or directory. cp stands for copy. It provides an exact copy of content.
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgIZlOoDOoytqkbamvIawllL1ghzDopjHMqGd-pYBRa_t_XDFi2J0678XTRlddW_SYhtSyesbZsuoyLUWH0i40tbVe7RcCeJYa6X9_gIl0Vxm0dX5oOnFpTab3hU1Djty2FQ9py0l6GU2Y/s16000/cp+command+in+linux.png
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgIZlOoDOoytqkbamvIawllL1ghzDopjHMqGd-pYBRa_t_XDFi2J0678XTRlddW_SYhtSyesbZsuoyLUWH0i40tbVe7RcCeJYa6X9_gIl0Vxm0dX5oOnFpTab3hU1Djty2FQ9py0l6GU2Y/s72-c/cp+command+in+linux.png
Pro Tech guides
https://devops-digest.blogspot.com/2020/11/linux-command-cp-for-copy-files-and.html
https://devops-digest.blogspot.com/
https://devops-digest.blogspot.com/
https://devops-digest.blogspot.com/2020/11/linux-command-cp-for-copy-files-and.html
true
7053638852026728047
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content