How to Copy Files in Linux

Copying the files is probably a part of the daily routine for anyone using Linux. You often want to copy a file to multiple directories. The easiest way to copy files on Linux is by using the command line. For those who have problems with the command line, you can use the graphical interface, but it is recommended to stick to the command line for flexibility. Nonetheless, this guide will discuss the various ways to copy files on Linux.

Copy

files to Linux You can use the graphical interface or command line to

copy the files to Linux

.

Method 1: Graphical Form

For anyone who is not comfortable with the command line, you can simply right-click on the file you want to copy and select the copy option. Then navigate to where you want to copy the file, right-click, and select the paste command.

<img src

=”https://linuxhint.com/wp-content/uploads/2022/10/copy-files-linux-01.png” alt=”” />

Alternatively, you can use the “copy tothat opens the file system so that you can navigate to your destination directory

.

Method 2: Command Line Form

Copying the files to Linux is best when using the command line. In addition, Linux offers the cp command that you can use to copy files from one directory to another by specifying their path.

There are several options you can use with the cp command. The following is a quick cheat sheet

: -v: Added for the cp command to be verbose. -i: Added to make the

  1. cp command interactive, especially when copying files to a directory containing files with the same file name
  2. . –

  3. p: Added to copy a file and its attributes, such as access permissions and modification dates.
  4. b: It was added to create a backup of the file being copied to the destination folder, but with a different extension.
  5. -r: Specifies recursion and is used when you want to copy all files in a directory.

Let’s see how to use the cp command to copy the

files. Copy files to the

same directory

Suppose you want to copy the files to another directory in the same location. In that case, you just need to specify the file name or pattern and the destination directory. We will use the files in the following image for this example

:

In the given image, if we needed to copy the bash1.sh to the dir1 folder, the command would be:

Note that we added the -v option for verbose.

Also, if we need to copy more than one file, you need to separate the file names with a space and list all the files you want to copy.

If you need to copy the files with the same extension, you can use the wildcard to match a certain pattern. For example, you can use the following command to copy all text files. All files that match the pattern are copied to the specified directory.

Suppose you want to copy the files to a subdirectory of the current directory; you must specify the path. For example, if dir1 has a subdirectory named test and you want to copy a file to it, we could use the following command:

If you copy the files

to a directory that contains the same file name, you will end up overwriting the existing files unless you add the -i option for interactivity.

Let’s try repeating the command in the example above to see the error it generates and how you can choose to overwrite existing files or not.

In the image above, if you want to overwrite files that have the same names in the destination directory, you need to type y in the terminal and , then press the Enter key. If you don’t want to overwrite, press n

.

If you want to create a copy of a file in the same directory, you must specify the destination file and the name of the replica file. For example, we’re going to create a copy of our file1.txt called a replica.txt, and we’re going to keep its attributes. In that case, the command would be as shown in the following:

To create a backup copy of the file you

are copying, add the -b flag and note the new backup file that is created with a different extension. Let’s create a backup of bash1.sh in our destination folder. Now we have a new backup called bash1.sh

If you want to copy a directory and all files, add the -r option and specify the path. For example, in the following image, we copy the linuxhint folder and all its contents to dir1.

<img src

=”https://linuxhint.com/wp-content/uploads/2022/10/copy-files-linux-10.png” alt=”” /> Copy files to different directories

When you must copy files located in different directories, use the following syntax:

For example, the command is shown below if we need to copy a file to the /Pictures subdirectory in the linuxhint subdirectory on dir1.

The trick is to specify the exact path of the source file and destination. Conclusion We have seen the various options you have when it comes to copying the files on Linux.

The described commands only need a little practice, and you’ll soon master how to copy the files to Linux using the command line.