How to Copy Paste in Linux Terminal

how to copy paste in linux terminal.

 How to Copy Paste in Linux Terminal


How to Copy Paste in Linux Terminal


Do you need to copy files and folders on Linux? Save your time by using the cp command to move data using the Linux terminal.


Copying files and directories on Linux can be very fast using the terminal. This article will discuss how to copy and paste in Linux Terminal using the cp command.


Also Read: 25 Basic Linux Commands to Learn

TABLE OF CONTENTS

1. What is the cp command in Linux?

2. How to Copy Paste in Linux Terminal

3. Syntax cp

4. Copy Files with cp

5. Using cp to copy folders/directories

6. Useful cp options

7. Conclusion


What is the cp command in Linux?


cp is a Command-line utility for Unix and Linux systems capable of copying files and folders, cp is basically available on every Linux distro. The syntax is simple, and it's easiest to use if you open a terminal to the directory containing the files you want to copy.


How to Copy Paste in Linux Terminal


cp syntax


The syntax for cp is very similar to the mv command, in that you only need to specify the source (the file or folder you want to copy) and the destination (the directory or filename for the copy).

cp [options] >source>… >destination>


Your command can contain multiple sources, but there can only be one destination. The destination can be another directory, a new file name, or both.


Copy Files With cp


If you want to make a copy of one file in the same directory, but with a different name, use a command like this:

Linux cp command

cp file.txt newfile.txt


To do the same thing, but in a sub-directory, use a command like this:

cp file.txt Backup/newfile.txt


The above command assumes you have a folder called Backup in your current directory.


If you don't want a new name for the file, just specify the directory and not the filename in the destination:

cp file.txt Backup


To copy multiple files with cp, simply write down all the files you want to copy, separated by spaces, before giving the destination.

The cp Multiple Files command

cp file1.txt file2.txt file3.txt Backup


To save time copying multiple files, you can use the wildcard tag (*), to automatically copy all files in a directory with the same extension, using something like the example below:

Command cp Wildcard

cp *.txt Backup


The above command will find all files ending in .txt in the current directory and copy them to the Backup directory.

Of course, this is only useful if all or most of the files you want to copy have the same extension, or some other similarity in file names.


Using cp to copy folders/directories


If you want the directory and all of its contents to be copied to a new location, you must specify the -R option. Here's an example:

cp -R Files Backup


The above command will copy the file folder and place the copy in a folder called Backup.


If you want the contents of the folder to be copied, but not the folder, you must use the -T option:

cp -RT Files Backup


Useful cp options


This commonly used option for the cp command is especially helpful if you are concerned about possible overwriting or file attribute conflicts.


You'll notice in the previous command that you didn't get a message from the terminal confirming that something had happened. To see what happened, use the -v (verbose) option:

Verbose cp command

cp -v file.txt newfile.txt


If you're worried about accidental overwriting, you can set the -i option to always ask for confirmation when there's a conflicting name for the same file. For example:

Command cp Interactive Mode

cp -i file.txt newfile.txt


With the above command, if there is already a file named newfile.txt, cp will ask if you are sure you want to overwrite it.


You can also preserve file attributes, such as user ownership, file mode, and modification date, with the -p option:

cp -p file.txt newfile.txt


The -p (preserve) option is useful if you have multiple users on your system, or if you have other sync operations that are sensitive to file modification dates.


Conclusion


So that's how to copy paste in Linux terminal. Now you know how to use the cp command to copy files easily and safely. So get in the habit of always using the command-line to do the job much faster.

  __________________________________________________________________________________________________________________________________________________________________________________________________________________ ____________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________ _________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________ ________________________________________________________________________________________________ ________

Previous Post Next Post