Linux Commands

How to Copy Files in Linux

Copying the files is likely a part of daily routine for anyone who is using Linux. You often want to copy a file across various directories. The easiest way of copying files in Linux is using the command line. For those who have issues 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 will discuss the various ways of copying files in Linux.

Copying Files in Linux

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

Method 1: Graphical Way

For anyone who is not comfortable with the command line, you can simply right-click on the file that 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.

Alternatively, you can use the “copy to” which opens the file system for you to navigate to your target directory.

Method 2: Command-Line Way

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

There are various options that 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 into a directory that contains files with the same filename.
  3. -p: Added to copy a file and its attributes, such as access permissions and modification dates.
  4. -b: Added to create a backup of the file being copied to the destination folder, but with a different extension.
  5. -r: It specifies recursiveness and is used when you want to copy all files in a directory.

Let’s see how the cp command is used to copy the files.

Copying Files in the Same Directory

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

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

$ cp -v dir1

Note that we added the -v option for verbose.

Also, if we need to copy more than one file, you must separate the file names with a space and list all the files that 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 match a given pattern. For instance, you could use the following command to copy all the text files. All files that match the pattern get copied to the specified directory.

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

Suppose you wanted to copy the files to a subdirectory in the current directory; you must specify the path. For instance, if dir1 has a subdirectory named test and you want to copy a file to it, 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 to repeat the command in the previous example to see the error it raises and how you can choose to overwrite the existing files or not.

In the previous image, if you want to overwrite the files having the same names in the target directory, you must type y to the terminal, 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 target file and the name of the replica file. For instance, let’s create a copy of our file1.txt called replica.txt, and let’s retain its attributes. In that case, the command would be as shown in the following:

$ cp -vp file1.txt replica.txt

To create a backup of the file that 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 to our destination folder. We now have a new backup named

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

Copying Files in Different Directories

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

$ cp [options] [source-path] [destination-path]

For instance, the command is shown in the following if we needed to copy a file in the /Pictures into the linuxhint sub-directory in the dir1.

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


We’ve seen the various options that you have when it comes to copying the files in Linux. The commands described only need a little practice, and soon you will master how to copy the files in Linux using the command line.

About the author

Denis Kariuki

Denis is a Computer Scientist with a passion for Networking and Cyber Security. I love the terminal, and using Linux is a hobby. I am passionate about sharing tips and ideas about Linux and computing.