How to copy files on Linux

Copying the files is probably a daily routine for anyone using Linux. Often you want to copy a file across different directories. The easiest way to copy files on Linux is to use the command line. For those who have trouble with the command line, you can use the graphical interface, but it’s recommended to stick to the command line for more flexibility. Nonetheless, this guide explains the different ways to copy files on Linux.

Copy files on Linux

You can copy the files on Linux using the graphical user interface or the command line.

Method 1: Graphic way

For anyone unfamiliar 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 choose Paste.

Alternatively, you can use the “Copy to” which will open the file system so you can navigate to your target directory.

Method 2: Command line method

Copying the files on Linux is best done from the command line. Also, Linux provides the cp command, which allows you to copy the files from one directory to another by specifying its path.

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

  1. -v: Added to make the cp command verbose.
  2. -i: Added to make the cp command interactive, especially when copying the files to a directory that contains files with the same filename.
  3. -p: Added to copy a file and its attributes like access permissions and modification dates.
  4. -b: Added to create a backup copy of the file that will be copied to the destination folder, but with a different extension.
  5. -r: Specifies recursiveness 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 this case, you only need to specify the file name or pattern and the target directory. We will use the files in the image below for this example:

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

$ cp -v bash1.sh directory1

Note that we’ve added the -v option for verbose.

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

$ cp -v file1 file2 file3 … [target-directory]

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

$ cp -v *.txt [target-directory]

Suppose you wanted to copy the files to a subdirectory of the current directory; You must provide the path. For example, if dir1 has a subdirectory called test and you want to copy a file there, we could use the following command:

$ cp -v filename ./dir/subdir

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

Let’s try repeating the command in the previous one example to see the error it throws and how to choose whether or not to overwrite the existing files.

In the previous image, if you want to overwrite the files with the same names in the destination directory, you need to type y in the terminal and then press Enter. 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 replica file name. For example, let’s make a copy of our file1.txt named replica.txt and keep its attributes. In this case the command would look like this:

$ cp -vp file1.txt replica.txt

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. We now 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 image below, we copied the linuxhint folder and all of its contents to dir1.

Copy files to different directories

If you need to copy the files located in different directories, use the following syntax:

$ p.p [options] [source-path] [destination-path]

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

The trick is to provide the exact path of the source file and destination.

Conclusion

We’ve seen the different options you have when it comes to copying the files on Linux. The commands described only take a little practice and you will soon master copying files under Linux using the command line.

Related Posts