Copying the file or folder means creating a new file having duplicate content as in the existing file.

Sometimes, we need to copy the files or folders rather than having a backup program. The files can be copied with the same name, or you can change the name as well.

Copying a file, folder, or directory is a simple and basic task in the Linux operating system. Rename, delete or copy commands are used as daily purpose operations while working with the command-line interface.

Although there are multiple commands to copying the files, the “cp” and “rsync” command are widely used simplest approaches.

How to Copy Files with “cp” Command in Linux:

The “cp” command is one of the commonly used commands to perform the copy operation. You can copy files or folders from source to destination, i-e, one directory through this command.

The syntax of the “cp” command is:

$ cp [options] [source] [destination…]

Let’s take a look at an example to understand the “cp” command tool better.

In the home directory, create a “temp” folder with the text file named “text_file1.txt” and add random content to it.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/word-image-313.png" data-lazy- height="513" src="data:image/svg xml,” width=”783″>

Copy a file with the same name:

To copy a “text_file1.txt” file directory with the same name, open the terminal and type the mentioned “cp” command with the right path.

Get the folder’s path by right-clicking on the file and navigate to the “Properties” option (it is the easy way to get the path link).

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/word-image-314.png" data-lazy- height="513" src="data:image/svg xml,” width=”787″>

A dialogue box will open with the complete path of a text file:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/word-image-315.png" data-lazy- height="497" src="data:image/svg xml,” width=”465″>

Use this path with the “cp” command to copy file:

$ cp /home/wardah/temp/text_file1.txt /home/wardah/temp2

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/word-image-316.png" data-lazy- height="61" src="data:image/svg xml,” width=”815″>

This command will copy the “text_file1.txt” file to the “temp2” folder.

To verify it, type the “ls” command in the terminal:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/word-image-317.png" data-lazy- height="81" src="data:image/svg xml,” width=”812″>

Copy a file with a different name:

To copy the file in the current working directory with the different name, type the following “cp” command with file location:

$ cp /home/wardah/temp/text_file1.txt /home/wardah/temp/text_file2.txt

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/word-image-318.png" data-lazy- height="78" src="data:image/svg xml,” width=”814″>

Verify it using the “ls” command”:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/word-image-319.png" data-lazy- height="75" src="data:image/svg xml,” width=”810″>

Copy Multiple Files with “cp” Command:

To copy multiple files with the “cp” command, navigate the terminal to the directory where files are saved and then run the “cp” command with the file names you want to copy and the destination path.

$ cd /home/wardah/temp

$ cp text_file1.txt text_file2.txt text_file3.txt /home/wardah/temp2

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/word-image-320.png60c68c13a0f16.jpg" data-lazy- height="102" src="data:image/svg xml,” width=”814″>

Run the mentioned command to verify if files are copied successfully:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/word-image-321.png" data-lazy- height="75" src="data:image/svg xml,” width=”812″>

Above mentioned scenarios are how to copy a single or selected file in a directory. Now, use the wildcard character (*) to copy present files of one directory to any other specific directory.

Let’s check how it works:

Run the “ls” command to check how many files exist in the temp directory:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/word-image-323.png" data-lazy- height="80" src="data:image/svg xml,” width=”811″>

Instead of mentioning all file names in the terminal, use the wildcard (*) with the directory path to copy all the files into destination:

$ cp /home/wardah/temp/* /home/wardah/temp2

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/word-image-325.png" data-lazy- height="56" src="data:image/svg xml,” width=”812″>

Now, run the “ls” command again to check if all files are copied in the “temp2” directory:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/word-image-327.png" data-lazy- height="81" src="data:image/svg xml,” width=”812″>

How to Copy files with the “rsync” command in Linux:

The “rsync” command is another versatile Linux tool to synchronize and copy files and directories locally as well as remotely.

The syntax of the “rsync” command is to copy files is:

$ rsync [options…] [source] [destination…]

It is a pre-built tool in many Linux distribution. However, if you don’t get it on your system, install it by executing the following command:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/word-image-328.png" data-lazy- height="208" src="data:image/svg xml,” width=”807″>

To copy a file from one place to another, run the following command:

$ rsync /home/wardah/dir1/file1.txt /home/wardah/dir2/text_file.txt

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/word-image-329.png" data-lazy- height="74" src="data:image/svg xml,” width=”814″>

To confirm, type:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/word-image-330.png" data-lazy- height="72" src="data:image/svg xml,” width=”817″>

To copy all the directory files to another location, the command would be:

$ rsync -a /home/wardah/dir1/ /home/wardah/dir2

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/word-image-331.png" data-lazy- height="57" src="data:image/svg xml,” width=”814″>

(The “-a” with the “rsync” command is used to copy directories recursively)

Here are the two concepts:

If you add a trailing slash (/) with the path, it will copy the content of the source directory to the destination directory, just like shown in the image:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/word-image-332.png" data-lazy- height="509" src="data:image/svg xml,” width=”786″>

But, if you don’t add it, it will copy the source directory inside the destination directory, like:

$ rsync -a /home/wardah/dir /home/wardah/dir2

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/word-image-333.png" data-lazy- height="55" src="data:image/svg xml,” width=”812″>

The above command will copy a “dir1” directory to the “dir2” directory.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/word-image-334.png" data-lazy- height="513" src="data:image/svg xml,” width=”786″>

Conclusion:

Copying a file or directory is the basic command one can operate. One can use it multiple times while using Linux operating system.

This guide has seen the two simplest approaches, the “cp” command and the “rsync” command. Using these commands, we have learned how to copy a single file, multiple files, and even copy one directory to another.