The cp command in Linux stands for “replica.” This is a command-line application used to replicate information and directories from one location to some other throughout the document gadget. Through the usage of the cp command, customers can create duplicates of information or directories, conserving the unique content material.

supply is the document or listing you need to replicate, and vacation spot is the positioning the place you need to position the replica. More than a few choices will also be added to switch the habits of the replica, similar to conserving document attributes or offering verbose output. It’s a basic and widely-used command in Linux for managing information and directories.

On this submit, we will be able to have a look at some commonplace techniques the cp command is used to replicate information and folders in Linux.

Syntax:

cp [options] supply vacation spot
1. Duplicating a document
cp foo.txt bar.txt

This command will replica the contents of the document foo.txt right into a document named bar.txt.

Instance:

Let’s say you will have a document named foo.txt with the next content material:

Hi, Global!

If you happen to run the command cp foo.txt bar.txt, it is going to create a brand new document named bar.txt with the very same content material as foo.txt:

Hi, Global!

If bar.txt already exists, its content material can be overwritten with the content material of foo.txt. If bar.txt does now not exist, it is going to be created.

2. Duplicating a listing (and its contents)
cp -R foo-folder bar-folder

The -R choice stands for “recursive,” and it’s used to replicate directories and their contents, together with subdirectories.

Right here’s what the command cp -R foo-folder bar-folder does:

  • cp: Invokes the replica command.
  • -R: Tells the command to perform recursively, copying all directories and subdirectories.
  • foo-folder: The supply listing that you need to replicate.
  • bar-folder: The vacation spot listing the place you need to replicate the supply listing.

Instance

Let’s say you will have a listing referred to as foo-folder with the next construction:

foo-folder/
├── file1.txt
└── subfolder
    └── file2.txt

And you need to replicate this complete listing into some other listing referred to as bar-folder.

You may run the command:

cp -R foo-folder bar-folder

After working this command, the bar-folder listing may have the similar construction as foo-folder:

bar-folder/
└── foo-folder
    ├── file1.txt
    └── subfolder
        └── file2.txt

If bar-folder does now not exist, it is going to be created. If it does exist, the foo-folder can be copied into it, conserving the construction of foo-folder.

Be aware: If you wish to replica the contents of foo-folder without delay into bar-folder with out making a foo-folder within bar-folder, you would have to be sure bar-folder exists after which run:

cp -R foo-folder/* bar-folder/
3. Display the copying growth
cp -v foo.txt bar.txt

The -v choice stands for “verbose,” and when used with the cp command, it supplies detailed details about the operations being carried out.

Instance:

Think you will have a document named foo.txt for your present listing, and you need to create a replica of this document in the similar listing with a brand new title bar.txt. You’ll use the next command:

cp -v foo.txt bar.txt

If the operation is a hit, the command will output a message like this:

'foo.txt' -> 'bar.txt'

This message confirms that the document foo.txt has been copied to bar.txt.

4. Affirmation to overwrite a document
cp -i foo.txt bar.txt

The -i stands for “interactive”. Whilst you use this feature, the gadget will instructed you prior to overwriting any information. This comes in handy if you wish to keep away from unintentionally overwriting current information.

Instance:

Let’s say you will have a document named foo.txt for your present listing and you need to create a replica of it named bar.txt in the similar listing. Then again, you’re now not positive if a document named bar.txt already exists, and also you don’t need to overwrite it with out being warned.

You may use the command:

cp -i foo.txt bar.txt

If bar.txt already exists, the gadget will instructed you with a message like:

cp: overwrite 'bar.txt'?

You’ll then make a selection to overwrite it by way of typing y (sure) or keep away from overwriting by way of typing n (no).

If bar.txt doesn’t exist, the command will merely create a replica of foo.txt named bar.txt with none instructed.

5. Copying a couple of information to a listing
cp foo.txt bar.txt baz

This command will reproduction a replica of foo.txt and bar.txt in, into the baz listing. The baz listing should first exist to ensure that command to paintings.

Extra Linux instructions:
Listing Operations rmdir · cd · pwd
Document Operations cat · cp · dd · much less · ls · mkdir · mv · tail · tar · zip
Document Gadget Operations chown · mkfs
Networking ping · curl · wget · iptables
Seek and Textual content Processing to find · grep · sed · whatis
Gadget Knowledge and Control env · historical past · most sensible · who
Consumer and Consultation Control display · su · sudo

The submit The right way to Reproduction Information and Folders in Linux seemed first on Hongkiat.

WordPress Website Development Source: https://www.hongkiat.com/blog/linux-command-cp/

[ continue ]