The cp
command in Linux stands for “copy.” This can be a command-line software used to copy data and directories from one location to a few different during the record device. Thru using the cp command, consumers can create duplicates of knowledge or directories, protecting the original content material subject material.
provide
is the record or checklist you wish to have to copy, and holiday spot
is the site where you wish to have to put the copy. Various possible choices may also be added to modify the conduct of the copy, very similar to protecting record attributes or providing verbose output. It’s a fundamental and widely-used command in Linux for managing data and directories.
In this put up, we can take a look at some not unusual tactics the cp
command is used to copy data and folders in Linux.
Syntax:
cp [options] provide holiday spot
1. Duplicating a record
cp foo.txt bar.txt
This command will copy the contents of the record foo.txt
proper right into a record named bar.txt
.
Example:
Let’s say you are going to have a record named foo.txt
with the following content material subject material:
Hello, World!
If you happen to occur to run the command cp foo.txt bar.txt
, it’ll create a brand spanking new record named bar.txt
with the exact same content material subject material as foo.txt
:
Hello, World!
If bar.txt
already exists, its content material subject material will also be overwritten with the content material subject material of foo.txt
. If bar.txt does no longer exist, it’ll be created.
2. Duplicating an inventory (and its contents)
cp -R foo-folder bar-folder
The -R
selection stands for “recursive,” and it’s used to copy directories and their contents, in conjunction with subdirectories.
Proper right here’s what the command cp -R foo-folder bar-folder
does:
cp
: Invokes the copy command.-R
: Tells the command to accomplish recursively, copying all directories and subdirectories.foo-folder
: The provision checklist that you wish to have to copy.bar-folder
: The holiday spot checklist where you wish to have to copy the provision checklist.
Example
Let’s say you are going to have an inventory known as foo-folder
with the following building:
foo-folder/ ├── file1.txt └── subfolder └── file2.txt
And you wish to have to copy this entire checklist into any other checklist known as bar-folder
.
You could run the command:
cp -R foo-folder bar-folder
After running this command, the bar-folder
checklist can have the identical building as foo-folder
:
bar-folder/ └── foo-folder ├── file1.txt └── subfolder └── file2.txt
If bar-folder
does no longer exist, it’ll be created. If it does exist, the foo-folder will also be copied into it, protecting the development of foo-folder
.
Bear in mind: If you want to copy the contents of foo-folder
at once into bar-folder
without creating a foo-folder
inside bar-folder
, you would need to ensure bar-folder
exists and then run:
cp -R foo-folder/* bar-folder/
3. Show the copying expansion
cp -v foo.txt bar.txt
The -v
selection stands for “verbose,” and when used with the cp
command, it provides detailed information about the operations being performed.
Example:
Assume you are going to have a record named foo.txt
on your provide checklist, and you wish to have to create a duplicate of this record within the identical checklist with a brand spanking new identify bar.txt. You’ll use the following command:
cp -v foo.txt bar.txt
If the operation is a success, the command will output a message like this:
'foo.txt' -> 'bar.txt'
This message confirms that the record foo.txt
has been copied to bar.txt.
4. Confirmation to overwrite a record
cp -i foo.txt bar.txt
The -i
stands for “interactive”. When you use this option, the device will recommended you previous to overwriting any data. This is useful if you want to steer clear of accidentally overwriting present data.
Example:
Let’s say you are going to have a record named foo.txt
on your provide checklist and you wish to have to create a duplicate of it named bar.tx
t within the identical checklist. However, you’re no longer certain if a record named bar.txt already exists, and in addition you don’t want to overwrite it without being warned.
You could use the command:
cp -i foo.txt bar.txt
If bar.txt
already exists, the device will recommended you with a message like:
cp: overwrite 'bar.txt'?
You’ll then select to overwrite it by means of typing y
(positive) or steer clear of overwriting by means of typing n
(no).
If bar.txt
doesn’t exist, the command will simply create a duplicate of foo.txt
named bar.txt
without any recommended.
5. Copying a few data to an inventory
cp foo.txt bar.txt baz
This command will copy a duplicate of foo.txt
and bar.txt
in, into the baz
checklist. The baz
checklist will have to first exist to make certain that command to artwork.
Additional Linux directions:
Record Operations | rmdir · cd · pwd |
Record Operations | cat · cp · dd · much less · ls · mkdir · mv · tail · tar · zip |
Record Device Operations | chown · mkfs |
Networking | ping · curl · wget · iptables |
Search and Text Processing | to find · grep · sed · whatis |
Device Wisdom and Keep an eye on | env · historical past · most sensible · who |
Client and Session Keep an eye on | display · su · sudo |
The put up The right way to Reproduction Information and Folders in Linux gave the impression first on Hongkiat.
Supply: https://www.hongkiat.com/blog/linux-command-cp/
0 Comments